Aug-13-2018, 05:14 AM
So with some help I wrote this simple Snake game in Python using Pygame. It works in it's current state with no errors that I could find. I was wondering if anyone had some ideas to refine the code or any ideas to make the game better, I'd love to hear them.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
import pygame import sys import random import time pygame.init() class Snake(): def __init__( self ): self .position = [ 100 , 50 ] self .body = [[ 100 , 50 ],[ 90 , 50 ],[ 80 , 50 ]] self .direction = "RIGHT" def changeDirTo( self , dir ): if dir = = "RIGHT" and not self .direction = = "LEFT" : self .direction = "RIGHT" elif dir = = "LEFT" and not self .direction = = "RIGHT" : self .direction = "LEFT" elif dir = = "UP" and not self .direction = = "DOWN" : self .direction = "UP" elif dir = = "DOWN" and not self .direction = = "UP" : self .direction = "DOWN" def move( self ,foodPos): if self .direction = = "RIGHT" : self .position[ 0 ] = self .position[ 0 ] + 10 elif self .direction = = "LEFT" : self .position[ 0 ] = self .position[ 0 ] - 10 elif self .direction = = "UP" : self .position[ 1 ] = self .position[ 1 ] - 10 elif self .direction = = "DOWN" : self .position[ 1 ] = self .position[ 1 ] + 10 self .body.insert( 0 , list ( self .position)) if self .position = = foodPos: return 1 else : self .body.pop() return 0 def move_Right( self ): self .position[ 0 ] = self .position[ 0 ] + 10 def move_Left( self ): self .position[ 0 ] = self .position[ 0 ] - 10 def move_Up( self ): self .position[ 0 ] = self .position[ 1 ] - 10 def move_Down( self ): self .position[ 0 ] = self .position[ 1 ] + 10 def checkCollision( self ): if self .position[ 0 ] > 490 or self .position[ 0 ] < 10 : return 1 elif self .position[ 1 ] > 500 or self .position[ 1 ] < 10 : return 1 for bodyPart in self .body[ 1 :]: if self .position = = bodyPart: return 1 return 0 def getHeadPosition( self ): return self .position def getBody( self ): return self .body class FoodSpawn(): def __init__( self ): self .position = [random.randint( 4 , 46 ) * 10 ,random.randint( 4 , 46 ) * 10 ] self .isFoodOnScreen = True def spawnFood( self ): if self .isFoodOnScreen = = False : self .position = [random.randrange( 4 , 46 ) * 10 ,random.randrange( 4 , 46 ) * 10 ] self .isFoodOnScreen = True return self .position def setFoodOnScreen( self ,b): self .isFoodOnScreen = b window = pygame.display.set_mode(( 500 + 20 , 500 + 20 )) pygame.display.set_caption( "Snake Game" ) fps = pygame.time.Clock() score = 0 snake = Snake() foodSpawner = FoodSpawn() def gameOver(): font = pygame.font.SysFont( 'Candara' , 30 ) score_text = font.render( "Congrats you got " + str (score) + " points!" , 4 ,( 255 , 0 , 0 )) window.blit(score_text,( 100 , 250 )) pygame.display.flip() time.sleep( 3 ) pygame.quit() sys.exit() while True : for event in pygame.event.get(): if event. type = = pygame.QUIT: gameOver() pressed = pygame.key.get_pressed() if pressed[pygame.K_RIGHT]: snake.changeDirTo( 'RIGHT' ) elif pressed[pygame.K_LEFT]: snake.changeDirTo( 'LEFT' ) elif pressed[pygame.K_UP]: snake.changeDirTo( 'UP' ) elif pressed[pygame.K_DOWN]: snake.changeDirTo( 'DOWN' ) elif pressed[pygame.K_ESCAPE]: gameOver() foodPos = foodSpawner.spawnFood() if (snake.move(foodPos) = = 1 ): score + = 1 foodSpawner.setFoodOnScreen( False ) window.fill(pygame.Color( 225 , 225 , 225 )) for x in range ( 0 , 510 , 10 ): pygame.draw.rect(window, ( 0 , 0 , 225 ), [x, 0 , 10 , 10 ]) pygame.draw.rect(window, ( 0 , 0 , 225 ), [x, 510 , 10 , 10 ]) for x in range ( 0 , 510 , 10 ): pygame.draw.rect(window, ( 0 , 0 , 225 ), [ 0 , x, 10 , 10 ]) pygame.draw.rect(window, ( 0 , 0 , 225 ), [ 510 , x, 10 , 10 ]) for pos in snake.getBody(): pygame.draw.rect(window,pygame.Color( 0 , 225 , 0 ),pygame.Rect(pos[ 0 ],pos[ 1 ], 10 , 10 )) pygame.draw.rect(window,pygame.Color( 225 , 0 , 0 ),pygame.Rect(foodPos[ 0 ],foodPos[ 1 ], 10 , 10 )) if (snake.checkCollision() = = 1 ): gameOver() pygame.display.set_caption( "Snake | Score: " + str (score)) pygame.display.flip() fps.tick( 20 ) pygame.quit() |