1

I created an advanced snake game in python/pygame. The problem is that the game has gotten to the point that when I put the new version on our family computer so everyone can play it, it runs too slowly.

I ran the gameplay with cprofile and got the states, but I'm not sure what is normal and what should, or can be improved.

Here is a video of the exact gameplay I ran using cProfile, followed by the cProfile results. Thanks for any help!

https://www.youtube.com/watch?v=Txyoq7df_5c&feature=youtu.be When the screen goes black is the moment when I exited the game.

         1641078 function calls (1641062 primitive calls) in 60.384 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000   60.384   60.384 <string>:1(<module>)
     1785    0.021    0.000    0.021    0.000 Snakey Deluxe.py:117(timeStamp)
        1    0.000    0.000    0.000    0.000 Snakey Deluxe.py:128(playMusic)
     4603    0.045    0.000    8.386    0.002 Snakey Deluxe.py:223(mess_to_screen)
     4603    0.022    0.000    0.504    0.000 Snakey Deluxe.py:229(text_objects)
        1    0.000    0.000    0.051    0.051 Snakey Deluxe.py:233(quit_game)
        1    0.238    0.238   60.384   60.384 Snakey Deluxe.py:801(gameLoop)
     4603    0.025    0.000    2.257    0.000 __init__.py:1114(resource_exists)
     4603    0.022    0.000    2.597    0.001 __init__.py:1130(resource_stream)
     9206    0.056    0.000    0.459    0.000 __init__.py:1556(__init__)
     4603    0.019    0.000    0.935    0.000 __init__.py:1569(has_resource)
     9206    0.042    0.000    0.630    0.000 __init__.py:1638(_fn)
     9206    0.039    0.000    2.356    0.000 __init__.py:1656(__init__)
     9206    0.198    0.000    1.858    0.000 __init__.py:1660(_setup_prefix)
     4603    0.013    0.000    0.597    0.000 __init__.py:1677(_has)
     4603    0.019    0.000    1.337    0.000 __init__.py:1686(get_resource_stream)
     9206    0.023    0.000    0.030    0.000 __init__.py:2935(_get_mro)
     9206    0.041    0.000    0.081    0.000 __init__.py:2942(_find_adapter)
     9206    0.071    0.000    2.536    0.000 __init__.py:389(get_provider)
       32    0.001    0.000    0.001    0.000 apples.py:122(check)
     1824    0.041    0.000    0.063    0.000 apples.py:145(update)
     2270    0.006    0.000    0.009    0.000 apples.py:169(eat)
      893    0.003    0.000    0.003    0.000 apples.py:192(appleGrow)
        1    0.000    0.000    0.000    0.000 apples.py:36(__init__)
    48/32    0.001    0.000    0.003    0.000 apples.py:48(apples)
     4603    0.014    0.000    0.583    0.000 genericpath.py:16(exists)
     9206    0.081    0.000    0.572    0.000 ntpath.py:104(join)
    73648    0.661    0.000    1.671    0.000 ntpath.py:139(splitdrive)
    55236    0.447    0.000    1.924    0.000 ntpath.py:210(split)
     9206    0.023    0.000    0.386    0.000 ntpath.py:251(dirname)
    73648    0.135    0.000    0.214    0.000 ntpath.py:35(_get_empty)
    82854    0.152    0.000    0.241    0.000 ntpath.py:41(_get_sep)
    73648    0.137    0.000    0.212    0.000 ntpath.py:47(_get_altsep)
    64442    0.121    0.000    0.198    0.000 ntpath.py:53(_get_bothseps)
    82854    0.153    0.000    0.244    0.000 ntpath.py:65(_get_colon)
     4603    0.021    0.000    4.876    0.001 pkgdata.py:34(getResource)
      182    0.001    0.000    0.002    0.000 random.py:170(randrange)
      182    0.001    0.000    0.001    0.000 random.py:220(_randbelow)
        1    0.000    0.000    0.000    0.000 snake_skins.py:51(rainbowColor)
        1    0.000    0.000    0.000    0.000 snake_skins.py:58(colorMaker)
        1    0.000    0.000    0.000    0.000 snakes.py:19(__init__)
     1824    0.003    0.000    0.004    0.000 snakes.py:316(turnCheck)
     1136    0.004    0.000    0.006    0.000 snakes.py:340(turnNow)
     1824    0.238    0.000    0.595    0.000 snakes.py:400(draw)
       31    0.000    0.000    0.000    0.000 snakes.py:415(popScore)
        1    0.000    0.000    0.000    0.000 snakes.py:429(popObj)
     1824    0.013    0.000    0.014    0.000 snakes.py:432(objCheck)
     1824    0.070    0.000    0.096    0.000 snakes.py:97(update)
     4603    0.025    0.000    0.214    0.000 sysfont.py:36(_simplename)
     4603    0.105    0.000    0.183    0.000 sysfont.py:37(<listcomp>)
     4603    2.646    0.001    7.522    0.002 sysfont.py:534(font_constructor)
     4603    0.046    0.000    7.804    0.002 sysfont.py:548(SysFont)
        1    0.000    0.000   60.384   60.384 {built-in method exec}
        1    0.000    0.000    0.000    0.000 {built-in method exit}
    36824    0.036    0.000    0.036    0.000 {built-in method getattr}
     1825    0.138    0.000    0.138    0.000 {built-in method get}
   395858    0.437    0.000    0.437    0.000 {built-in method isinstance}
   134283    0.113    0.000    0.113    0.000 {built-in method len}
        1    0.000    0.000    0.000    0.000 {built-in method load}
     4603    1.009    0.000    1.009    0.000 {built-in method open}
        1    0.000    0.000    0.000    0.000 {built-in method play}
        1    0.051    0.051    0.051    0.051 {built-in method quit}
    44124    0.242    0.000    0.242    0.000 {built-in method rect}
        1    0.000    0.000    0.000    0.000 {built-in method set_visible}
        1    0.000    0.000    0.000    0.000 {built-in method set_volume}
     4603    0.569    0.000    0.569    0.000 {built-in method stat}
     1824    1.437    0.001    1.437    0.001 {built-in method update}
     4733    0.004    0.000    0.004    0.000 {method 'append' of 'list' objects}
      182    0.000    0.000    0.000    0.000 {method 'bit_length' of 'int' objects}
    53544    3.773    0.000    3.773    0.000 {method 'blit' of 'pygame.Surface' objects}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
    46030    0.055    0.000    0.055    0.000 {method 'endswith' of 'str' objects}
     1825    0.914    0.001    0.914    0.001 {method 'fill' of 'pygame.Surface' objects}
     9206    0.012    0.000    0.012    0.000 {method 'get' of 'dict' objects}
     4603    0.005    0.000    0.005    0.000 {method 'get_rect' of 'pygame.Surface' objects}
      255    0.000    0.000    0.000    0.000 {method 'getrandbits' of '_random.Random' objects}
    46030    0.040    0.000    0.040    0.000 {method 'isalnum' of 'str' objects}
     4603    0.006    0.000    0.006    0.000 {method 'join' of 'str' objects}
    87457    0.082    0.000    0.082    0.000 {method 'lower' of 'str' objects}
       63    0.000    0.000    0.000    0.000 {method 'remove' of 'list' objects}
     4603    0.476    0.000    0.476    0.000 {method 'render' of 'pygame.font.Font' objects}
    73648    0.090    0.000    0.090    0.000 {method 'replace' of 'str' objects}
    13809    0.025    0.000    0.025    0.000 {method 'split' of 'str' objects}
     1824   44.823    0.025   44.823    0.025 {method 'tick' of 'Clock' objects}

Further information: when I ran the game on the family computer the game would run especially slow whenever the level up animation would pop up. But that only confuses me because it's really just doing the same thing as when it draws everything else to the screen, except that the position is increased every frame.

Thomas Hauser
  • 55
  • 1
  • 10

0 Answers0