def main_menu(): choosing = True while choosing: SCREEN.fill((152, 251, 152)) display_msg("Classic Snake Game", (0, 0, 0), (WIDTH / 3.2, HEIGHT / 5.5)) display_msg("s - Play", (0, 0, 0), (WIDTH / 3.2, HEIGHT / 3.5)) display_msg("n - Hamiltonian Cycle AI", (0, 0, 0), (WIDTH / 3.2, HEIGHT / 2.8)) display_msg("a - AI", (0, 0, 0), (WIDTH / 3.2, HEIGHT / 2.4)) display_msg("k - View Leaderboard", (0, 0, 0), (WIDTH / 3.2, HEIGHT / 2.05)) display_msg("e - Quit", (0, 0, 0), (WIDTH / 3.2, HEIGHT / 1.82)) pygame.display.flip() for event in pygame.event.get(): if event.type == pygame.QUIT: exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_e: exit() elif event.key == pygame.K_s: play(Snake(), Food()) choosing = False elif event.key == pygame.K_n: hamiltonian_cycle.hamiltonian_cycle(SCREEN, CLOCK, Snake(), Food(), WIDTH, HEIGHT) choosing = False elif event.key == pygame.K_a: a = AI(SCREEN, CLOCK, Snake(), Food(), WIDTH, HEIGHT) a.run() elif event.key == pygame.K_k: view_leaderboard() choosing = False
def __init__(self): self.screen = Screen() self.screen.setup(width=WIDTH, height=HEIGHT) self.screen.tracer(0) self.screen.onkeypress(self.snakeDown, "Down") self.screen.onkeypress(self.snakeUp, "Up") self.screen.onkeypress(self.snakeLeft, "Left") self.screen.onkeypress(self.snakeRight, "Right") self.screen.listen() self.artist = Turtle(visible=False) self.artist.up() self.artist.speed("slowest") self.pen = Turtle() self.pen.penup() self.pen.speed(0) self.pen.color("black") self.pen.hideturtle() self.pen.goto(0, HEIGHT/2 - 40) self.snake = Snake() self.food = Food() self.commandPending = False self.highScore = 0 self.writeScore()
def __init__(self, row_size=10, col_size=10): self.gameBoard = Board(row_size, col_size) self.player1 = Snake(Direction.LEFT, row_size // 2, col_size // 2) self.food = Food(1, 1, self.gameBoard, self.player1) self.all_sprites = [SnakeSprite(self.player1), FoodSprite(self.food)] self.frame_count = 0 self.player1Alive = True self.grow = False self.move = Direction.LEFT
class SnakeGameState(): """ SnakeGameState """ def __init__(self, row_size=10, col_size=10): self.gameBoard = Board(row_size, col_size) self.player1 = Snake(Direction.LEFT, row_size // 2, col_size // 2) self.food = Food(1, 1, self.gameBoard, self.player1) self.all_sprites = [SnakeSprite(self.player1), FoodSprite(self.food)] self.frame_count = 0 self.player1Alive = True self.grow = False self.move = Direction.LEFT def update(self): """ update """ self.update_frame_count() # Update fame state if update frame count == 0 (i.e on frame_count rollover) if not self.frame_count: if (self.player1.head[0] == self.food.position[0] and self.player1.head[1] == self.food.position[1]): self.grow = True self.food.eaten = True if self.grow: self.grow = False self.player1.grow(self.move) else: self.player1.move(self.move) self.player1.update() self.food.update() for sprite in self.all_sprites: sprite.update() def validMove(self): if (self.player1.head in self.player1.tail or self.player1.head[0] == -1 or self.player1.head[1] == -1 or self.player1.head[0] == self.gameBoard.rowSize or self.player1.head[1] == self.gameBoard.colSize): self.player1Alive = False def update_frame_count(self): self.frame_count = (self.frame_count + 1) % 10
def addFood(): x = random.randrange(size[0]) y = random.randrange(size[1]) x -= x % 20 y -= y % 20 food = Food(WHITE, x, y) foodGroup.add(food)
class Game(object): def __init__(self): self.screen = Screen() self.screen.setup(width=WIDTH, height=HEIGHT) self.screen.tracer(0) self.screen.onkeypress(self.snakeDown, "Down") self.screen.onkeypress(self.snakeUp, "Up") self.screen.onkeypress(self.snakeLeft, "Left") self.screen.onkeypress(self.snakeRight, "Right") self.screen.listen() self.artist = Turtle(visible=False) self.artist.up() self.artist.speed("slowest") self.pen = Turtle() self.pen.penup() self.pen.speed(0) self.pen.color("black") self.pen.hideturtle() self.pen.goto(0, HEIGHT/2 - 40) self.snake = Snake() self.food = Food() self.commandPending = False self.highScore = 0 self.writeScore() def snakeUp(self): if not self.commandPending: self.commandPending = True self.snake.moveUp() self.commandPending = False def snakeDown(self): if not self.commandPending: self.commandPending = True self.snake.moveDown() self.commandPending = False def snakeLeft(self): if not self.commandPending: self.commandPending = True self.snake.moveLeft() self.commandPending = False def snakeRight(self): if not self.commandPending: self.commandPending = True self.snake.moveRight() self.commandPending = False def nextFrame(self): self.artist.clear() #print ("x: " + str(self.snake.head.x) + " - y: " + str(self.snake.head.y)) if self.snake.checkFoodCollision(self.food): self.snake.eat() self.food.changeLocation() self.writeScore() elif self.snake.checkWallCollision() or self.snake.checkSelfCollision(): self.snake = Snake() self.food.changeLocation() self.writeScore() else: self.snake.moveOneStep(screen) self.food.drawSelf(self.artist) self.snake.drawSelf(self.artist) self.screen.update() self.screen.ontimer(lambda: self.nextFrame(), 100) def writeScore(self): self.updateScore() self.pen.clear() self.pen.write("Score: {} High Score: {}".format(self.snake.score, self.highScore), align="center", font=("Courier", 20, "normal")) def updateScore(self): if self.snake.score > self.highScore: self.highScore = self.snake.score
def create(self): for i in range(self.foodnum): self.yum[i] = Food() for i in range(self.sharpnum): self.ouch[i] = Sharp() self.comp(i)
import pygame from consts import * from snake import Snake, Food pygame.init() screen = pygame.display.set_mode((screen_width, screen_height)) clock = pygame.time.Clock() snake = Snake() food = Food() myfont = pygame.font.SysFont("monospace", 16) def drawGrid(): for y in range(0, int(grid_height)): for x in range(0, int(grid_width)): if (x + y) % 2 == 0: box = pygame.Rect(x * gridsize, y * gridsize, gridsize, gridsize) pygame.draw.rect(screen, blue, box) def drawSnake(): for pos in snake.positions: box = pygame.Rect((pos[0], pos[1]), (gridsize, gridsize)) pygame.draw.rect(screen, snake.color, box) pygame.draw.rect(screen, (93, 216, 228), box, 1) def drawFood(): box = pygame.Rect((food.position[0], food.position[1]),
# 創造一個pygame視窗並設定大小及標題 size = (700, 500) screen = pygame.display.set_mode(size) pygame.display.set_caption("好棒棒遊戲") # 設定一個開關供迴圈使用 done = False clock = pygame.time.Clock() snake = Snake(5) g = pygame.sprite.Group() for i in range(10): food = Food(WHITE, 300, 300) g.add(food) # -------- 主要的程式迴圈 ----------- while not done: # --- 事件迴圈 event loop for event in pygame.event.get(): # 從事件list中抓取事件 if event.type == pygame.QUIT: # 當使用者按下結束 done = True # 將done變數設為True-->while迴圈將會結束 # --- 程式的運算與邏輯 pressed = pygame.key.get_pressed() snake.move(pressed) snake.是否吃到食物(g)