S
S
Span4ev2022-02-21 02:17:49
Python
Span4ev, 2022-02-21 02:17:49

Why does my pygame game slow down?

I'm making a game based on Metiz's book (with which, probably, everyone has already got it))
And everything seems to be fine, but the result is displayed with ... I don't know ... 10 fps? Five? Two?
So I wonder if it's the python or me? Maybe I somehow rewrote the code from the book, which, nevertheless, works? Is it a cycle thing? Maybe you need to install fresh drivers for the vidyuhi))
maybe it's 4770k, 1070, 16RAM and you need to throw it in the trash and buy something more serious?
But seriously, is this how python + paygame works with simple animation? If so, then this is sad and there is no point in learning Python and taking it to motivate the game.
Or did I somehow crookedly rewrite the cycle? Well, I would have rewritten it crookedly - it would not have worked, probably. Could it be that some parenthesis in the wrong place (as an example) has that effect? I didn’t add any gag, I don’t know either Python or Paygame at such a level that I can improvise in such things.

Here is a video of what it looks like.
https://youtu.be/EK6q-e2Bh3k
Screen recording did not affect the FPS in any way.
Metiz himself does not indulge in fashionable things, like:

fpsClock = pg.time.Clock()
fpsClock.tick(360)


But I tried - it didn't really work. Unless if you set fpsClock.tick (60000) - then it will be a little more cheerful. +- 3 fps

I did not throw all the code - a lot of code. Yes, and I think that here every connoisseur already knows this game by heart from the book because of the countless questions of people like me.
But you need to eat, I'll throw off what you ask.

Before that, everything "flyed" without fpsClock = pg.time.Clock(). It is obvious that the cycle is so loading. But I just don't know if that's the way it should be, so I'm asking you.

The problem was in the main loop. Briefly, it was like this:

def start():

  pg.init()
  settings = Settings()
  screen = pg.display.set_mode((settings.screen_width, settings.screen_height))
  pg.display.set_caption('Space Inv version 2')
  ship = Ship(settings, screen)
  bullets = Group() 
  aliens = Group() 
  
  while True:
    
    engine.check_events(settings, screen, ship, bullets)
    ship.update_ship_pos()
    engine.update_bullets(bullets)
    # Вызываю метод создания армии пришельцев постоянно
    engine.create_aliens_army(settings, screen, ship, aliens)
    engine.update_screen(settings, screen, ship, aliens, bullets)

start()


And you need it like this:

def start():

  pg.init()
  settings = Settings()
  screen = pg.display.set_mode((settings.screen_width, settings.screen_height))
  pg.display.set_caption('Space Inv version 2')
  ship = Ship(settings, screen)
  bullets = Group() 
  aliens = Group()
  # Метод создания армии пришельцев нужно вызывать только один раз
  engine.create_aliens_army(settings, screen, ship, aliens)
  
  while True:
    
    engine.check_events(settings, screen, ship, bullets)
    ship.update_ship_pos()
    engine.update_bullets(bullets)
    engine.update_screen(settings, screen, ship, aliens, bullets)

start()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Span4ev, 2022-02-21
@Span4ev

Get it right. For those who run into the same situation, here's an explanation:
It's only me that's slow here, because
I ran the engine.create_aliens_army(settings, screen, ship, aliens) function of creating aliens in the main While loop, because of which all other loops were executed every nanosecond. I moved the call to create an army from the main loop and everything was fine.
Watch where you call methods.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question