Every line of 'python snake game' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.
119 def run_game(): 120 # this function runs the game 121 snake = Snake() 122 apple = Apple() 123 while True: 124 for event in pygame.event.get(): 125 if event.type == pygame.QUIT: 126 pygame.quit() 127 sys.exit() 128 elif event.type == pygame.KEYDOWN: 129 if event.key == pygame.K_ESCAPE: 130 pygame.quit() 131 sys.exit() 132 # don't move in the opposite direction 133 elif event.key == pygame.K_LEFT and snake.dir != 'r': 134 snake.dir = 'l' 135 elif event.key == pygame.K_RIGHT and snake.dir != 'l': 136 snake.dir = 'r' 137 elif event.key == pygame.K_UP and snake.dir != 'd': 138 snake.dir = 'u' 139 elif event.key == pygame.K_DOWN and snake.dir != 'u': 140 snake.dir = 'd' 141 142 if snake.eat(apple): 143 # make a new apple 144 apple = Apple() 145 else: 146 # remove the last segment from the snake (it stayed the same size) 147 del snake.coords[-1] 148 149 snake.move() 150 if snake.hit(): 151 # dead - play the hit sound and return the score 152 snake.hit_snd.play() 153 return len(snake.coords) - 3 154 155 # Update screen 156 screen.fill(BGCOLOR) 157 draw_grid() 158 draw_score(len(snake.coords) - 3) 159 snake.draw() 160 apple.draw() 161 pygame.display.flip() 162 clock.tick(FPS)
110 def start_game(): 111 global s 112 create_block() 113 s = create_snake() 114 # Reaction on keypress 115 c.bind("", s.change_direction) 116 main()
172 def restart(self,event): 173 self.canvas.delete("food","snake","text") 174 self.canvas.unbind('') 175 176 # to initialize the snake in the range of (191,191,341,341) 177 r=random.randrange(191,191+15*10,self.step) 178 self.snakeX=[r,r+self.step,r+self.step*2] 179 self.snakeY=[r,r,r] 180 181 # to initialize the moving direction 182 self.snakeDirection = 'left' 183 self.snakeMove = [-1,0] 184 185 # reset the score to zero 186 self.gamescore=-10 187 self.draw_score() 188 189 # to initialize the game (food and snake) 190 self.draw_food() 191 self.draw_snake() 192 193 # to play the game 194 self.play()
105 def add_apple(self): 106 self.apples.add(self.emptygrid())
56 def draw(self, scr): 57 scr.draw.set(self.pos) 58 if self.remove: 59 for pos in self.remove: 60 scr.draw.reset(pos) 61 self.remove[:] = []
78 def gameOver(self): 79 self._gameOver = True 80 self._gameOverCount = 0 81 self._levelUp = True 82 self._level = 1 83 self.setSpeed("move", 4) 84 self._lives = 4
125 def down(self): 126 self.play(3)
99 def move(self): 100 """ 101 移动snake身体 102 """ 103 if self.direction == Direction.UP: 104 self.snake_body[0] = (self.snake_body[0][0], self.snake_body[0][1] - self.cube_width) 105 106 elif self.direction == Direction.DOWN: 107 self.snake_body[0] = (self.snake_body[0][0], self.snake_body[0][1] + self.cube_width) 108 109 # top += cube_width 110 elif self.direction == Direction.LEFT: 111 112 self.snake_body[0] = (self.snake_body[0][0] - self.cube_width, self.snake_body[0][1]) 113 114 # left -= cube_width 115 elif self.direction == Direction.RIGHT: 116 self.snake_body[0] = (self.snake_body[0][0] + self.cube_width, self.snake_body[0][1])
66 def draw(self): 67 self.shape.draw()