S
S
Span4ev2022-02-11 06:20:58
Python
Span4ev, 2022-02-11 06:20:58

What is wrong with pygame performance and this code?

I copied the code from one site, pasted it to myself and launched it, to see how it works.
Fucked up with performance. The object moves for a second, then freezes for 7 seconds (well, it's different)
Hanging, not moving, waiting for something. It happens that the movement occurs and after half a minute

I closed the browser and everything superfluous to the heap did not help. At the same time, the load of the processor and memory is around 20%

Why? Are some quantum calculations being made here that suffocate Python or paygame?

Why do I need to know why it works so slowly?

1. Perhaps this is a bad example of code and how not to do it.
2. Maybe everything depends on the performance (Potential) of pygame and it should be so, which means pygame is such a thing.
3. Maybe I have problems with the RAM that I did not notice before, or with the screw, with the axis, with the mouse and the webcam.
4. Weak PC for such calculations: i7 4770k, 16 RAM, SSD

I played with fapeesik, even 600 does not save. If everything is fine with you, then this is a bad sign for me. I will look for the reason

I managed to write this text, and the object (blue circle) has not yet reached the edge of the screen

itself:

import pygame

pygame.init()

width = 600 # ширина окна
height = 400 # высота окна
radius = 20   # радиус круга
pos_x = 300 # координата x центра круга
pos_y = 200 # координата y центра круга

green = (0, 255, 0)
blue = (0, 0, 255)
speed = 1 # скорость перемещения
move_x = speed
move_y = speed

screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Игровое окно")
screen.fill(green)
pygame.display.flip()
FPS = 60
clock = pygame.time.Clock()

running = True
while running:
  for event in pygame.event.get():
     
    if event.type == pygame.QUIT:
      running = False
    pos_x += move_x
    pos_y += move_y

    if pos_y + radius > height or pos_y - radius < 0:
      move_y = move_y * -1
    if pos_x + radius > width or pos_x - radius < 0:
      move_x = move_x * -1
  
  screen.fill(green)
  pygame.draw.circle(screen, blue, (pos_x, pos_y), radius)
  pygame.display.flip()
  clock.tick(FPS)
  
pygame.quit()

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
S1riyS, 2022-02-11
@Span4ev

Remove block with position check from loopfor event in pygame.event.get()

V
Vindicar, 2022-02-11
@Vindicar

Measure the execution time of individual sections of the code using time.perf_counter() , find out what it is thinking about.
For me, the most likely candidates are pygame.event.get() and screen.fill().

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question