def __init__(self, nw_tile, nh_tile): self.nw_tile = nw_tile # Set number of tile in x self.nh_tile = nh_tile # Set number of tile in y self.number_body = 3 # Set number of snake's body self.score = 0 # Set score of sanke self.db_save_size = 6 # Column size of data base self.id = 0 # index of database's column self.type = 1 self.x = 2 self.y = 3 self.s = 4 self.t = 5 self.enemy_dict = {} # Enemy snake dictionary self.client_state_dict = { 'OK': 0, 'notNStart': 1, 'notNExit': 2 } # KList state of client self.client_state = self.client_state_dict[ 'notNStart'] # State of cleint self.play_state = False # Playing state self.game_time = 0 # time of game self.tile_mng = TileManager(width, height, self.nw_tile, self.nh_tile) # Set TileManager self.snake = Snake(1, 1, self.tile_mng, green, SNAKE, raw_input("Enter : "), self.number_body) # Set my sanke self.apple = Apple(0, 0, self.tile_mng, red, 0) # Set apple self.hud = HUD() # Set HUD
def resetGame(mySnake): # Recreate Level 1 nibble.level = 1 global currentLevel currentLevel = Level(1, nibble.total_columns, nibble.total_lines) # Draw level nibble.drawLevel(currentLevel) # Zero out total score nibble.score = 0 # Reset snake's attributes mySnake.resetSnake(nibble.center_x, nibble.center_y) mySnake.lives = 3 # Zero the apples eaten global apples_eaten apples_eaten = 0 # Show the first Apple global apple apple = Apple(nibble.total_columns, nibble.total_lines, currentLevel, mySnake) pygame.draw.rect( nibble.screen, (0, 255, 0), pygame.Rect(apple.getX(), apple.getY(), mySnake.size, mySnake.size), 0)
def __init__(self): # Snake attributes self.x_start = PLAYABLE_AREA_WIDTH / 2 self.y_start = PLAYABLE_AREA_HEIGHT / 2 self.len = 1 self.is_alive = True self.time_left = 200 self.life_time = 0 self.score = 0 # Create vector of position and velocity self.head = Vector(self.x_start, self.y_start) # actual position self.vel = Vector(RIGHT_VECTOR.x, RIGHT_VECTOR.y) # Init snake with 4 body segments self.tail = [] self.tail.append(Vector(self.x_start - 30, self.y_start)) # Add last segment of body self.tail.append(Vector(self.x_start - 20, self.y_start)) # Add second segment of body self.tail.append(Vector(self.x_start - 10, self.y_start)) # Add first segment of body self.len += 3 # Increase a body length # Vision for input self.visions = [Vision() for _ in range(8)] self.visions_array = [] # Get DNA as snake's brain self.DNA = NeuralNetwork(INPUT_NODES, HIDDEN_NODES, OUTPUT_NODES) self.apple = Apple()
def eat(self): self.apple = Apple() self.gen_not_in_tail() self.time_left += 100 self.grow()
def __init__(self, size): self.size = size self.snake = Snake(self) self.apple = Apple(self) self.board = Board(self) self.board.updateBoard() self.keyListener = Listener(on_press=self.on_press) self.isGameRunning = True
def __init__(self): self.score = 0 self.snake = Snake(SCREEN_SIZE[0] // 2, SCREEN_SIZE[1] // 2) self.apple = Apple() self.apple.placeApple(self.snake.getBody(), SCREEN_SIZE[0], SCREEN_SIZE[1]) self.scorePosX = SCREEN_SIZE[0] // 2 - 10 self.eatingSound = pygame.mixer.Sound('Eating.ogg')
def __init__(self): self._display_surf = None self._image_surf = None self._apple_surf = None self._running = True self.board = Board(self.windowWidth, self.windowHeight) self.player = Player(3) self.apple = Apple(5,5)
def main(): # create fruit fr1 = Fruit("Pear", 3.0, 3.0) fr1.toString() print() # create a parametrized apple ap1 = Apple(5.0, 2.0, 'Gala', False) ap1.toString()
def restart(self, autostart=False): self.score = 0 self.snake = Snake() self.apple = Apple() self.stopped = not autostart # game not running (no steps made) self.finished = False # game is done and can't be resumed self.didRestart = True self.speed = SConfig.speed self.uiInstrText = 'press <SPACE> to start'
def eat_apple(self): is_collider, object_collider = self.collision.check_collision( self.snake) if self.collision.object_colision(self.snake, self.apple): del self.apple new_x, new_y = self.random_position() self.apple = Apple(self.screen, new_x, new_y) self.snake.grow_body(self.snake.get_position()[0], self.snake.get_position()[1])
def load(self): self.boundary = Boundary(self.screen, 0, 0) snake_position_x, snake_position_y = self.random_position() self.snake = Snake(self.screen, snake_position_x, snake_position_y) apple_position_x, apple_position_y = self.random_position() self.apple = Apple(self.screen, apple_position_x, apple_position_y) self.collision = Collision(self.boundary.get_boundaries())
def __init__(self, cnx=None): self._running = True self._displaySurface = None self._snakeHeadImageSurface = None self._snakeBodyImageSurface = None self._appleImageSurface = None #self._borderImageSurface = None self.thePlayer = SnakeHead() self.apple = Apple() self.cnx = cnx
def __init__(self): # tiles and pixels self._nb_tile_x = ConstantVariables.NB_COLUMN self._nb_tile_y = ConstantVariables.NB_ROW # elements self._snakes = [] self._apple = Apple(self._nb_tile_x - 1, self._nb_tile_y - 1) # other self._running = True
def __init__(self): pygame.init() pygame.display.set_caption("ugly snake ") pygame.mixer.init() self.play_backround_music() self.surface = pygame.display.set_mode((DX, DY)) self.surface.fill((255, 255, 255)) self.snake = Snake(self.surface, 1) self.snake.draw() self.apple = Apple(self.surface) self.apple.draw()
def __init__(self): self.width = 600 self.height = 600 self.player = Player() self.block = 10 self.background = (0, 0, 0) pygame.init() self.screen = pygame.display.set_mode((self.width, self.height)) self.running = True self.clock = pygame.time.Clock() self.apple = Apple() self.active = False Tk().wm_withdraw()
def getNewApple(self): retry = True while retry: retry = False x = randrange(self.width) y = randrange(self.height) for b in self.player.body: if b.x == x and b.y == y: retry = True break if retry: continue self.apple = Apple(x, y) if self.isAI: self.moves = self.setMove()
def __init__(self, width, height, occurrences, ai, shortest, display_moves): self.occurrences = occurrences self.player = Snake() # Instanciate a dummy apple self.apple = Apple(0, 1) self.width = width self.height = height self.isAI = ai print(self.isAI) self.moves = None self.score = 0 self.scores = [] self.display_moves = display_moves self.shortest = shortest if ai: self.moves = self.setMove() self.getNewApple()
def __init__(self, window_width, window_height): # tiles and pixels self._nb_tile_x = 60 self._tile_width = floor(window_width / self._nb_tile_x) self._width_px = self._tile_width * self._nb_tile_x self._nb_tile_y = floor(window_height / self._tile_width) self._height_px = self._tile_width * self._nb_tile_y # elements # screen self._screen = pygame.display.set_mode((self._width_px, self._height_px)) self._background = pygame.image.load('../assets/grass.jpg') self._background = pygame.transform.scale(self._background, (self._width_px, self._height_px)) # objects self._snake = Snake(self._nb_tile_x, self._nb_tile_y, self._tile_width) self._apple = Apple(self._nb_tile_x - 1, self._nb_tile_y - 1, self._tile_width) # other self._running = True
def on_init(self): self._running = True cols = 50 rows = 50 mapSizeX = 700 mapSizeY = 700 imageSize = (int(mapSizeX / cols), int(mapSizeY / rows)) self.map = Map(50, 50, mapSizeX, mapSizeY, cols, rows, self.headless) self.snake = Snake(cols / 2, rows / 2, imageSize, self.headless) self.apple = Apple(cols, rows, imageSize, self.headless) if self.bot: self.bot.AddGoal(self.apple) self.bot.AddBody(self.snake) self.bot.AddMap(self.map) if not self.headless: pygame.display.set_caption("Snake") self._display_surf = pygame.display.set_mode( self.size, pygame.DOUBLEBUF) self.font = pygame.font.SysFont('mono', 14, bold=True) self.fontEndscore = pygame.font.SysFont('mono', 32, bold=True)
def run(self): play_btn = Button(self.load_screen.win, RETRO_BLUE, 0.106125 * WIDTH, 0.702 * HEIGHT, size=100, on_click=self.check_play_screen, btn_text="Play") self.load_screen.add_game_object("play_btn", (10, play_btn)) exit_btn = Button(self.load_screen.win, RETRO_BLUE, 0.76375 * WIDTH, 0.702 * HEIGHT, size=100, on_click=self.exit_trigger, btn_text="Exit") self.load_screen.add_game_object("exit_btn", (11, exit_btn)) settings_btn = Button(self.load_screen.win, RETRO_BLUE, 0.39625 * WIDTH, 0.702 * HEIGHT, size=100, on_click=None, btn_text="Settings") self.load_screen.add_game_object("settings_btn", (12, settings_btn)) snake = Snake(self.play_screen.win) self.play_screen.add_game_object("snake", (10, snake)) score_box = TextBox(self.play_screen.win, NEON_YELLOW, 650, 100, "fonts/true-crimes.ttf", "Score: ", 50) self.play_screen.add_game_object("score_box", (8, score_box)) apple = Apple(self.play_screen.win, RETRO_PINK, 28) self.play_screen.add_game_object("apple", (9, apple)) while True: self.active_screen.screen_update()
def display(self): Apple()._shadow(self.scale_) Apple()._basic_apple(self.scale_)
print "python's dir: " + os.getcwd() print "script's dir: " + os.path.dirname(os.path.abspath(__file__)) # Create level currentLevel = Level(1, nibble.total_columns, nibble.total_lines) # Draw level nibble.drawLevel(currentLevel) nibble.displayMessage("Level" + str(nibble.level) + ", Push Space") nibble.drawLevel(currentLevel) # Create a Snake mySnake = Snake(nibble.center_x, nibble.center_y) # Show the first Apple apple = Apple(nibble.total_columns, nibble.total_lines, currentLevel, mySnake) pygame.draw.rect( nibble.screen, (0, 255, 0), pygame.Rect(apple.getX(), apple.getY(), mySnake.size, mySnake.size), 0) pygame.mixer.init(48000, -16, 1, 1024) sound_eat = pygame.mixer.Sound( os.path.dirname(os.path.abspath(__file__)) + '/hit.wav') sound_crash = pygame.mixer.Sound( os.path.dirname(os.path.abspath(__file__)) + '/sounds/crash.ogg') sound_game_over = pygame.mixer.Sound( os.path.dirname(os.path.abspath(__file__)) + '/sounds/reverse.ogg') def resetLevel(mySnake): # Recreate Level 1
def __init__(self): self.scale_ = 1.2 Apple().__init__()
def reset(self): self._apple = Apple(self.blockSize * randint(1, self.weight // self.blockSize - 1), # column 0 is reserved for snake's starting point self.blockSize * randint(0, self.height // self.blockSize - 1)) self._snake = Snake(0, self.blockSize * randint(0, self.height // self.blockSize - 1), self.blockSize) self._snake.length = 1 self._game = Game()
def __init__(self): self.scale_ = 2.4 Apple().__init__()
poder_ativado = False if personagem == 'hawking': blackhole_group.remove(blackhole) animacao = True timer_poder = 0 if personagem == 'einstein': FPS = 100 # spawn das maças if some >= FPS * 5: difuldade_timer += 1 if difuldade_timer == drop_interval: aleatorio = randrange(1, 101) if aleatorio <= 50: apple = Apple('Apples/apple.png', randrange(1, Ltela - 100), -40, randrange(1, dificuldade)) apple_group.add(apple) if aleatorio >= 51 and aleatorio <= 55: power_apple = Power_apple('Apples/blue_apple.png', randrange(1, Ltela - 100), -40, 3) power_apple_group.add(power_apple) if aleatorio >= 56 and aleatorio <= 89: if not poder_ativado and personagem == 'newton' or personagem != 'newton': #ativa rotten_apple rotten_apple = Rotten_apple('Apples/rotten_apple.png', randrange(1, Ltela - 100), -40, randrange(1, dificuldade)) rotten_apple_group.add(rotten_apple)
def gen_not_in_tail(self): while self.apple.pos in self.tail: self.apple = Apple()
from Snake import Snake pygame.init() if __name__ == "__main__": width = 700 height = 600 dsply = pygame.display.set_mode((width, height)) pygame.display.set_caption('Super Snake') bg = pygame.image.load("Assignment14/Snake-AI/assets/img/background.png") font = pygame.font.Font('Assignment14/Snake-AI/assets/font/ALGER.ttf', 32) clock = pygame.time.Clock() snake = Snake(dsply) apple = Apple(dsply) # Show Score Text text = font.render('Score: 0', True, (255, 255, 255)) textRect = text.get_rect() textRect.center = (width // 2, 30) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: exit() if not snake.x_move: if snake.x < apple.x or snake.x - 1 < apple.x or snake.x - 2 < apple.x or snake.x - 3 < apple.x or snake.x - 4 < apple.x: snake.direction = 'r' if snake.x == apple.x or snake.x - 1 == apple.x or snake.x - 2 == apple.x or snake.x - 3 == apple.x or snake.x - 4 == apple.x:
def loop(self, screen): screen.blit(self.text, self.textRect) screen.blit(self.text2, self.textRect2) clock = pygame.time.Clock() self.snake = Snake(self.display, -(Config['snake']['width'])) self.snake2 = Snake(self.display, (Config['snake']['width'])) apple = Apple(self.display) ai = Ai() x_change = Config['snake']['speed'] y_change = 0 x_change2 = -Config['snake']['speed'] y_change2 = 0 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT and x_change == 0: x_change = -Config['snake']['speed'] y_change = 0 elif event.key == pygame.K_RIGHT and x_change == 0: x_change = Config['snake']['speed'] y_change = 0 elif event.key == pygame.K_UP and y_change == 0: x_change = 0 y_change = -Config['snake']['speed'] elif event.key == pygame.K_DOWN and y_change == 0: x_change = 0 y_change = Config['snake']['speed'] if event.key == pygame.K_a and x_change2 == 0: x_change2 = -Config['snake']['speed'] y_change2 = 0 elif event.key == pygame.K_d and x_change2 == 0: x_change2 = Config['snake']['speed'] y_change2 = 0 elif event.key == pygame.K_w and y_change2 == 0: x_change2 = 0 y_change2 = -Config['snake']['speed'] elif event.key == pygame.K_s and y_change2 == 0: x_change2 = 0 y_change2 = Config['snake']['speed'] snake_rect = self.snake.draw(Config['colors']['blue']) snake_rect2 = self.snake2.draw(Config['colors']['green']) apple_rect = apple.draw() ai.update(pos=(snake_rect[0], snake_rect[1]), apple=(apple_rect[0], apple_rect[1]), size=self.snake.max_size) move = ai.action() bumper_x = Config['game']['width'] - Config['game']['bumper_size'] bumper_y = Config['game']['height'] - Config['game']['bumper_size'] if apple_rect.colliderect(snake_rect): apple.remove() apple.randomize() self.snake.eat() elif apple_rect.colliderect(snake_rect2): apple.remove() apple.randomize() self.snake2.eat() snakehit = self.snake.hit(self.snake2.body, bumper_x, bumper_y) snakehit2 = self.snake2.hit(self.snake.body, bumper_x, bumper_y) if (snakehit or snakehit2): if (snakehit and snakehit2): print("Tie") elif snakehit: self.snake2.score += 1 self.player2score += 1 else: self.snake.score += 1 self.player1score += 1 apple.remove() # snake.remove() self.map(screen) self.loop(screen) self.snake.move(x_change, y_change) self.snake2.move(x_change2, y_change2) pygame.display.update() clock.tick(Config['game']['fps'])
def loop(self): clock = pygame.time.Clock() snake = Snake(self.display) apple = Apple(self.display) x_change = 0 y_change = 0 self.score = 0 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: x_change = -Config['snake']['speed'] y_change = 0 elif event.key == pygame.K_RIGHT: x_change = Config['snake']['speed'] y_change = 0 elif event.key == pygame.K_UP: x_change = 0 y_change = -Config['snake']['speed'] elif event.key == pygame.K_DOWN: x_change = 0 y_change = Config['snake']['speed'] #Background and game area self.display.fill(Config['colors']['red']) pygame.draw.rect(self.display, Config['colors']['black'], [ Config['game']['bumper_size'], Config['game']['bumper_size'], Config['game']['height'] - Config['game']['bumper_size'] * 2, Config['game']['width'] - Config['game']['bumper_size'] * 2 ]) #apple apple_rect = apple.draw() #move the snake snake.move(x_change, y_change) #Draw the snake snake_rect = snake.draw() snake.draw_body() #collision bumper_x = Config['game']['width'] - Config['game']['bumper_size'] bumper_y = Config['game']['height'] - Config['game']['bumper_size'] if (snake.x_pos < Config['game']['bumper_size'] or snake.y_pos < Config['game']['bumper_size'] or snake.x_pos + Config['snake']['width'] > bumper_x or snake.y_pos + Config['snake']['height'] > bumper_y): self.loop() # Detect collision with apple if apple_rect.colliderect(snake_rect): apple.randomize() self.score += 1 snake.eat() # Collide with Self if len(snake.body) >= 1: for cell in snake.body: if snake.x_pos == cell[0] and snake.y_pos == cell[1]: self.loop() # Initialize font and draw title and score text pygame.font.init() font = pygame.font.Font('./assets/Now-Regular.otf', 28) score_text = 'Score: {}'.format(self.score) score = font.render(score_text, True, Config['colors']['white']) title = font.render('Snake Game', True, Config['colors']['white']) title_rect = title.get_rect(center=(Config['game']['width'] / 2, Config['game']['bumper_size'] / 2)) score_rect = score.get_rect( center=(500 / 2, Config['game']['height'] - Config['game']['bumper_size'] / 2)) self.display.blit(score, score_rect) self.display.blit(title, title_rect) pygame.display.update() clock.tick(Config['game']['fps'])