def main(): pygame.init() clock = pygame.time.Clock() screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), 0, 32) surface = pygame.Surface(screen.get_size()) surface = surface.convert() snake = Critter(0) food = Food(0) draw_grid(surface) myfont = pygame.font.SysFont("monospace", 16) score = 0 while True: clock.tick(FRAME_RATE) snake.user_input() draw_grid(surface) result = snake.move() if result == -1: score = 0 if snake.get_head_position() == food.position: snake.length += 1 score += 1 food.randomize_position() food.randomize_color() snake.draw(surface) food.draw(surface) screen.blit(surface, (0, 0)) text = myfont.render(f"Score: {score}", 1, (0, 0, 0)) screen.blit(text, (5, 10)) pygame.display.update()
class Game(): """ """ def __init__(self, win_res, gridsize, fps_max): pygame.init() self.gridsize = gridsize self.clock = pygame.time.Clock() self.window = Window(win_res, self.gridsize) self.snake = Snake(self) self.food = Food(self.window) self.fps_max = fps_max def run_game(self): while True: # Program body for event in pygame.event.get(): if event.type == pygame.QUIT: print("Fermeture du jeu, Goodbye!") pygame.quit() sys.exit() else: if event.type == pygame.KEYDOWN: self.snake.keys_handling(event.key) self.window.screen.blit(self.window.checkerboard, (0, 0)) self.snake.move() self.snake.draw() self.snake.eat() self.food.add_color() self.food.draw() pygame.display.update() self.clock.tick(self.fps_max)
def main(): screen = pg.display.set_mode(dimension) screen.fill((0, 0, 0)) pg.display.set_caption("Snake - IA") # Game Variables amount_of_squares = (DIM["x"] / square_dimension, DIM["y"] / square_dimension) my_grid = Grid(dimension, square_dimension) snake = Snake(amount_of_squares, my_grid) # Main player - snake of Segments() pg.time.set_timer(UPDATE_EVENT, 100) food = Food(my_grid) keep_adding = True game_clock = pg.time.Clock() # Game loop while True: # FPS - Management game_clock.tick(40) # Key key = pg.key.get_pressed() temp_direction = snake.segments[0].direction if key[pg.K_UP] and temp_direction[1] != 1: snake.move(np.array([0, -1])) elif key[pg.K_DOWN] and temp_direction[1] != -1: snake.move(np.array([0, 1])) elif key[pg.K_LEFT] and temp_direction[0] != 1: snake.move(np.array([-1, 0])) elif key[pg.K_RIGHT] and temp_direction[0] != -1: snake.move(np.array([1, 0])) elif key[pg.K_SPACE] and keep_adding: snake.add_segment() keep_adding = False ''' Events handler ''' ##screen.fill((0,0,0)) for event in pg.event.get(): if event.type == UPDATE_EVENT: snake.update(my_grid) if event.type == QUIT: print('Quitting game...') sys.exit(0) ''' Game Functions ''' snake.display(my_grid) food.draw() food.update(snake) my_grid.display(screen) pg.display.flip() return 0
def main(): pygame.init() clock = pygame.time.Clock() screen = pygame.display.set_mode((screen_width, screen_height), 0, 32) surface = pygame.Surface(screen.get_size()) surface = surface.convert() drawGrid(surface) snake = Snake() food = Food() myfont = pygame.font.SysFont("impact", 16) run = True #main game loop that runs until the snake dies while (run): clock.tick(10) snake.movement() drawGrid(surface) snake.move() if snake.headPos() == food.pos: snake.lengthBody += 1 snake.gameScore += 1 food.randomPos() snake.draw(surface) food.draw(surface) screen.blit(surface, (0, 0)) textScore = myfont.render("Score {0}".format(snake.gameScore), 1, (0, 0, 0)) screen.blit(textScore, (5, 10)) if snake.death == True: myfont = pygame.font.SysFont("impact", 28) run = False screen.fill((0, 0, 0)) textDead = myfont.render( "Your Final Score is {0}".format(snake.gameScore), 1, (255, 255, 255)) screen.blit(textDead, ((screen_width // 2) - 200, (screen_height // 2) - 50)) textRestart = myfont.render("Press Spacebar to Restart!", 1, (255, 255, 255)) screen.blit(textRestart, ((screen_width // 2) - 210, (screen_height // 2) - 25)) pygame.display.update() while run == False: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: snake.reset() run = True main() pygame.display.update()
class Game: def __init__(self, width: int, height: int, stdscr): curses.curs_set(0) stdscr.clear() stdscr.nodelay(True) self.__stdscr = stdscr self.__width = width self.__height = height self.__frame = Frame(width, height, "*", stdscr) self.__food = Food(width // 4, height // 4, "$", stdscr, 1, 1, width - 1, height - 1) self.__snake = Snake(width // 2, height // 2, "+", stdscr) self.__total_time = 0 self.__draw_time_buffer = 0 def clear(self) -> None: self.__frame = Frame(self.__width, self.__height, "*", self.__stdscr) self.__food = Food(self.__width // 4, self.__height // 4, "$", self.__stdscr, 1, 1, self.__width - 1, self.__height - 1) self.__snake = Snake(self.__width // 2, self.__height // 2, "+", self.__stdscr) def draw(self, time_delta: int) -> None: self.__draw_time_buffer += time_delta if self.__draw_time_buffer > 30000: self.__draw_time_buffer -= 30000 self.__stdscr.clear() self.__frame.draw() self.__food.draw() self.__snake.draw() self.__stdscr.refresh() def logic(self, time_delta: int) -> None: self.__total_time += time_delta c = int(self.__stdscr.getch()) self.__snake.turn(c) if self.__total_time > 200000: self.__total_time -= 200000 if self.__snake.is_hit_self() or self.__frame.is_hit( self.__snake.head): self.clear() if self.__snake.head.is_hit(self.__food): self.__snake.grow() self.__food.generate() self.__snake.move()
def main(): pygame.init() clock = pygame.time.Clock() logo = pygame.image.load("durp.jpg") pygame.display.set_icon(logo) pygame.display.set_caption("Snake") screen = pygame.display.set_mode((screen_width, screen_height), pygame.RESIZABLE) surface = pygame.Surface(screen.get_size()) surface = surface.convert() draw_grid(surface) snake = Snake() food = Food() myfont = pygame.font.SysFont("monospace", 16) running = True score = 0 while running: print(snake.positions) clock.tick(fps) snake.keys() draw_grid(surface) snake.move() if snake.get_head_position() == food.position: snake.length += 1 score += 1 food.randomise_position() snake.draw(surface) food.draw(surface) screen.blit(surface, (0, 0)) text = myfont.render("Score {0}".format(score), 1, (0, 0, 0)) pygame.display.update() for event in pygame.event.get(): if event.type == pygame.QUIT: running = False
def game_loop(): snake = Snake() food = Food() grid = Grid() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() game_display.fill(WHITE) food.draw(game_display) snake.draw(game_display) snake.move(grid, food, game_display) pygame.display.update() clock.tick(FPS)
def game(): snake = Snake(snake_color) food = Food() food.set_position(snake) food.draw(window) clock = pygame.time.Clock() while True: snake.move_body() if snake.head.position == food.position: snake.grow() food.set_position(snake) food.draw(window) if snake.head.position in [elem.position for elem in snake.body[1:]]: display_message_box(snake.points) snake.restart() food.set_position(snake) food.draw(window) pygame.time.delay(50) clock.tick(10) reload_window(snake, food)
class FoodTests(unittest.TestCase): def setUp(self): self.food = Food(Vector(0, 0), Colors.CHOCOLATE) def tearDown(self): del self.food def test_instantiation(self): self.assertEqual(self.food.color, Colors.CHOCOLATE) self.assertEqual(self.food.position, Vector(0, 0)) def test_draw(self): game = FakeGame() self.food.draw(game) num_draw_calls = len(game.draw_calls) self.assertEqual(num_draw_calls, 1, 'Number of draw calls!') draw_call = game.draw_calls[0] self.assertEqual(draw_call['method'], 'draw_cell') draw_call_args = draw_call['args'] self.assertEqual(draw_call_args, (0, 0, Colors.CHOCOLATE))
food = Food(block_size,(window.get_width(), window.get_height())) run = True while run: pygame.time.delay(100) for event in pygame.event.get(): if event.type == pygame.QUIT: run = False keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: snek.steer(Direction.LEFT) elif keys[pygame.K_RIGHT]: snek.steer(Direction.RIGHT) elif keys[pygame.K_UP]: snek.steer(Direction.UP) elif keys[pygame.K_DOWN]: snek.steer(Direction.DOWN) elif keys[pygame.K_SPACE]: snek.eat() snek.move() snek.check_for_food(food) window.fill((0,0,0)) snek.draw(pygame, window) food.draw(pygame, window) pygame.display.update() # end while loop pygame.quit()
class Game: # session class def __init__(self): # session initialisation pg.init() pg.font.init() self.font = pg.font.SysFont('Arial', 16) self.screen = pg.display.set_mode((TOTAL_WIDTH, GRID_HEIGHT)) pg.display.set_caption(TITLE) self.clock = pg.time.Clock() self.fps = FPS self.render = True def new_simulation(self): # start a new simulation session self.genetic_algorithm = GeneticAlgorithm(self) while True: self.genetic_algorithm.cur_snake = 0 for network in self.genetic_algorithm.population: self.new_game(network) self.genetic_algorithm.cur_snake += 1 self.genetic_algorithm.selection() self.genetic_algorithm.reproduction() self.genetic_algorithm.mutation() self.genetic_algorithm.current_generation += 1 print('Generation: ', self.genetic_algorithm.current_generation) def new_game(self, neural_network): # start a new game of snake with the specified neural network self.snake = Snake(self, neural_network) self.food = Food(self) self.run() def run(self): # game loop while self.snake.alive: self.events() self.update() if self.render: self.draw() self.clock.tick(self.fps) / 1000 def update(self): # game loop update self.snake.update() self.food.update() def events(self): # check for user interaction events for event in pg.event.get(): if event.type == pg.QUIT: pg.quit() sys.exit() elif event.type == pg.KEYDOWN: if event.key == pg.K_UP: self.render = True self.fps *= 2 elif event.key == pg.K_DOWN: self.render = True self.fps = int(self.fps / 2) if self.fps < 1: self.fps = 1 elif self.fps > 128: self.render = False self.fps = 128 def draw(self): # draw on screen elements self.screen.fill(BLACK) self.draw_grid() self.snake.draw() self.food.draw() genstring = 'Generation: ' + str( self.genetic_algorithm.current_generation) gentext = self.font.render(genstring, False, WHITE) self.screen.blit(gentext, (TOTAL_WIDTH - gentext.get_width(), (GRID_HEIGHT - 3 * gentext.get_height()))) snakestring = 'Snake: ' + str(self.genetic_algorithm.cur_snake) snaketext = self.font.render(snakestring, False, WHITE) self.screen.blit(snaketext, (TOTAL_WIDTH - snaketext.get_width(), (GRID_HEIGHT - 2 * snaketext.get_height()))) scorestring = 'Highscore: ' + str(self.genetic_algorithm.highscore) scoretext = self.font.render(scorestring, False, WHITE) self.screen.blit(scoretext, (TOTAL_WIDTH - scoretext.get_width(), (GRID_HEIGHT - 1 * scoretext.get_height()))) pg.display.flip() def draw_grid(self): # draw map tile grid for x in range(0, GRID_WIDTH + 1, TILESIZE): pg.draw.line(self.screen, GREY, (x, 0), (x, GRID_HEIGHT)) for y in range(0, GRID_HEIGHT, TILESIZE): pg.draw.line(self.screen, GREY, (0, y), (GRID_WIDTH, y))
class Environment: def __init__(self, wrap=False, grid_size=10, local_size=9, rate=100, max_time=math.inf, action_space=5, food_count=1, stick_count=1, obstacle_count=0, lava_count=0, zombie_count=0, history=30, map_path=None): """ Initialise the Game Environment with default values """ #self.FPS = 120 # NOT USED YET self.UPDATE_RATE = rate self.SCALE = 20 # Scale of each block 20x20 pixels self.GRID_SIZE = grid_size self.LOCAL_GRID_SIZE = local_size # Has to be an odd number self.ENABLE_WRAP = wrap if not self.ENABLE_WRAP: self.GRID_SIZE += 2 self.ACTION_SPACE = action_space self.MAP_PATH = map_path self.DISPLAY_WIDTH = self.GRID_SIZE * self.SCALE self.DISPLAY_HEIGHT = self.GRID_SIZE * self.SCALE # Maximum timesteps before an episode is stopped self.MAX_TIME_PER_EPISODE = max_time # Create and Initialise steve self.steve = Steve(history) # Create Food self.NUM_OF_FOOD = food_count self.food = Food(self.NUM_OF_FOOD) self.NUM_OF_STICKS = stick_count self.stick = Stick(self.NUM_OF_STICKS) # Create Obstacles self.NUM_OF_OBSTACLES = obstacle_count self.obstacle = Obstacle(self.NUM_OF_OBSTACLES) # Create Lava self.NUM_OF_LAVA = lava_count self.lava = Lava(self.NUM_OF_LAVA) # Create Zombies self.NUM_OF_ZOMBIES = zombie_count self.zombie = Zombie(self.NUM_OF_ZOMBIES) self.score = 0 self.time = 0 self.state = None self.display = None self.bg = None self.clock = None self.font = None self.steps = 0 self.spawn_new_food = False self.last_eaten_food = -1 # Used putting food and obstacles on the grid self.grid = [] for j in range(self.GRID_SIZE): for i in range(self.GRID_SIZE): self.grid.append((i * self.SCALE, j * self.SCALE)) self.maze = [] # created in reset() def prerender(self): """ If you want to render the game to the screen, you will have to prerender Load textures / images """ pygame.init() pygame.key.set_repeat(1, 1) self.display = pygame.display.set_mode( (self.DISPLAY_WIDTH, self.DISPLAY_HEIGHT)) pygame.display.set_caption('Malmo Simulation') self.clock = pygame.time.Clock() pygame.font.init() self.font = pygame.font.SysFont('Default', 32, bold=False) # Creates a visual steve self.steve.create(pygame, self.SCALE) # Creates visual Food self.food.create(pygame, self.SCALE) # Creates visual Food self.stick.create(pygame, self.SCALE) # Creates visual Obstacles self.obstacle.create(pygame, self.SCALE) # Creates visual Lava self.lava.create(pygame, self.SCALE) # Creates visual Multipliers self.zombie.create(pygame, self.SCALE) # Creates the grid background self.bg = pygame.image.load("./Images/Grid100.png").convert() self.bg = pygame.transform.scale(self.bg, (self.SCALE * 100, self.SCALE * 100)) def reset(self): """Reset the environment""" self.steps = 0 # Reset the score to 0 self.score = 0 self.steve.health = 3 # Positions on the grid that are not allowed to spawn things disallowed = [] # Create obstacles and lava in the environment self.obstacle.array.clear() self.obstacle.array_length = 0 self.lava.array.clear() self.lava.array_length = 0 if self.MAP_PATH != None: self.obstacle.reset_map(self.GRID_SIZE, self.MAP_PATH, self.ENABLE_WRAP) self.lava.reset_map(self.GRID_SIZE, self.MAP_PATH, self.ENABLE_WRAP) if not self.ENABLE_WRAP: self.obstacle.create_border(self.GRID_SIZE, self.SCALE) # Add all obstacle positions to the disallowed list [disallowed.append(grid_pos) for grid_pos in self.obstacle.array] [disallowed.append(grid_pos) for grid_pos in self.lava.array] # Add random obstacles not already on the map self.obstacle.reset(self.grid, disallowed) # Add all obstacle positions to the disallowed list disallowed = [] [disallowed.append(grid_pos) for grid_pos in self.obstacle.array] [disallowed.append(grid_pos) for grid_pos in self.lava.array] # Create lava in the environment # self.lava.array.clear() # self.lava.array_length = 0 # if self.MAP_PATH != None: # self.lava.reset_map(self.GRID_SIZE, self.MAP_PATH, self.ENABLE_WRAP) # [disallowed.append(grid_pos) for grid_pos in self.lava.array] # Add random lava not already on the map self.lava.reset(self.grid, disallowed) disallowed = [] [disallowed.append(grid_pos) for grid_pos in self.obstacle.array] [disallowed.append(grid_pos) for grid_pos in self.lava.array] self.maze = createGrid(self.GRID_SIZE, disallowed, self.SCALE) # print(self.obstacle.array) # print("\n") # print(self.maze) # Reseting Steve at a random (or specific) position self.steve.reset(self.grid, disallowed) # Initialise the movement to not moving if self.ACTION_SPACE == 5: self.steve.dx = 0 self.steve.dy = 0 disallowed.append(self.steve.pos) # Create a piece of food self.food.reset(self.NUM_OF_FOOD, self.grid, disallowed) # self.food.make_within_range(self.GRID_SIZE, self.SCALE, self.steve) self.spawn_new_food = False [disallowed.append(grid_pos) for grid_pos in self.food.array] # Spawn in a zombie self.zombie.reset(self.grid, disallowed) # Fill the state array with the appropriate state representation # self.state = self.state_array() # self.state = self.state_vector_3D() self.state = self.local_state_vector_3D() # Reset the time self.time = 0 # A dictionary of information that can be useful info = {"time": self.time, "score": self.score} return self.state, info def quick_reset(self): """Reset the environment and places all entities according to the map layout""" self.steps = 0 # Reset the score to 0 self.score = 0 self.steve.health = 5 # Positions on the grid that are not allowed to spawn things disallowed = [] # Create obstacles and lava in the environment self.obstacle.array.clear() self.obstacle.array_length = 0 self.lava.array.clear() self.lava.array_length = 0 self.zombie.array.clear() self.zombie.amount = 0 self.steve.pos, self.food.array, self.stick.array, self.zombie.array, barrier = reset_impossible_map( self.GRID_SIZE, self.MAP_PATH) self.zombie.prev_array = self.zombie.array # self.steve.pos = (0,0) self.steve.x = self.steve.pos[0][0] self.steve.y = self.steve.pos[0][1] # quick fix for something silly self.steve.pos[0] = self.steve.x self.steve.pos.append(self.steve.y) self.steve.history.clear() self.steve.history.append((self.steve.x, self.steve.y)) self.steve.hasSword = False self.obstacle.reset_map(self.GRID_SIZE, self.MAP_PATH, self.ENABLE_WRAP) self.lava.reset_map(self.GRID_SIZE, self.MAP_PATH, self.ENABLE_WRAP) self.obstacle.create_border(self.GRID_SIZE, self.SCALE) [disallowed.append(grid_pos) for grid_pos in self.obstacle.array] [disallowed.append(grid_pos) for grid_pos in self.lava.array] [disallowed.append(grid_pos) for grid_pos in barrier] self.maze = createGrid(self.GRID_SIZE, disallowed, self.SCALE) # Initialise the movement to not moving if self.ACTION_SPACE == 5: self.steve.dx = 0 self.steve.dy = 0 self.spawn_new_food = False self.zombie.amount = len(self.zombie.array) self.food.amount = len(self.food.array) self.stick.amount = len(self.stick.array) # Fill the state array with the appropriate state representation # self.state = self.state_array() # self.state = self.state_vector_3D() self.state = self.local_state_vector_3D() # Reset the time self.time = 0 # A dictionary of information that can be useful info = {"time": self.time, "score": self.score} return self.state, info def render(self): """Renders ONLY the CURRENT state""" # Mainly to close the window and stop program when it's running action = self.controls() # Set the window caption to the score # pygame.display.set_caption("Score: " + str(self.score)) # Draw the background, steve and the food self.display.blit(self.bg, (0, 0)) self.obstacle.draw(self.display) self.lava.draw(self.display) self.food.draw(self.display) self.stick.draw(self.display) self.zombie.draw(self.display) self.steve.draw(self.display) self.steve.draw_health(self.display) # Text on screen text = self.font.render("Score: " + str(int(self.score)), True, (240, 240, 240, 0)) self.display.blit(text, (10, 0)) # text = self.font.render("Multiplier: "+str(self.steve.score_multiplier)+"x", True, (240, 240, 240, 0)) # self.display.blit(text,(150,0)) # Update the pygame display pygame.display.update() # print(pygame.display.get_surface().get_size()) # pygame.display.get_surface().lock() # p = pygame.PixelArray(pygame.display.get_surface()) # p = pygame.surfarray.array3d(pygame.display.get_surface()) # print("PIXELS:", p.shape) # pygame.display.get_surface().unlock() # print(clock.get_rawtime()) # clock.tick(self.FPS) # FPS setting # Adds a delay to the the game when rendering so that humans can watch pygame.time.delay(self.UPDATE_RATE) return action def end(self): """ Ending the Game - This has to be at the end of the code Clean way to end the pygame env with a few backups... """ pygame.quit() quit() sys.exit(0) # safe backup def wrap(self): """ If the steve goes out the screen bounds, wrap it around""" if self.steve.x > self.DISPLAY_WIDTH - self.SCALE: self.steve.x = 0 if self.steve.x < 0: self.steve.x = self.DISPLAY_WIDTH - self.SCALE if self.steve.y > self.DISPLAY_HEIGHT - self.SCALE: self.steve.y = 0 if self.steve.y < 0: self.steve.y = self.DISPLAY_HEIGHT - self.SCALE self.steve.pos = (self.steve.x, self.steve.y) def step(self, action): """ Step through the game, one state at a time. Return the reward, the new_state, whether its reached_diamond or not, and the time """ self.steps += 1 # Rewards: # reward_each_time_step = 1.0 reward_each_time_step = -0.1 reward_collecting_diamond = 10.0 reward_out_of_bounds = -1.0 # not used reward_zombie_hit = -10.0 reward_in_lava = -10.0 reward_barrier = -2.0 # Increment time step self.time += 1 # If the steve has reached the food reached_diamond = False hit_obstacle = False in_lava = False # If the episode is finished - after a certain amount of timesteps or it crashed done = False # Initialze to -1 for every time step - to find the fastest route (can be a more negative reward) reward = reward_each_time_step # Negetive exponential distance rewards # if len(self.zombie.array) > 0: # distance_list = [] # for i, z in enumerate(self.zombie.array): # distance_list.append((math.sqrt((self.steve.x - z[0])**2 + (self.steve.y - z[1])**2)/self.GRID_SIZE)/2) # # print(distance_list) # if min(distance_list) < 1.6: # reward = reward_barrier # normal_distance = ((distance/self.GRID_SIZE)*(pi/2))-pi/4 # normal_distance = ((distance/self.GRID_SIZE)*(1.0))-0.4 # reward = np.tan(normal_distance) # reward = -np.exp(-distance)*10 # Linear distance reward # if len(self.zombie.array) > 0: # reward = (math.sqrt((self.steve.x - self.zombie.array[0][0])**2 + (self.steve.y - self.zombie.array[0][1])**2)/20)/self.GRID_SIZE # Exponential distance reward # if len(self.zombie.array) > 0: # reward = (((self.steve.x - self.zombie.array[0][0])**2 + (self.steve.y - self.zombie.array[0][1])**2)/20**2)/self.GRID_SIZE # Test: if moving, give a reward # if self.steve.dx != 0 or self.steve.dy != 0: # reward = -0.01 # Test for having a barrier around the zombie # if reward < 0.143: # reward = -5 # Update the position of steve self.steve.update(self.SCALE, action, self.ACTION_SPACE) if self.ENABLE_WRAP: self.wrap() else: if self.steve.x > self.DISPLAY_WIDTH - self.SCALE: reward = reward_out_of_bounds done = True if self.steve.x < 0: reward = reward_out_of_bounds done = True if self.steve.y > self.DISPLAY_HEIGHT - self.SCALE: reward = reward_out_of_bounds done = True if self.steve.y < 0: reward = reward_out_of_bounds done = True # Check for obstacle collision for i in range(self.obstacle.array_length): hit_obstacle = (self.steve.pos == self.obstacle.array[i]) if hit_obstacle: self.steve.x = self.steve.prev_pos[0] self.steve.y = self.steve.prev_pos[1] self.steve.pos = self.steve.prev_pos self.steve.history.pop(len(self.steve.history) - 1) self.steve.history.append(self.steve.pos) # done = True # reward = -0.1 # Check for lava collision for i in range(self.lava.array_length): in_lava = (self.steve.pos == self.lava.array[i]) if in_lava: done = True reward = reward_in_lava # Update the position of the zombie # self.zombie.move(self.maze, self.steve, self.steps, self.SCALE) self.zombie.range_move(self.maze, self.steve, self.steps, self.SCALE) # Check if zombie gets steve for i in range(self.zombie.amount): # print(self.steve.pos, self.zombie.array[i]) if self.steve.pos == self.zombie.array[i]: zombie_hit = True else: zombie_hit = False self.steve.isHit = False if zombie_hit: self.zombie.move_back(i) self.steve.isHit = True self.steve.health = self.steve.health - 1 # done = True reward = reward_zombie_hit break # Make the most recent history have the most negative rewards if self.steve.history_size != 0: decay = (reward_each_time_step) / (self.steve.history_size) for i in range(len(self.steve.history) - 1): # print(i,-1*(1-decay*i)) if ((self.steve.pos) == self.steve.history[-i - 2]): # reward = -1*(1-decay*i) break # Checking if Steve has reached the diamond reached_diamond, diamond_index = self.food.eat(self.steve) # ADDED FOR ALLOWING THE MODEL TO HAVE STEVE OVER THE FOOD IN THE STATE # if self.spawn_new_food: # disallowed = [] # [disallowed.append(grid_pos) for grid_pos in self.obstacle.array] # # [disallowed.append(grid_pos) for grid_pos in self.zombie.array] # # if self.steve.pos not in disallowed: # # disallowed.append(self.steve.pos) # self.food.make(self.grid, disallowed, index = self.last_eaten_food) # self.spawn_new_food = False # reached_diamond = False # If Steve reaches the food, increment score if reached_diamond: self.score += 1 self.last_eaten_food = diamond_index self.spawn_new_food = False # Create a piece of food that is not within Steve disallowed = [] [disallowed.append(grid_pos) for grid_pos in self.obstacle.array] # [disallowed.append(grid_pos) for grid_pos in self.zombie.array] self.food.make(self.grid, disallowed, index=diamond_index) # Only collect 1 food item at a time # done = True # Reward functions reward = reward_collecting_diamond else: self.spawn_new_food = False # If Steve collects a stick, increment score collected_stick, stick_index = self.stick.collect(self.steve) if collected_stick: self.score += 1 # Create a piece of food that is not within Steve disallowed = [] [disallowed.append(grid_pos) for grid_pos in self.obstacle.array] self.stick.make(self.grid, disallowed, index=stick_index) # Only collect 1 food item at a time # done = True # Reward functions reward = reward_collecting_diamond # For fun if self.score == 3: self.steve.hasSword = True done = True # To make it compatible with malmo if self.score == self.NUM_OF_FOOD: # reward = 1 pass if self.NUM_OF_FOOD != 0: done = True pass if self.steve.health <= 0: done = True # If the episode takes longer than the max time, it ends if self.time == self.MAX_TIME_PER_EPISODE: # print("Steve survived :)") # reward = -1.0 done = True # Get the new_state # new_state = self.state_array() # new_state = self.state_vector_3D() new_state = self.local_state_vector_3D() # print(reward) # print(self.steve.history) # A dictionary of information that may be useful info = {"time": self.time, "score": self.score} return new_state, reward, done, info def state_index(self, state_array): """ Given the state array, return the index of that state as an integer Used for the Qlearning lookup table """ return int((self.GRID_SIZE**3) * state_array[0] + (self.GRID_SIZE**2) * state_array[1] + (self.GRID_SIZE**1) * state_array[2] + (self.GRID_SIZE**0) * state_array[3]) def sample_action(self): """ Return a random action Can't use action space 4 with a tail, else it will have a double chance of doing nothing or crash into itself """ return np.random.randint(0, self.ACTION_SPACE) def set_state(self, state): """Set the state of the game environment""" self.state = state def set_map(self, map_path): self.MAP_PATH = map_path def number_of_states(self): """ Return the number of states with just the steve head and 1 food Used for Q Learning look up table """ return (self.GRID_SIZE**2) * ((self.GRID_SIZE**2)) def number_of_actions(self): """ Return the number of possible actions Action space: > nothing, up, down, left, right """ return self.ACTION_SPACE def state_array(self): """ The state represented as an array or steve positions and food positions Used for Q learning """ new_state = np.zeros(4) new_state[0] = int(self.steve.x / self.SCALE) new_state[1] = int(self.steve.y / self.SCALE) new_state[2] = int(self.food.x / self.SCALE) new_state[3] = int(self.food.y / self.SCALE) return new_state def state_vector(self): """ The state represented as a onehot 1D vector Used for the feed forward NN """ # (rows, columns) state = np.zeros((self.GRID_SIZE**2, 3)) # This is for STEVE, the FOOD and EMPTY for i in range(self.GRID_SIZE): # rows for j in range(self.GRID_SIZE): # columns if ((self.steve.x / self.SCALE) == j and (self.steve.y / self.SCALE) == i): state[i * self.GRID_SIZE + j] = [1, 0, 0] # print("steve:", i*self.GRID_SIZE+j) elif ((self.food.x / self.SCALE) == j and (self.food.y / self.SCALE) == i): state[i * self.GRID_SIZE + j] = [0, 1, 0] # print("Food:", i*self.GRID_SIZE+j) else: state[i * self.GRID_SIZE + j] = [0, 0, 1] # Flatten the vector to a 1 dimensional vector for the input layer to the NN state = state.flatten() state = state.reshape(1, (self.GRID_SIZE**2) * 3) # state = np.transpose(state) return state def state_vector_3D(self): """ State as a 3D vector of the whole map for the CNN Shape = (Layers, GRID_SIZE, GRID_SIZE) """ state = np.zeros((3, self.GRID_SIZE, self.GRID_SIZE)) state[0, int(self.steve.y / self.SCALE), int(self.steve.x / self.SCALE)] = 1 state[1, int(self.food.y / self.SCALE), int(self.food.x / self.SCALE)] = 1 # Obstacles for i in range(self.obstacle.array_length): state[2, int(self.obstacle.array[i][1] / self.SCALE), int(self.obstacle.array[i][0] / self.SCALE)] = 1 # Zombies # for i in range(len(self.zombie.array)): # state[1, int(self.zombie.array[i][1]/self.SCALE), int(self.zombie.array[i][0]/self.SCALE)] = 1 return state def local_state_vector_3D(self): """ State as a 3D vector of the local area around the steve Shape = (Layers, LOCAL_GRID_SIZE, LOCAL_GRID_SIZE) """ s_pos = 0 d_pos = 1 z_pos = 2 h_pos = 3 l_pos = 4 o_ops = 5 #s = steve sx = int(self.steve.x / self.SCALE) sy = int(self.steve.y / self.SCALE) state = np.zeros((6, self.LOCAL_GRID_SIZE, self.LOCAL_GRID_SIZE)) # Agent local_pos = int((self.LOCAL_GRID_SIZE - 1) / 2) state[s_pos, local_pos, local_pos] = 1 # Diamonds for i in range(self.food.amount): x_prime_food = local_pos + int( self.food.array[i][0] / self.SCALE) - sx y_prime_food = local_pos + int( self.food.array[i][1] / self.SCALE) - sy if x_prime_food < self.LOCAL_GRID_SIZE and x_prime_food >= 0 and y_prime_food < self.LOCAL_GRID_SIZE and y_prime_food >= 0: state[d_pos, y_prime_food, x_prime_food] = 1 pass # Zombies for i in range(len(self.zombie.array)): x_prime_zom = local_pos + int( self.zombie.array[i][0] / self.SCALE) - sx y_prime_zom = local_pos + int( self.zombie.array[i][1] / self.SCALE) - sy if x_prime_zom < self.LOCAL_GRID_SIZE and x_prime_zom >= 0 and y_prime_zom < self.LOCAL_GRID_SIZE and y_prime_zom >= 0: state[z_pos, y_prime_zom, x_prime_zom] = 1 pass # Obstacles for i in range(self.obstacle.array_length): x_prime_obs = local_pos + int( self.obstacle.array[i][0] / self.SCALE) - sx y_prime_obs = local_pos + int( self.obstacle.array[i][1] / self.SCALE) - sy if x_prime_obs < self.LOCAL_GRID_SIZE and x_prime_obs >= 0 and y_prime_obs < self.LOCAL_GRID_SIZE and y_prime_obs >= 0: state[o_ops, y_prime_obs, x_prime_obs] = 1 pass # Out of bounds for j in range(0, self.LOCAL_GRID_SIZE): for i in range(0, self.LOCAL_GRID_SIZE): x_prime_wall = local_pos - sx y_prime_wall = local_pos - sy if i < x_prime_wall or j < y_prime_wall: state[o_ops, j, i] = 1 pass x_prime_wall = local_pos + (self.GRID_SIZE - sx) - 1 y_prime_wall = local_pos + (self.GRID_SIZE - sy) - 1 if i > x_prime_wall or j > y_prime_wall: state[o_ops, j, i] = 1 pass # Lava for i in range(self.lava.array_length): x_prime_lava = local_pos + int( self.lava.array[i][0] / self.SCALE) - sx y_prime_lava = local_pos + int( self.lava.array[i][1] / self.SCALE) - sy if x_prime_lava < self.LOCAL_GRID_SIZE and x_prime_lava >= 0 and y_prime_lava < self.LOCAL_GRID_SIZE and y_prime_lava >= 0: state[l_pos, y_prime_lava, x_prime_lava] = 1 pass # History if self.steve.history_size != 0: decay = 1 / self.steve.history_size for i in range(len(self.steve.history) - 1): x_prime = local_pos + int( self.steve.history[-i - 2][0] / self.SCALE) - sx y_prime = local_pos + int( self.steve.history[-i - 2][1] / self.SCALE) - sy if x_prime < self.LOCAL_GRID_SIZE and x_prime >= 0 and y_prime < self.LOCAL_GRID_SIZE and y_prime >= 0: if 1 - decay * i >= 0 and state[h_pos, y_prime, x_prime] == 0: state[h_pos, y_prime, x_prime] = 1 - decay * i pass # Delete these layers: # state = np.delete(state, s_pos, 0) # state = np.delete(state, d_pos, 0) # state = np.delete(state, z_pos, 0) # state = np.delete(state, h_pos, 0) # state = np.delete(state, l_pos, 0) # state = np.delete(state, o_pos, 0) # DIAMOND DOJO # state = np.delete(state, 2, 0) # state = np.delete(state, 2, 0) # ZOMBIE DOJO # state = np.delete(state, 1, 0) # state = np.delete(state, 2, 0) # EXPLORE DOJO # state = np.delete(state, 1, 0) # state = np.delete(state, 1, 0) # GRID 9 SETUP # state = np.delete(state, 1, 0) # state = np.delete(state, 2, 0) # state = np.delete(state, 2, 0) # state = np.delete(state, 3, 0) # state = np.delete(state, 4, 0) return state def get_pixels(self): """ Returns the pixels in a (GRID*20, GRID*20, 3) size array/ Unfortunatly it has to render in order to gather the pixels """ return pygame.surfarray.array3d(pygame.display.get_surface()) def controls(self): """ Defines all the players controls during the game """ action = 0 # Do nothing as default for event in pygame.event.get(): # print(event) # DEBUGGING if event.type == pygame.QUIT: self.end() if event.type == pygame.KEYDOWN: # print(event.key) #DEBUGGING # In order to quit easily if (event.key == pygame.K_q): self.end() # Moving up if (event.key == pygame.K_UP or event.key == pygame.K_w): if self.ACTION_SPACE == 5: action = 1 # up # Moving down elif (event.key == pygame.K_DOWN or event.key == pygame.K_s): if self.ACTION_SPACE == 5: action = 2 # down # Moving left elif (event.key == pygame.K_LEFT or event.key == pygame.K_a): if self.ACTION_SPACE == 5: action = 3 # left # Moving right elif (event.key == pygame.K_RIGHT or event.key == pygame.K_d): if self.ACTION_SPACE == 5: action = 4 # right return action def play(self): """ Lets you simply play the game Useful for debugging and testing out the environment """ GAME_OVER = False self.prerender() # self.reset() for i in range(10): # MAP_NUMBER = np.random.randint(5) # MAP_NUMBER = 1 # MAP_PATH = "./Maps/Grid{}/map{}.txt".format(self.GRID_SIZE-2, MAP_NUMBER) # MAP_PATH = "./Maps/Grid{}/impossible_map_empty{}.txt".format(self.GRID_SIZE-2, MAP_NUMBER) # self.set_map(MAP_PATH) # self.reset() self.quick_reset() GAME_OVER = False while not GAME_OVER: # print(self.steve.history) action = self.controls() # action = self.render() # print(self.get_pixels().shape) s, r, GAME_OVER, i = self.step(action) # print("\n\n") # DEBUGGING # print(self.state_vector_3D()) # DEBUGGING # print(self.local_state_vector_3D()) # DEBUGGING # print(r) self.render() if GAME_OVER: print("Game Over: time:", i["time"]) self.end()
if RIGHT_DIR: s.update_tail() s.update_head(x=10) screen.fill((0,0,0)) textsurface = myfont.render(str(len(s.position)-2) , False, (255, 255, 255)) screen.blit(textsurface,(0,0)) if s.position[0] == f.position(): food_eaten = True for i in range(1,len(s.position)): if s.position[0] == s.position[i]: GAME_OVER = True print 'Game Over !!' break if food_eaten: s.eat(f.position()[0], f.position()[1]) f.generate(s.position) food_eaten = False f.draw(screen) s.draw(screen, color) #print 'Snake: ', s.position[0] ## Debug #print 'Food: ', f.position() ## Debug pygame.display.flip() clock.tick(15)
class SnakeGame: def __init__(self): print(pygame.font.get_fonts()) pygame.init() self.settings = Settings() self.screen = pygame.display.set_mode( (self.settings.screen_width, self.settings.screen_height)) pygame.display.set_caption('Hungry Python') self.stats = Stats(self) self.snakes = [] self.direction = ["LEFT", "RIGHT", "UP", "DOWN"] self.food = Food(self) self.score = 0 self._init_game() self.lose_button = Button(self, "You Lose!", 300, 250) self.restart_button = Button(self, "Click on spacebar or here to proceed", 300, 350, 30) self.highscore_button = Button(self, f"Highscore : {self.stats.highscore}", 480, 50, 30) self.score_button = Button(self, f"Score : {self.score}", 480, 50, 20) def _init_game(self): self.snakes = [] self.snakes.append( Snake(self, self.settings.snake_initial_x, self.settings.snake_initial_y)) self.snakes.append(Snake(self, 280, 300)) self.snakes.append(Snake(self, 260, 300)) self.moving_direction = self.direction[1] self.food.update() self.score = 0 def run_game(self): while True: self._check_events() if self.stats.game_active: self._update_snakes() self._check_collision() time.sleep(self.settings.sleep_length) self._update_screen() def _update_screen(self): self.screen.fill(self.settings.bg_color) for snake in self.snakes: snake.draw() self.food.draw() self.score_button.draw_button() if not self.stats.game_active: self.lose_button.draw_button() self.restart_button.draw_button() self.highscore_button.draw_button() pygame.display.flip() def _update_snakes(self, update_type="uncollided"): head_x = self.snakes[0].x head_y = self.snakes[0].y offset = self.settings.snake_speed if self.moving_direction == self.direction[0]: head_x -= offset elif self.moving_direction == self.direction[1]: head_x += offset elif self.moving_direction == self.direction[2]: head_y -= offset elif self.moving_direction == self.direction[3]: head_y += offset self.snakes.insert(0, Snake(self, head_x, head_y)) if update_type == "uncollided": self.snakes.pop(-1) def _check_collision(self): if pygame.Rect.colliderect(self.snakes[0].rect, self.food.rect): self._update_snakes("collided") self.food = Food(self) self.score += self.settings.points self.score_button.update_text(f"Score : {self.score}") for a in range(0, len(self.snakes) - 1): for b in range(a + 1, len(self.snakes)): if pygame.Rect.colliderect(self.snakes[a].rect, self.snakes[b].rect): self._lose() x = self.snakes[0].x y = self.snakes[0].y offset = self.settings.cube_length if x < 0 or x + offset > self.settings.screen_width: self._lose() elif y < 0 or y + offset > self.settings.screen_height: self._lose() def _lose(self): self.stats.game_active = False self.stats.update_highscore(self.score) self.highscore_button.update_text( f"Highscore : {self.stats.highscore}") self._init_game() def _check_events(self): for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type == pygame.KEYDOWN: self._check_keydown_events(event) elif event.type == pygame.MOUSEBUTTONDOWN: mouse_pos = pygame.mouse.get_pos() self._check_button_mouse(mouse_pos) def _check_keydown_events(self, event): if event.key == pygame.K_LEFT and not self.moving_direction == self.direction[ 1]: self.moving_direction = self.direction[0] elif event.key == pygame.K_RIGHT and not self.moving_direction == self.direction[ 0]: self.moving_direction = self.direction[1] elif event.key == pygame.K_UP and not self.moving_direction == self.direction[ 3]: self.moving_direction = self.direction[2] elif event.key == pygame.K_DOWN and not self.moving_direction == self.direction[ 2]: self.moving_direction = self.direction[3] elif event.key == pygame.K_SPACE: if not self.stats.game_active: self.stats.game_active = True def _check_button_mouse(self, mouse_pos): button_clicked = self.restart_button.text_rect.collidepoint(mouse_pos) if button_clicked and not self.stats.game_active: self.stats.game_active = True
class Game: """The main class that makes up the game""" def __init__(self): """Constructor""" self.speed = START_SPEED # will increase game speed every 10 times we eat self.eaten = 0 # defining the outer wall blocks self.wallblock = pygame.Surface(( BLOCK_SIZE, BLOCK_SIZE, )) self.wallblock.set_alpha(255) self.wallblock.fill(BLUE) self.wallblockdark = pygame.Surface(( BLOCK_SIZE_INNER, BLOCK_SIZE_INNER, )) self.wallblockdark.set_alpha(255) self.wallblockdark.fill(BLUE_DARK) # initialize pygame, clock for game speed and screen to draw pygame.init() # initializing mixer, sounds, clock and screen pygame.mixer.init() self.eatsound = pygame.mixer.Sound("snakeeat.wav") self.crashsound = pygame.mixer.Sound("snakecrash.wav") self.clock = pygame.time.Clock() self.screen = pygame.display.set_mode(( (WIDTH + 2) * BLOCK_SIZE, (HEIGHT + 2) * BLOCK_SIZE, )) pygame.display.set_caption("snake") font = pygame.font.SysFont(pygame.font.get_default_font(), 40) self.gameovertext = font.render("GAME OVER", 1, WHITE) self.starttext = font.render("PRESS ANY KEY TO START", 1, WHITE) self.screen.fill(BLACK) # we need a snake and something to eat self.snake = Snake(self.screen, WIDTH // 2, HEIGHT // 2) self.food = Food(self.screen, 1, HEIGHT + 1, 1, WIDTH + 1) # food should not appear where the snake is while self.food.get_pos() in self.snake.pos_list: self.food = Food(self.screen, 1, HEIGHT + 1, 1, WIDTH + 1) # only queue quit and and keydown events # pygame.event.set_allowed([pygame.QUIT, pygame.KEYDOWN]) pygame.event.set_blocked([ pygame.MOUSEMOTION, pygame.MOUSEBUTTONUP, pygame.MOUSEBUTTONDOWN, ]) def draw_walls(self): """Draw all walls of the game surface""" # left and right walls for y in range(HEIGHT + 1): # pylint: disable=invalid-name self.screen.blit(self.wallblock, ( 0, y * BLOCK_SIZE, )) self.screen.blit(self.wallblockdark, ( 5, y * BLOCK_SIZE + 5, )) self.screen.blit(self.wallblock, ( (WIDTH + 1) * BLOCK_SIZE, y * BLOCK_SIZE, )) self.screen.blit(self.wallblockdark, ( (WIDTH + 1) * BLOCK_SIZE + 5, y * BLOCK_SIZE + 5, )) # upper and bottom walls for x in range(WIDTH + 2): # pylint: disable=invalid-name self.screen.blit(self.wallblock, ( x * BLOCK_SIZE, 0, )) self.screen.blit(self.wallblockdark, ( x * BLOCK_SIZE + 5, 5, )) self.screen.blit(self.wallblock, ( x * BLOCK_SIZE, (HEIGHT + 1) * BLOCK_SIZE, )) self.screen.blit(self.wallblockdark, ( x * BLOCK_SIZE + 5, (HEIGHT + 1) * BLOCK_SIZE + 5, )) def loop(self): """The game's main loop""" while True: # check crash or move outside the limits if self.snake.outside_limits(WIDTH, HEIGHT) or self.snake.crashed: self.crashsound.play() return # draw screen with snake and foods self.food.draw() self.snake.draw() self.draw_walls() pygame.display.flip() # check if snake eates if self.food.get_pos() == self.snake.get_head_pos(): self.eatsound.play() self.snake.grow() # food should not appear where the snake is self.food = Food(self.screen, 1, HEIGHT + 1, 1, WIDTH + 1) while self.food.get_pos() in self.snake.pos_list: self.food = Food(self.screen, 1, HEIGHT + 1, 1, WIDTH + 1) self.eaten += 1 # increase game speed if self.eaten % SPEED_INC == 0: self.speed += SPEED_TICK # game speed control self.clock.tick(self.speed) # get the next event on queue event = pygame.event.poll() if event.type == pygame.QUIT: sys.exit() elif event.type == pygame.KEYDOWN: actmotdir = self.snake.motion_dir if event.key == pygame.K_ESCAPE: sys.exit() elif event.key == pygame.K_UP and actmotdir != DOWN: self.snake.motion_dir = UP elif event.key == pygame.K_DOWN and actmotdir != UP: self.snake.motion_dir = DOWN elif event.key == pygame.K_RIGHT and actmotdir != LEFT: self.snake.motion_dir = RIGHT elif event.key == pygame.K_LEFT and actmotdir != RIGHT: self.snake.motion_dir = LEFT # remove the snake and make movement self.snake.remove() self.snake.move() def game_over(self): """When crashed print "game over" and wait for Esc key""" self.clock.tick(LONG) self.snake.draw() self.draw_walls() for pos in self.snake.pos_list[1:]: self.screen.blit(self.snake.backblock, ( pos[1] * BLOCK_SIZE, pos[0] * BLOCK_SIZE, )) pygame.display.flip() self.clock.tick(SHORT) while True: self.screen.blit(self.gameovertext, ( (WIDTH - 4) * BLOCK_SIZE / 2, HEIGHT * BLOCK_SIZE / 2, )) pygame.display.flip() event = pygame.event.wait() if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: sys.exit() def start(self): """The game starts here""" # press any key to start!!! self.draw_walls() self.screen.blit(self.starttext, ( (WIDTH - 10) * BLOCK_SIZE / 2, HEIGHT * BLOCK_SIZE / 2, )) pygame.display.flip() waiting = True while waiting: event = pygame.event.wait() if event.type == pygame.KEYDOWN: waiting = False self.screen.fill(BLACK) # main loop self.loop() self.game_over()
class SnakeGame(arcade.Window): def __init__(self): super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE) self.set_update_rate(0.25) self.wall_list = None self.snake = None self.food = None self.score = 0 self.should_wait_until_next_redraw = False self.game_over = False def setup(self): self.wall_list = arcade.SpriteList() self.snake = Snake() self.food = Food() self.score = 0 self.should_wait_until_next_redraw = False self.game_over = False self.setup_wall() self.snake.setup() self.setup_food() def setup_wall(self): for x in range(0, SCREEN_WIDTH, TILE_SIZE): for y in range(0, SCREEN_HEIGHT, TILE_SIZE): wall = arcade.Sprite( get_texture("images/tiles/grassCenter.png"), TILE_SCALING) wall.left = x wall.bottom = y self.wall_list.append(wall) def setup_food(self): while True: x = random.randrange(0, SCREEN_WIDTH, TILE_SIZE) y = random.randrange(0, SCREEN_HEIGHT, TILE_SIZE) if not self.snake.exists_at_coordinates(x, y): break self.food.left = x self.food.bottom = y def on_draw(self): self.game_over = self.game_over or self.snake.is_off_screen( ) or self.snake.has_collided_with_self() self.should_wait_until_next_redraw = False self.snake.move() self.wall_list.draw() if self.snake.exists_at_coordinates(self.food.left, self.food.bottom): self.setup_food() self.score += 1 self.snake.extend() if self.game_over: self.draw_game_over() else: self.snake.draw() self.food.draw() score_text = f"Score: {self.score}" arcade.draw_text(score_text, 10, 10, arcade.csscolor.WHITE, 18) @staticmethod def draw_game_over(): arcade.draw_text("Game Over", TILE_SIZE, SCREEN_HEIGHT / 2, arcade.color.WHITE, 54) def on_key_press(self, key, modifiers): # If multiple key presses handled between redraws, there is a risk that the snake can do # a 180 degree turn on the spot thus moving over itself. This flag prevents this. if self.should_wait_until_next_redraw: return else: self.should_wait_until_next_redraw = True if (key == arcade.key.UP or key == arcade.key.W) and not self.snake.moving_down: self.snake.set_moving_up() elif (key == arcade.key.DOWN or key == arcade.key.S) and not self.snake.moving_up: self.snake.set_moving_down() elif (key == arcade.key.LEFT or key == arcade.key.A) and not self.snake.moving_right: self.snake.set_moving_left() elif (key == arcade.key.RIGHT or key == arcade.key.D) and not self.snake.moving_left: self.snake.set_moving_right()
class Board: def __init__(self, screen): self.screen = screen self.segments = [] self.blockSize = 20 self.segmentLife = 3 self.stillPlaying = True self.padding = 3 self.food = Food(self.screen) self.randomizeFood() self.score = 0 self.insert(Segment()) self.paused = False self.messages = [] def draw(self): self.drawSegments() self.food.draw() self.drawScore() self.drawMessages() def drawSegments(self): for segment in self.segments: segment.draw(self.screen) def drawMessages(self): for message in self.messages: message.draw(self.screen) self.messages = [] def drawScore(self): scoreText = "Score: " + str(self.score) self.messages.append(Message(scoreText)) def insert(self, segment): self.segments.insert(0, segment) def updateSegments(self): survivingSegments = [] for segment in self.segments: segment.update() if segment.life > 0: survivingSegments.append(segment) self.segments = survivingSegments def update(self, direction='right'): if self.stillPlaying and not self.paused: self.updateSnake(direction) self.draw() elif not self.stillPlaying: self.draw() deathMessage = "You have died. Press space to reset or q to quit." self.messages.append( Message( deathMessage, self.screen.get_rect().centerx - TextRenderer.getRenderedWidth(deathMessage) / 2, self.screen.get_rect().centery)) else: self.draw() def updateSnake(self, direction='right'): self.updateSegments() self.moveSnake(direction) if (not self.snakeIsInsideScreen( self.screen)) or (self.segments[0].getRect().collidelist( self.segments[1:]) != -1): self.stillPlaying = False if (self.collidingWithFood()): self.increaseLength(5) self.score += 1 self.randomizeFood() def snakeIsInsideScreen(self, screen): return screen.get_rect().contains(self.segments[0].getRect()) def moveSnake(self, direction): if (direction == 'right'): self.insert( Segment(self.segments[0].x + self.blockSize + self.padding, self.segments[0].y, self.segmentLife, self.blockSize)) elif (direction == 'left'): self.insert( Segment(self.segments[0].x - self.blockSize - self.padding, self.segments[0].y, self.segmentLife, self.blockSize)) elif (direction == 'up'): self.insert( Segment(self.segments[0].x, self.segments[0].y - self.blockSize - self.padding, self.segmentLife, self.blockSize)) elif (direction == 'down'): self.insert( Segment(self.segments[0].x, self.segments[0].y + self.blockSize + self.padding, self.segmentLife, self.blockSize)) def collidingWithFood(self): return self.segments[0].getRect().colliderect(self.food.getRect()) def increaseLength(self, length): self.segmentLife += length def randomizeFood(self): self.food.getRect().x = random.choice( range( 10, self.screen.get_rect().width - (self.blockSize + self.padding + 10))) self.food.getRect().y = random.choice( range( 10, self.screen.get_rect().height - (self.blockSize + self.padding + 10))) if (self.isCollidingWithSnake(self.food)): self.randomizeFood() def isCollidingWithSnake(self, collideableObject): return collideableObject.getRect().collidelist(self.segments) != -1
class Game: def __init__(self): self.snake = None self.food = None self.clock = None self.screen = None self.menu = None self.background_image = None self.background_sound = None self.fps = settings.fps self.font = None self.menu = None # pygame and other object inizialization def inizialization(self) -> None: # pygame inizialization pygame.init() pygame.display.set_caption(settings.caption) # other objects inizialization self.screen = pygame.display.set_mode( (settings.width, settings.height)) self.snake = Snake() self.food = Food() self.clock = pygame.time.Clock() self.background_image = pygame.image.load( 'data/background/background.png').convert() self.font = pygame.font.SysFont('Arial', 36, True) self.menu = Menu() # draw sprites and bg def draw_game_objects(self) -> None: self.screen.blit(self.background_image, (0, 0)) self.snake.draw(self.screen) self.food.draw(self.screen) # game loop def main_loop(self) -> None: in_menu = True running = True paused = False render = self.font.render("PAUSE", 1, settings.RED) back_to_menu_button = Back_to_menu_button(780, 560, 210, 40) while running: for event in pygame.event.get(): if event.type == pygame.QUIT: exit() if not in_menu: # not in menu if event.type == pygame.KEYUP: if event.key == pygame.K_ESCAPE: paused = not paused if not in_menu: # not in menu if not paused: # game is not in pause self.snake.movement(self.food) self.draw_game_objects() if not self.snake.check_collision(): self.game_over() self.reset() else: # game in pause render_score = self.font.render( f"Your score: {self.snake.score}!", 1, settings.RED) self.screen.blit(render_score, (10, 10)) self.screen.blit(render, (435, 250)) back_to_menu_button.draw(self.screen) if back_to_menu_button.check_state(): self.reset() in_menu = not in_menu paused = False else: # in menu if self.menu.update(self.screen): in_menu = not in_menu pygame.display.flip() self.clock.tick(self.fps) # game over func def game_over(self): print(f"Your score: {self.snake.score}!") # reset for new game def reset(self): self.snake = Snake() self.food = Food()
s=Snake() f=Food() sc=score() screen.listen() screen.onkey(fun=s.Left,key="Left") screen.onkey(fun=s.Up,key="Up") screen.onkey(fun=s.Down,key="Down") screen.onkey(fun=s.Right,key="Right") option=1 while option: screen.update() time.sleep(0.1) s.move() if s.head.distance(f)<15: s.extend() f.draw() sc.w() if s.head.xcor()>=280 or s.head.ycor()>=280 or s.head.xcor()<=-280 or s.head.ycor()<=-280: print("Imma out") sc.w() sc.high_score() sc.reset() s.reset() for segments in s.t: if segments==s.head: pass elif s.head.distance(segments)<15: sc.w() sc.high_score() sc.reset() s.reset()
class Game(): def __init__(self): self.player = Player() self.food = Food() self.points = 0 self.ticks = 0 self.running = False self.foodExists = False def startGame(self): pygame.init() clock = pygame.time.Clock() display = pygame.display.set_mode((500,500)) self.running = True while self.running: clock.tick(15) self.ticks += 1 self.eventController() self.collisionController() self.inputController() self.foodController() self.updateController() self.drawController(display) def eventController(self): for event in pygame.event.get(): if event.type == pygame.QUIT: self.running = False return def inputController(self): keyInput = pygame.key.get_pressed() if keyInput[pygame.K_ESCAPE]: self.running = False return if keyInput[pygame.K_UP] and self.player.head.direction != constants.downDir: self.player.head.cube.updateDirection(constants.upDir) if keyInput[pygame.K_DOWN] and self.player.head.direction != constants.upDir: self.player.head.cube.updateDirection(constants.downDir) if keyInput[pygame.K_LEFT] and self.player.head.direction != constants.rightDir: self.player.head.cube.updateDirection(constants.leftDir) if keyInput[pygame.K_RIGHT] and self.player.head.direction != constants.leftDir: self.player.head.cube.updateDirection(constants.rightDir) def drawController(self, display): display.fill((0, 0, 0)) self.food.draw(display) for tail in self.player.body: tail.cube.draw(display) pygame.display.flip() def updateController(self): for tail in self.player.body: tail.cube.update() if tail.forward != None: tail.cube.updateTailLocation(tail.forward.cube.getDirection(), 10) def collisionController(self): if self.player.body[0].cube.getLocation() == self.food.getLocation(): self.points += 1 self.player.addTail() self.foodExists = False def foodController(self): if self.foodExists == False: self.food.updateLocation(Location(self.food.getRandomLocation(), self.food.getRandomLocation())) self.foodExists = True
class Board: def __init__(self, screen): self.screen = screen self.segments = [] self.blockSize = 20 self.segmentLife = 3 self.stillPlaying = True self.padding = 3 self.food = Food(self.screen) self.randomizeFood() self.score = 0 self.insert(Segment()) self.paused = False self.messages = [] def draw(self): self.drawSegments() self.food.draw() self.drawScore() self.drawMessages() def drawSegments(self): for segment in self.segments: segment.draw(self.screen) def drawMessages(self): for message in self.messages: message.draw(self.screen) self.messages = [] def drawScore(self): scoreText = "Score: " + str(self.score) self.messages.append(Message(scoreText)) def insert(self, segment): self.segments.insert(0, segment) def updateSegments(self): survivingSegments = [] for segment in self.segments: segment.update() if segment.life > 0: survivingSegments.append(segment) self.segments = survivingSegments def update(self, direction='right'): if self.stillPlaying and not self.paused: self.updateSnake(direction) self.draw() elif not self.stillPlaying: self.draw() deathMessage = "You have died. Press space to reset or q to quit." self.messages.append(Message(deathMessage, self.screen.get_rect().centerx - TextRenderer.getRenderedWidth(deathMessage)/2, self.screen.get_rect().centery)); else: self.draw() def updateSnake(self, direction='right'): self.updateSegments() self.moveSnake(direction) if (not self.snakeIsInsideScreen(self.screen)) or (self.segments[0].getRect().collidelist(self.segments[1:]) != -1): self.stillPlaying = False if(self.collidingWithFood()): self.increaseLength(5) self.score += 1 self.randomizeFood() def snakeIsInsideScreen(self, screen): return screen.get_rect().contains(self.segments[0].getRect()) def moveSnake(self, direction): if(direction == 'right'): self.insert(Segment(self.segments[0].x + self.blockSize+self.padding, self.segments[0].y, self.segmentLife, self.blockSize)) elif(direction == 'left'): self.insert(Segment(self.segments[0].x - self.blockSize-self.padding, self.segments[0].y, self.segmentLife, self.blockSize)) elif(direction == 'up'): self.insert(Segment(self.segments[0].x, self.segments[0].y - self.blockSize-self.padding, self.segmentLife, self.blockSize)) elif(direction == 'down'): self.insert(Segment(self.segments[0].x, self.segments[0].y + self.blockSize+self.padding, self.segmentLife, self.blockSize)) def collidingWithFood(self): return self.segments[0].getRect().colliderect(self.food.getRect()) def increaseLength(self, length): self.segmentLife += length def randomizeFood(self): self.food.getRect().x = random.choice(range(10, self.screen.get_rect().width-(self.blockSize+self.padding+10))) self.food.getRect().y = random.choice(range(10, self.screen.get_rect().height-(self.blockSize+self.padding+10))) if(self.isCollidingWithSnake(self.food)): self.randomizeFood() def isCollidingWithSnake(self, collideableObject): return collideableObject.getRect().collidelist(self.segments) != -1
class Game: def __init__(self, w, h): self.width = w self.height = h self.food = Food(w / 2, h / 2) self.player = Snake(40, 40) self.player2 = Snake(100, 100) self.canvas = Canvas(self.width, self.height, "Testing...") self.gridwh = 20 self.mv = None def run(self): clock = pygame.time.Clock() run = True while run: clock.tick(60) for event in pygame.event.get(): if event.type == pygame.QUIT: run = False if event.type == pygame.K_ESCAPE: run = False keys = pygame.key.get_pressed() if keys[pygame.K_RIGHT]: if self.player.facing != 1: self.mv = 0 if keys[pygame.K_LEFT]: if self.player.facing != 0: self.mv = 1 if keys[pygame.K_UP]: if self.player.facing != 3: self.mv = 2 if keys[pygame.K_DOWN]: if self.player.facing != 2: self.mv = 3 if self.player.location == self.food.location: self.player.eat() self.player.eaten += 1 self.food.respawn() elif self.player.location in self.player.length[: -1] or self.player.location in self.player.set_pieces: self.player.respawn() if 0 <= self.player.x < self.width and 0 <= self.player.y < self.height: pass else: self.player.respawn() if self.player.x % 20 == 0 and self.player.y % 20 == 0: if self.mv == 0: self.player.turn(0) elif self.mv == 1: self.player.turn(1) elif self.mv == 2: self.player.turn(2) elif self.mv == 3: self.player.turn(3) self.player.move(self.player.facing) # Update Canvas self.canvas.draw_background() self.player.draw(self.canvas.get_canvas()) ### self.player.draw_sets(self.canvas.get_canvas()) ### # self.player2.draw(self.canvas.get_canvas()) self.food.draw(self.canvas.get_canvas()) self.canvas.update() pygame.quit()
class PlayState(GameState): TICK_LENGTH: float = 0.1 GRID_SIZE: int = 50 INITIAL_BODY_SIZE = 3 WHITE = (255, 255, 255) BLACK = (0, 0, 0) FOOD_COLOR = (0xFF, 0x90, 0x00) tick_time: float head: Head body: List[Body] food: Food hud: HUD def init(self): PlayState.TICK_LENGTH = 0.1 self.tick_time = 0 self.head = Head(PlayState.GRID_SIZE) self.body = [] for i in range(PlayState.INITIAL_BODY_SIZE): self.__add_body__() self.food = Food(PlayState.GRID_SIZE, self.head.pos) self.hud = HUD() pass def handle_input(self): if MyInput.key_check_pressed(MyInput.BACK): self.switch_state(GameStateType.TITLE) self.head.handle_input() pass def update(self, screen_size: Tuple[float, float], delta_time): self.tick_time += delta_time if self.tick_time >= PlayState.TICK_LENGTH: self.tick_time = 0 self.__tick__(delta_time) pass def __tick__(self, delta_time): for body in self.body: if body.colliding_with(self.head): Snake.score = len(self.body) - PlayState.INITIAL_BODY_SIZE self.switch_state(GameStateType.GAME_OVER) if len(self.body) >= 1: self.body[0].set_direction(self.head) for i in range(1, len(self.body)): self.body[i].set_direction(self.body[i - 1]) for body in self.body: body.update() self.head.update() if self.head.colliding_with(self.food): PlayState.TICK_LENGTH *= .95 self.food.reset(PlayState.GRID_SIZE, self.head.pos) self.__add_body__() if self.head.pos.x < 0 or self.head.pos.x > PlayState.GRID_SIZE - 1 or self.head.pos.y < 0 or self.head.pos.y > PlayState.GRID_SIZE - 1: Snake.score = len(self.body) - PlayState.INITIAL_BODY_SIZE self.switch_state(GameStateType.GAME_OVER) def draw(self, surface: Surface, screen_size: Tuple[float, float], delta_time): cell_width = 10 grid_width = cell_width * PlayState.GRID_SIZE start_x = (screen_size[0] * 0.5) - (grid_width * 0.5) start_y = (screen_size[1] * 0.5) - (grid_width * 0.5) pygame.draw.rect(surface, PlayState.WHITE, Rect(start_x, start_y, grid_width, grid_width), 1) self.head.draw(surface, PlayState.BLACK, start_x, start_y, cell_width, cell_width) for body in self.body: body.draw(surface, PlayState.BLACK, start_x, start_y, cell_width, cell_width) self.food.draw(surface, PlayState.FOOD_COLOR, start_x, start_y, cell_width, cell_width) score = len(self.body) - PlayState.INITIAL_BODY_SIZE self.hud.draw(surface, start_x, start_y, grid_width, score, PlayState.BLACK) def __add_body__(self): if len(self.body) < 1: self.body.append(Body(self.head)) else: self.body.append(Body(self.body[len(self.body) - 1]))
class Game: def __init__(self, board_size): self.__board_size = board_size self.__food = Food(random.randint(0, board_size // 4), random.randint(0, board_size // 6), 50, board_size) self.__snake_vel = [0, 0] self.__init_game() def start(self, FPS): clock = pygame.time.Clock() while not self.__game_over: self.__logic() self.__input() self.__render() clock.tick(FPS) def __init_game(self): pygame.init() self.__BACKGROUND_COLOR = (0, 0, 0) self.__score_font = pygame.font.SysFont("monospace", 35) self.__WIDTH = self.__board_size + 150 self.__HEIGHT = self.__board_size + 100 self.__snake = Snake([self.__board_size // 2, self.__board_size // 2], (self.__WIDTH, self.__HEIGHT)) self.__screen = pygame.display.set_mode((self.__WIDTH, self.__HEIGHT)) self.__game_over = False def __logic(self): if self.__food.is_colliding(self.__snake.get_position(), self.__snake.get_rect_size()): self.__snake.add_point() self.__food.spawn() def __input(self): for event in pygame.event.get(): # the X button is pressed if event.type == pygame.QUIT: sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: self.__snake_vel = [-1, 0] elif event.key == pygame.K_RIGHT: self.__snake_vel = [1, 0] elif event.key == pygame.K_UP: self.__snake_vel = [0, -1] elif event.key == pygame.K_DOWN: self.__snake_vel = [0, 1] elif event.key == pygame.K_p: self.__pause_game() self.__game_over = self.__snake.move(self.__snake_vel) def __render(self): self.__screen.fill(self.__BACKGROUND_COLOR) self.__food.draw(self.__screen) self.__snake.draw(self.__screen) self.__print_score() pygame.display.update() def __print_score(self): text = "Score: " + str(self.__snake.get_score()) COLOR = (255, 255, 0) # yellow label = self.__score_font.render(text, 1, COLOR) self.__screen.blit(label, (5, self.__HEIGHT - 40)) def __pause_game(self): pause = True while pause: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_p: pause = False
class Playing(State): COLLIDED = 0 def __init__(self, game): self.snake = Snake() self.elapsed_time = pygame.time.get_ticks() self.game = game self.food = None self.font = pygame.font.SysFont('Arial', 14, bold=True) self.point_text = "Points: " self.spawn_food() def draw_grid(self, win: pygame.Surface): for row in range(1, ROWS): start_pos = (0, row * SQUARE_SIZE) end_pos = (WIDTH, row * SQUARE_SIZE) pygame.draw.line(win, GREEN, start_pos, end_pos) for col in range(1, COLS): start_pos = (col * SQUARE_SIZE, 0) end_pos = (col * SQUARE_SIZE, HEIGHT) pygame.draw.line(win, GREEN, start_pos, end_pos) def spawn_food(self): unallowed_squares = map(lambda segment: (segment.row, segment.col), self.snake.segments) self.food = Food(unallowed_squares=unallowed_squares) def draw(self, win: pygame.Surface): self.draw_grid(win) self.snake.draw(win) self.food.draw(win) point_text = self.point_text + str(self.snake.points) text = self.font.render(point_text, True, WHITE) text_rect = text.get_rect() text_rect.right = WIDTH text_rect.top = 0 win.blit(text, text_rect) def move(self): self.snake.move() def handle_event(self, event: pygame.event.EventType): self.snake.handle_event(event) def check_for_collisions(self): return self.snake.check_for_collisions(self.food.row, self.food.col) def run(self, win: pygame.Surface): for event in pygame.event.get(): if event.type == pygame.QUIT: return False self.handle_event(event) new_elapsed_time = pygame.time.get_ticks() delta_time = new_elapsed_time - self.elapsed_time if delta_time >= MOVE_TIME: self.move() self.elapsed_time = new_elapsed_time collision = self.check_for_collisions() if collision == Snake.WALL or collision == Snake.SNAKE: self.game.set_state(self.game.MENU) elif collision == Snake.FOOD: self.snake.grow() self.spawn_food() return True
class Game(object): def __init__(self): # Create board self.width = 750 self.height = 550 self.board = Board(self.width, self.height) # Create board ancho, alto self.board.setTitle("Snake") # Set tittle # Create snake self.cell1 = Cell((self.height / 2) + 10, (self.width / 2) + 10) # Create Cell cell2 = Cell(390, 390) self.snake = Snake(self.cell1) self.snake.appendCell(cell2) # Create food self.food = Food(random.randint(0, self.width - 10), random.randint(0, self.height - 10)) # Create Score self.score = 0 # AI self.reward = 0 def addScore(self, point): self.score += 1 def showScore(self, screen): font = pygame.font.Font('freesansbold.ttf', 18) score = font.render("Score: " + str(self.score), True, (255, 255, 255)) screen.blit(score, (10, 10)) # Calculate distance between the head and the snake body def isCollition(self, headPosx, cellX, headPosy, cellY): distance = math.sqrt((math.pow(headPosx - cellX, 2)) + (math.pow(headPosy - cellY, 2))) if distance <= 0: return True # Shows Game over title def gameOver(self, screen): font = pygame.font.Font('freesansbold.ttf', 40) game = font.render("GAME OVER", True, (255, 255, 255)) screen.blit(game, (220, (self.height) / 2)) pygame.display.flip() def restart(self): self.__init__() # Main method that shows board and cell and get events for the movement def run(self): game_over = False self.board.showScreen() screen = self.board.getScreen() running = True while running: for event in pygame.event.get(): # print(event) # to print clicks events and keyboard events if event.type == pygame.QUIT: running = False game_over = True # IF keystroke is pressed check whether its right or left if event.type == pygame.KEYDOWN: # KEYDOW is pressing that key or pressing any button on the keyboard if event.key == pygame.K_RETURN: self.restart() game_over = False if self.snake.direction == 'down': if event.key == pygame.K_LEFT: self.snake.move_left() if event.key == pygame.K_RIGHT: self.snake.move_right() if self.snake.direction == 'left': if event.key == pygame.K_UP: self.snake.move_up() if event.key == pygame.K_DOWN: # self.cell.move_down() # self.snake[0].move_down() self.snake.move_down() if self.snake.direction == 'right': if event.key == pygame.K_UP: self.snake.move_up() if event.key == pygame.K_DOWN: # self.cell.move_down() # self.snake[0].move_down() self.snake.move_down() if self.snake.direction == 'up': if event.key == pygame.K_LEFT: self.snake.move_left() if event.key == pygame.K_RIGHT: self.snake.move_right() if not game_over: # EAT eat = self.snake.eat(self.food.getPosX(), self.food.getPosY()) if eat: # Move the apple each time the snake eat it self.food.setPosX(random.randint(20, self.width - 18)) self.food.setPosY(random.randint(20, self.height - 16)) # create a new cell and add it to the snake cell = Cell(390, 390) self.snake.appendCell(cell) # Add score self.addScore(1) # Show Snake, food and score self.snake.walk(screen) self.food.draw(screen) self.showScore(screen) # validate when snake eats himself # Get head position head = [ self.snake.getHeadPossition()[0], self.snake.getHeadPossition()[1] ] body = self.snake.getBody() for i in range(3, len(body)): if self.isCollition(head[0], body[i].getPosX(), head[1], body[i].getPosY()): game_over = True # Validate borders if head[0] >= self.width - 20 or head[ 1] >= self.height - 18 or head[0] <= -1.5 or head[ 1] <= 0.9: game_over = True time.sleep(0.07) pygame.display.update() else: self.gameOver(screen) return game_over
food = Food(surface=screen, screen_width=screen_width, screen_height=screen_height, bg_rect_height=background.rect_height) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() keys = pygame.key.get_pressed() snake.get_direction(keys=keys) snake.move() if snake.head_x_position == food.food_x_position and snake.head_y_position == food.food_y_position: snake.tail.append((snake.head_x_position, snake.head_y_position, snake.head_size, snake.head_size)) food.new_position() # fill the screen and draw all elements screen.fill(white) background.draw() snake.draw() food.draw() snake.update_positions() pygame.display.update() clock.tick(15)
def game(): def countdown(): countdown_font = pygame.font.Font('atari.ttf', 42) SECOND_ELAPSED = pygame.USEREVENT time = 3 beep_sound.play() pygame.time.set_timer(SECOND_ELAPSED, 1000) while time > 0: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == SECOND_ELAPSED: time -= 1 if time != 0: beep_sound.play() if time == 0: break screen.fill(LIGHT_BLUE) draw_board() food.draw(screen) snake.draw(screen) info_bar.display_score(screen) countdown_text = countdown_font.render(str(time), True, BLACK) screen.blit(countdown_text, (BOARD_WIDTH // 2 - countdown_text.get_width() // 2, BOARD_HEIGHT // 2 - countdown_text.get_height() // 2)) pygame.display.update() pygame.time.set_timer(SECOND_ELAPSED, 0) snake = Snake() food = Food(ROWS, COLS, snake, CELL_WIDTH, 40) high_scores_file_name = 'high_scores.txt' info_bar = InfoBar(screen, high_scores_file_name) hit_sound = pygame.mixer.Sound('vgdeathsound.ogg') beep_sound = pygame.mixer.Sound('beep.ogg') countdown() pygame.mixer.music.load('Arizona-Sunset.mp3') pygame.mixer.music.play(-1) done = False while not done: changed_direction = False for event in pygame.event.get(): if event.type == pygame.QUIT: done = True elif not changed_direction and event.type == pygame.KEYUP: if event.key == pygame.K_UP: changed_direction = True snake.up() elif event.key == pygame.K_DOWN: changed_direction = True snake.down() elif event.key == pygame.K_LEFT: changed_direction = True snake.left() elif event.key == pygame.K_RIGHT: changed_direction = True snake.right() snake.update() if snake.has_collided_with_wall( 40, BOARD_HEIGHT, 0, BOARD_WIDTH) or snake.has_collided_with_tail(): screen.fill(LIGHT_BLUE) draw_board() food.draw(screen) snake.draw(screen) info_bar.display_score(screen) pygame.display.update() hit_sound.play() info_bar.update_high_scores_if_needed() pygame.mixer.music.stop() done, menu = game_over() if menu: return if not done: snake.reset() food = Food(ROWS, COLS, snake, CELL_WIDTH, 40) #or just reset info_bar.reset() hit_sound.stop() countdown() pygame.mixer.music.load('Arizona-Sunset.mp3') pygame.mixer.music.play(-1) food.is_eaten(info_bar) screen.fill(LIGHT_BLUE) draw_board() food.draw(screen) snake.draw(screen) info_bar.display_score(screen) if not done: pygame.display.update() clock.tick(FPS) return done
keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: snake.moveleft() elif keys[pygame.K_RIGHT]: snake.moveright() elif keys[pygame.K_DOWN]: snake.movedown() elif keys[pygame.K_UP]: snake.moveup() GAME_TABLE = render.Rendering(SCREEN, DARK_GREEN, LIGHT_GREEN, WHITE) if current < max: current += 1 food = Food(SCREEN) foodgroup.add(food) food.draw(SCREEN, snake.get_pos()) foodgroup.update() for c in foodgroup: c.redraw(SCREEN) if pygame.sprite.collide_rect(snake, c): current -= 1 snake.set_points(c.get_points()) c.kill() snake.draw() tail.draw() render.Rendering.show_points(GAME_TABLE, points=snake.get_points()) pygame.display.update() pygame.quit()