def move(self, food_pos): global fruit 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: fruit = random.choice(Costants.FRUITS) fruitname, fruit = fruit text = write(f"You ate : {fruitname}", 200, 0) 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 move(self, food_pos): 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: # 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 menu(): "This is the menu that waits you to click the s key to start" global xs, ys, game, fruit, fruitname, base # this will game.maxscore = 10... for example game = Score("score.txt") xs = 5 base = pygame.mixer.music base.load("sounds/base.ogg") base.play(-1) pygame.display.set_caption("Python Snake v. 1.8.8") Costants.window.blit( write("PYTHON SNAKE 2020 - MADE WITH PYGAME", middle=1), (0, 30)) Costants.window.blit(write("Press s to start", middle=1), (0, 60)) Costants.window.blit(write("Max - Score " + str(game.maxscore), middle=1), (0, 90)) # Costants.window.blit(write("Press m to start / stop the music", 0, 0), (0, 90)) Costants.window.blit( write("Use the arrow keys to move the snake in the game", 0, 0), (0, 310)) Costants.window.blit(write("Move the mouse to hear some music", 0, 0), (0, 330)) Costants.window.blit(write("and make the snake move in the menu", 0, 0), (0, 350)) Costants.window.blit(write("Music is experimental", 0, 0), (0, 380)) pygame.draw.line(Costants.window, (255, 255, 255), (0, 25), (400, 25), 2) pygame.draw.line(Costants.window, (255, 255, 255), (0, 110), (400, 110), 2) big(Costants.window) loop1 = 1 while loop1: show_snake() random_play() event = pygame.event.wait() if (event.type == pygame.QUIT): break if event.type == pygame.KEYDOWN: press_escape = event.key == pygame.K_ESCAPE if press_escape: break restart = (event.key == pygame.K_s) if restart: Costants.score = 0 Costants.GAME_SPEED = 8 Costants.window.fill((0, 0, 0)) snake.start() fruit = random.choice(Costants.FRUITS) fruitname, fruit = fruit start() pygame.display.update() Costants.clock.tick(Costants.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 menu(): "This is the menu that waits you to click the s key to start" global GAME_SPEED, score, xs, ys, game game = Score("score.txt") xs = 5 pygame.display.set_caption("Python Snake v. 1.8.8") window.blit(write("PYTHON SNAKE 2020 - MADE WITH PYGAME", middle=1), (0, 30)) window.blit(write("Press s to start", middle=1), (0, 60)) window.blit(write("Max - Score " + str(game.maxscore), middle=1), (0, 90)) # window.blit(write("Press m to start / stop the music", 0, 0), (0, 90)) window.blit( write("Use the arrow keys to move the snake in the game", 0, 0), (0, 310)) window.blit(write("Move the mouse to hear some music", 0, 0), (0, 330)) window.blit(write("and make the snake move in the menu", 0, 0), (0, 350)) window.blit(write("Music is experimental", 0, 0), (0, 380)) pygame.draw.line(window, (255, 255, 255), (0, 25), (400, 25), 2) pygame.draw.line(window, (255, 255, 255), (0, 110), (400, 110), 2) big(window) loop1 = 1 while loop1: show_snake() random_play() event = pygame.event.wait() if (event.type == pygame.QUIT): break if event.type == pygame.KEYDOWN: press_escape = event.key == pygame.K_ESCAPE if press_escape: break restart = (event.key == pygame.K_s) if restart: score = 0 GAME_SPEED = 8 window.fill((0, 0, 0)) snake.start() start() pygame.display.update() clock.tick(GAME_SPEED) pygame.quit()