Dec-06-2019, 02:53 AM
(This post was last modified: Dec-06-2019, 02:56 AM by xBlackHeartx.)
I did try to put everything into one loop, but I haven't been able to get the thing to run (when I press enter at the start screen, it just freezes and I have no clue why). That's why the version I actually play uses a separate loop for each screen. For some reason, that's the only way I could find to do it.
Here's that version if anyone's interested:
And yeah, having a quit() function may be kinda silly, but it feels a lot more elegant than just letting the game crash (which is what mine used to do when you lost).
Here's that version if anyone's interested:
import random import pygame pygame.init() screenWidth=500 screenHeight=500 mainWindow=pygame.display.set_mode((screenWidth,screenHeight)) pygame.display.set_caption("PySnake") #snake's graphics x=screenWidth/2 y=screenHeight/2 snakeSize=50 move="down" length=0 path=[] #food's graphics posx=0 posy=0 #fps fps=60 fpsClock=pygame.time.Clock() #game states win=True state='start' #sound effects 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(): 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(): pygame.quit() quit() frandxy() 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 state=='start': 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() state='game' if state=='game': 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() if key[pygame.K_e]: 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 state='end' if length>=((screenWidth/snakeSize)*(screenHeight/snakeSize))-1: state='end' mainWindow.fill((0,0,0)) #draw snake fps=6 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)) fps=60 if len(path)>length: path=path[:(length)] if state=='end': key=pygame.key.get_pressed() if key[pygame.K_ESCAPE]: closeGame() if key[pygame.K_RETURN]: bleep.play() state='playAgain' if win: 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)) if win==False: textTop=font.render('GAME OVER',0,(0,0,0)) textBottom=font.render(('Your Length Was: '+str(length)),0,(0,0,0)) mainWindow.fill((0,0,0)) pygame.draw.rect(mainWindow,(0,255,0),(screenWidth/2-snakeSize*3,\ screenHeight/2-snakeSize,\ snakeSize*6,snakeSize*2)) mainWindow.blit(textTop, (screenWidth/2-textTop.get_width()/2,screenHeight/2-snakeSize)) mainWindow.blit(textBottom, (screenWidth/2-textBottom.get_width()/2,screenHeight/2)) if state=='playAgain': for event in pygame.event.get(): if event.type==pygame.QUIT: closeGame() key=pygame.key.get_pressed() if key[pygame.K_ESCAPE]: bleep.play() closeGame() if key[pygame.K_y]: bleep.play() win=True x=screenWidth/2 y=screenHeight/2 length=0 move="down" path=[] 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() fpsClock.tick(fps)
And yeah, having a quit() function may be kinda silly, but it feels a lot more elegant than just letting the game crash (which is what mine used to do when you lost).