Dec-06-2019, 02:04 AM
(This post was last modified: Dec-06-2019, 02:13 AM by xBlackHeartx.)
Actually, my game doesn't lag anymore. As for the lag part, I found it was because of a list I was using to draw the snake's tail. Every time the snake moves (which is does every second about), it first adds its current coordinates to an ever growing list. This of course caused the list to grow rather quickly. I deduced this after I realized that the game would continue to slow down at the same rate regardless of whether or not the snake grew. I fixed this by adding in code to shorten the list to the current length of the snake.
Of course, as I said, I'm still having a problem with the thing taking up an insane amount of RAM. I mean, seriously here, this is a game that Atari could've run (its actually based on a snake game I once had that came with a port of several atari games, which included things like pitfall), and my gaming computer here has 1 gig of RAM! Why on earth would such a primitive 200-line program (whose length includes spaces and comments) take up so much RAM? I've heard that python programs are infamous for this, but this is just ridiculous. I was originally planning on putting this thing on my laptop, but I honestly doubt it could run it.
And if you want to see my code in its current form, here:
Also, Metalbur here was the one that suggested I switch to limiting the FPS rather than using the time.delay command to keep the game from running insanely fast. So yeah, that's the solution HE offered me. Also, I wasn't experiencing any graphical glitches (other than the food occasionally spawning on top of the snake until I turned its function into a do-while loop). Interestingly enough btw, I DID start to get graphical glitches in a version I made that plays itself (so that I could see my own victory screen). I upped the fps to keep it from taking forever, but I noticed that the thing doesn't render the snake right when I do that. Strangely enough btw, I have never shown that version on here or even mentioned it. So, how did Metalbur even know that by using code HE suggested to me that I would have glitches if I changed it, when I never revealed that I was having that problem? He even told me what I should set my fps at btw.
As for why I changed to limiting the fps rather than using time.delay, its because using time.delay created input lag. It also made the sound effects delayed, but that was fixed once I switched to limiting the fps (which, looking at tutorials online, is a common thing to do for snake games it appears).
Of course, as I said, I'm still having a problem with the thing taking up an insane amount of RAM. I mean, seriously here, this is a game that Atari could've run (its actually based on a snake game I once had that came with a port of several atari games, which included things like pitfall), and my gaming computer here has 1 gig of RAM! Why on earth would such a primitive 200-line program (whose length includes spaces and comments) take up so much RAM? I've heard that python programs are infamous for this, but this is just ridiculous. I was originally planning on putting this thing on my laptop, but I honestly doubt it could run it.
And if you want to see my code in its current form, here:
import random import pygame pygame.mixer.pre_init(44100,-16,2,2048) pygame.mixer.init() pygame.init() screenWidth=500 screenHeight=500 mainWindow=pygame.display.set_mode((screenWidth,screenHeight)) pygame.display.set_caption("PySnake") x=screenWidth/2 y=screenHeight/2 snakeSize=50 move="down" length=0 path=[] fps=6 fpsClock=pygame.time.Clock() win=True loseGameSound=pygame.mixer.Sound('pysnake_lose_game.wav') eatSound=pygame.mixer.Sound('pysnake_eat.wav') bleep=pygame.mixer.Sound('pysnake_bleep.wav') font=pygame.font.SysFont('arialblack',20) def frandxy(): global posx global posy if length>=((screenWidth/snakeSize)*(screenHeight/snakeSize))-1: winGame() posx=snakeSize*(random.randint(0,screenWidth/snakeSize-1)) posy=snakeSize*(random.randint(0,screenWidth/snakeSize-1)) if (posx,posy) in path or (posx,posy)==(x,y): frandxy() def closeGame(): bleep.play() pygame.time.delay(300) pygame.quit() quit() def playAgain(): global x global y global length global move global path while True: for event in pygame.event.get(): if event.type==pygame.QUIT: closeGame() key=pygame.key.get_pressed() if key[pygame.K_ESCAPE]: closeGame() if key[pygame.K_y]: bleep.play() x=screenWidth/2 y=screenHeight/2 length=0 move="down" path=[] game() if key[pygame.K_n]: closeGame() if win==False: mainWindow.fill((0,0,0)) if win==True: mainWindow.fill((0,255,0)) pygame.draw.rect(mainWindow,(0,255,0),(screenWidth/2-snakeSize*3,\ screenHeight/2-snakeSize,\ snakeSize*6,snakeSize*2)) font=pygame.font.SysFont('arialblack',20) textTop=font.render('Play Again?',0,(0,0,0)) textBottom=font.render(('(y)es/(n)o'),0,(0,0,0)) mainWindow.blit(textTop, (screenWidth/2-textTop.get_width()/2,screenHeight/2-snakeSize)) mainWindow.blit(textBottom, (screenWidth/2-textBottom.get_width()/2,screenHeight/2)) pygame.display.update() def loseGame(): loseGameSound.play() while True: for event in pygame.event.get(): if event.type==pygame.QUIT: closeGame() key=pygame.key.get_pressed() if key[pygame.K_ESCAPE]: closeGame() if key[pygame.K_RETURN]: bleep.play() playAgain() mainWindow.fill((0,0,0)) pygame.draw.rect(mainWindow,(0,255,0),(screenWidth/2-snakeSize*3,\ screenHeight/2-snakeSize,\ snakeSize*6,snakeSize*2)) font=pygame.font.SysFont('arialblack',20) textTop=font.render('GAME OVER',0,(0,0,0)) textBottom=font.render(('Your Length Was: '+str(length)),0,(0,0,0)) mainWindow.blit(textTop, (screenWidth/2-textTop.get_width()/2,screenHeight/2-snakeSize)) mainWindow.blit(textBottom, (screenWidth/2-textBottom.get_width()/2,screenHeight/2)) pygame.display.update() def winGame(): while True: for event in pygame.event.get(): if event.type==pygame.QUIT: closeGame() key=pygame.key.get_pressed() if key[pygame.K_ESCAPE]: closeGame() if key[pygame.K_RETURN]: bleep.play() playAgain() mainWindow.fill((0,255,0)) textTop=font.render('Your Snake Filled the Screen!',0,(0,0,0)) textBottom=font.render(('Congratulations!'),0,(0,0,0)) mainWindow.blit(textTop, (screenWidth/2-textTop.get_width()/2,screenHeight/2-snakeSize)) mainWindow.blit(textBottom, (screenWidth/2-textBottom.get_width()/2,screenHeight/2)) pygame.display.update() def game(): global x global y global move global length global path global fpsClock global win while True: for event in pygame.event.get(): if event.type==pygame.QUIT: closeGame() key=pygame.key.get_pressed() if key[pygame.K_UP] and move != "down": move="up" if key[pygame.K_w] and move !="down": move="up" if key[pygame.K_DOWN] and move != "up": move="down" if key[pygame.K_s] and move != "up": move="down" if key[pygame.K_RIGHT] and move != "left": move="right" if key[pygame.K_d] and move != "left": move="right" if key[pygame.K_LEFT] and move != "right": move="left" if key[pygame.K_a] and move != "right": move="left" if key[pygame.K_ESCAPE]: closeGame() path.insert(0,(x,y)) #move snake if move=="down" and y<=screenHeight-snakeSize: y+=snakeSize if move=="down" and y>=screenHeight: y=0 if move=="up" and y>=0: y-=snakeSize if move=="up" and y<0: y=screenHeight-snakeSize if move=="right" and x<=screenWidth-snakeSize: x+=snakeSize if move=="right" and x>=screenWidth: x=0 if move=="left" and x>=0: x-=snakeSize if move=="left" and x<0: x=screenWidth-snakeSize if x==posx and y==posy: length+=1 eatSound.play() frandxy() if (x,y) in path: win=False loseGame() mainWindow.fill((0,0,0)) #draw snake if length>0: for i in range(length): pygame.draw.rect(mainWindow, (0,255,0),(path[i][0],path[i][1], snakeSize,snakeSize)) pygame.draw.rect(mainWindow, (0,255,0),(x, y, snakeSize, snakeSize)) #draw food pygame.draw.rect(mainWindow, (255,0,0),(posx,posy, snakeSize, snakeSize)) if len(path)>length: path=path[:(length)] pygame.display.update() fpsClock.tick(fps) def start(): while True: for event in pygame.event.get(): if event.type==pygame.QUIT: closeGame() key=pygame.key.get_pressed() if key[pygame.K_ESCAPE]: closeGame() if key[pygame.K_e]: closeGame() titleFont=pygame.font.SysFont('arialblack',50) title=titleFont.render('PySnake',0,(0,255,0)) enterPrompt=font.render('Press Enter to Start',0,(0,255,0)) mainWindow.blit(title,(screenWidth/2-title.get_width()/2,screenHeight/2-snakeSize*2)) mainWindow.blit(enterPrompt,(screenWidth/2-enterPrompt.get_width()/2,screenHeight/2)) for event in pygame.event.get(): if event.type==pygame.QUIT: closeGame() key=pygame.key.get_pressed() if key[pygame.K_RETURN]: bleep.play() game() if key[pygame.K_ESCAPE]: closeGame() pygame.display.update() frandxy() start()
Also, Metalbur here was the one that suggested I switch to limiting the FPS rather than using the time.delay command to keep the game from running insanely fast. So yeah, that's the solution HE offered me. Also, I wasn't experiencing any graphical glitches (other than the food occasionally spawning on top of the snake until I turned its function into a do-while loop). Interestingly enough btw, I DID start to get graphical glitches in a version I made that plays itself (so that I could see my own victory screen). I upped the fps to keep it from taking forever, but I noticed that the thing doesn't render the snake right when I do that. Strangely enough btw, I have never shown that version on here or even mentioned it. So, how did Metalbur even know that by using code HE suggested to me that I would have glitches if I changed it, when I never revealed that I was having that problem? He even told me what I should set my fps at btw.
As for why I changed to limiting the fps rather than using time.delay, its because using time.delay created input lag. It also made the sound effects delayed, but that was fixed once I switched to limiting the fps (which, looking at tutorials online, is a common thing to do for snake games it appears).