def move(self, food_pos): global fruit, fruitname directions = {"RIGHT": 1, "LEFT": -1, "UP": -1, "DOWN": 1} if self.moves_towards in "RIGHT LEFT": self.x += directions[self.moves_towards] else: self.y += directions[self.moves_towards] self.body.insert(0, [self.x, self.y]) if [self.x, self.y] == food_pos: random_play() text = write(f"You ate : {fruitname}", 200, 0) if fruitname == "banana": self.body.pop() self.body.pop() play() fruit = random.choice(Costants.FRUITS) fruitname, fruit = fruit text_rect = text.get_rect(center=((Costants.w2, 10))) Costants.window.blit(Costants.clean, text_rect) Costants.window.blit(text, text_rect) # not popping the last element, it grows in size return 1 else: "If do not eat... same size" self.body.pop() # A random not at a random time and random volume return 0 if Costants.music: print("music is on") random_play(rnd=random.randrange(3, 10))
def start(): "Once you press the 's' key it runs, moves the snake a wait the user input" global loop, base # pygame.mixer.music.unload() #base.unload() go = "RIGHT" food_pos = [ random.randrange(1, Costants.BOARD_SIZE), random.randrange(1, Costants.BOARD_SIZE) ] loop = 1 while loop: for event in pygame.event.get(): if event.type == pygame.QUIT: loop = 0 elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: loop = 0 # You cannot move backwards elif event.key == pygame.K_RIGHT: go = "RIGHT" elif event.key == pygame.K_UP: go = "UP" elif event.key == pygame.K_DOWN: go = "DOWN" elif event.key == pygame.K_LEFT: go = "LEFT" if event.key == pygame.K_m: Costants.music = 1 if event.key == pygame.K_s: save_image(Costants.window) snake.not_backwards(go) # Moves and check if eats if snake.move(food_pos): play("click") Costants.score += 1 if Costants.score > int(game.maxscore): game.maxscore = str(Costants.score) #game.save_score(Costants.score) Costants.GAME_SPEED += .5 food_pos = [ random.randrange(1, Costants.BOARD_SIZE), random.randrange(1, Costants.BOARD_SIZE) ] # Draw everything on blit_all(food_pos) if snake.check_collisions() == 1: loop = 0 Costants.window.fill((0, 0, 0)) menu() pygame.display.update() Costants.clock.tick(Costants.GAME_SPEED) pygame.quit()
def start(): "Once you press the 's' key it runs, moves the snake a wait the user input" global GAME_SPEED, score, loop, music go = "RIGHT" food_pos = [ random.randrange(1, BOARD_SIZE), random.randrange(1, BOARD_SIZE) ] loop = 1 while loop: for event in pygame.event.get(): if event.type == pygame.QUIT: loop = 0 elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: loop = 0 # You cannot move backwards elif event.key == pygame.K_RIGHT: go = "RIGHT" elif event.key == pygame.K_UP: go = "UP" elif event.key == pygame.K_DOWN: go = "DOWN" elif event.key == pygame.K_LEFT: go = "LEFT" if event.key == pygame.K_m: music = 1 if event.key == pygame.K_s: save_image(window) snake.not_backwards(go) # Moves and check if eats if snake.move(food_pos): play("click") score += 1 GAME_SPEED += 1 food_pos = [ random.randrange(1, BOARD_SIZE), random.randrange(1, BOARD_SIZE) ] # Draw everything on blit_all(food_pos) if snake.check_collisions() == 1: loop = 0 window.fill((0, 0, 0)) menu() pygame.display.update() clock.tick(GAME_SPEED) pygame.quit()
def move(self, food_pos): global fruit, fruitname, base directions = {"RIGHT": 1, "LEFT": -1, "UP": -1, "DOWN": 1} if self.moves_towards in "RIGHT LEFT": self.x += directions[self.moves_towards] else: self.y += directions[self.moves_towards] self.body.insert(0, [self.x, self.y]) if [self.x, self.y] == food_pos: #base.stop() random_play() # Write what you eat if fruitname == "banana5": Costants.window.fill((0, 0, 0)) fruitname = "banana: go slow!" play("banana") if len(self.body) > 3: self.body = self.body[:-2] if Costants.GAME_SPEED > 8: Costants.GAME_SPEED -= 1 text = write(f"You ate : {fruitname}", 300, 0) text_rect = text.get_rect(center=((Costants.w2, 10))) Costants.window.blit(Costants.clean, text_rect) Costants.window.blit(text, text_rect) if fruitname == "cherry": Costants.window.fill((0, 0, 0)) self.body = self.body[:3] play("banana5") fruit = random.choice(Costants.FRUITS) fruitname, fruit = fruit # not popping the last element, it grows in size return 1 else: "If do not eat... same size" self.body.pop() # A random not at a random time and random volume return 0 if Costants.music: print("music is on") #base.unload() random_play(rnd=random.randrange(3, 10))
def gameover(): def surface(redux): main = pygame.Surface((Costants.w - redux, Costants.h - redux)) if (redux // 20) % 2 == 0: main.fill((0, 0, 0)) else: main.fill((64, 64, 64)) Costants.window.blit(main, (redux, redux)) Costants.screen.blit( pygame.transform.scale(main, ((Costants.w - redux) * 2, (Costants.h - redux) * 2)), (redux, redux)) # pygame.time.delay(5) pygame.display.update() Costants.clock.tick(20) play("over") for x in range(20): surface(x * 20)