def __init__(self, size): self.size = size self.height, self.width = size self.map_matrix = np.zeros((self.width, self.height)) self.fruitLocation = [ random.randint(0, self.width - 1), random.randint(0, self.height - 1) ] self.snake = Snake(self.height, self.width) self.frames = None self.direction = 0 self.score = 0 self.num_actions = 3
def __init__(self, surface, username, numberOfPlayers, soundsOn): # Initialize variables self.__clock = pygame.time.Clock() self.__surface = surface self.__isRunning = True self.__username = username self.__numberOfPlayers = numberOfPlayers self.__soundsOn = soundsOn # Initialize the game entities self.__ball = Ball(self.__surface) self.__score = self.__ball.get_score() self.__playfield = Playfield(self.__surface, self.__username, self.__score) self.__surface = self.__playfield.get_surface() self.__snake = Snake(self.__surface, self.__isRunning) self.__food = Food(self.__surface) # Check if multiplayer or not if self.__numberOfPlayers == 0: self.__paddle = Paddle(self.__surface) else: self.__paddle = Paddle(self.__surface, True)
def test_snake_creation(): snake = Snake(surface, True) all_directions = [(0, 1), (0, -1), (-1, 0), (1, 0)] assert snake.get_head_position() == (600, 400) assert snake.get_direction() in all_directions assert snake.get_length() == 15
class Game: def __init__(self, surface, username, numberOfPlayers, soundsOn): # Initialize variables self.__clock = pygame.time.Clock() self.__surface = surface self.__isRunning = True self.__username = username self.__numberOfPlayers = numberOfPlayers self.__soundsOn = soundsOn # Initialize the game entities self.__ball = Ball(self.__surface) self.__score = self.__ball.get_score() self.__playfield = Playfield(self.__surface, self.__username, self.__score) self.__surface = self.__playfield.get_surface() self.__snake = Snake(self.__surface, self.__isRunning) self.__food = Food(self.__surface) # Check if multiplayer or not if self.__numberOfPlayers == 0: self.__paddle = Paddle(self.__surface) else: self.__paddle = Paddle(self.__surface, True) def start_game(self): while self.__isRunning: # Check if game is being closed manually for event in pygame.event.get(): if event.type == pygame.QUIT: self.__isRunning = False # Check if game is done if self.__snake.game_over(): self.__isRunning = False mixer.music.stop() self.__play_gameover_sound() Tk().wm_withdraw() # Pop-up screen using tkinter library messagebox.showinfo( 'GAME OVER - You a dead snake bruv', 'I admit that I touched myself/walls :\'(') # Check if snake eats the food elif self.__snake.get_head().colliderect(self.__food.show_food()): self.__snake.set_length() self.__play_touch_sound() self.__food.update_food() # Check if the paddle touches the ball elif self.__paddle.get_paddle().colliderect( self.__ball.get_ball()): self.__ball.bounce() self.__play_touch_sound() # Check if one of the segments of the snake touches the ball for segment in self.__snake.get_snake(): if segment.colliderect(self.__ball.get_ball()): self.__ball.bounce() self.__play_touch_sound() # Continously update the game with the new values/info self.__update_game() def __update_game(self): self.__surface.fill((0, 0, 0)) self.__score = self.__ball.get_score() Playfield(self.__surface, self.__username, self.__score) self.__snake.update_snake() self.__food.look_for_food() self.__paddle.update_paddle() self.__ball.update_ball() pygame.display.flip() self.__clock.tick(60) # Check if sounds are activated or not and then play them accordingly def __play_touch_sound(self): if self.__soundsOn: touch = mixer.Sound("assets/touch_sound.wav") mixer.Sound.play(touch) def __play_gameover_sound(self): if self.__soundsOn: gameover = mixer.Sound("assets/game_over_sound.wav") mixer.Sound.play(gameover) # Code for testing the Game class def get_username(self): return self.__username def get_numberOfPlayers(self): return self.__numberOfPlayers
class GameEnv(object): def __init__(self, size): self.size = size self.height, self.width = size self.map_matrix = np.zeros((self.width, self.height)) self.fruitLocation = [ random.randint(0, self.width - 1), random.randint(0, self.height - 1) ] self.snake = Snake(self.height, self.width) self.frames = None self.direction = 0 self.score = 0 self.num_actions = 3 def isOver(self): if self.snake.collisionWithBoundry() | self.snake.collisionWithSelf(): return True return False def stepForward(self, inputDirection): self.snake.snakeStep(inputDirection) if self.snake.snake_head == self.fruitLocation: self.fruitLocation = [ random.randint(0, self.width - 1), random.randint(0, self.height - 1) ] self.snake.snake_pos.insert(0, list(self.snake.snake_head)) self.score += 1 else: self.snake.snake_pos.pop() self.snake.snake_pos.insert(0, list(self.snake.snake_head)) return def stepForwardWithReward(self, inputDirection): self.snake.snakeStep(inputDirection) if self.isOver(): return -1 if self.snake.snake_head == self.fruitLocation: self.fruitLocation = [ random.randint(0, self.width - 1), random.randint(0, self.height - 1) ] self.snake.snake_pos.insert(0, list(self.snake.snake_head)) self.score += 1 return len(self.snake.snake_pos) * 1 else: self.snake.snake_pos.pop() self.snake.snake_pos.insert(0, list(self.snake.snake_head)) return 0 def getLastFrames(self, num_frames): if self.frames == None: frames = [] mat = self.transferSnaketoArray() self.frames = [mat] * 4 else: mat = self.transferSnaketoArray() self.frames.pop() self.frames.insert(0, mat) return np.array(self.frames).reshape(num_frames, 20, 20) def transferSnaketoArray(self): # sanke - 1, apple - 2, bakcground = 0 matrix = np.zeros(self.size) for pos in self.snake.snake_pos: matrix[pos[0], pos[1]] = 1 matrix[self.fruitLocation[0], self.fruitLocation[1]] = 2 return matrix