能給我寫一個貪吃蛇游戲代碼嗎?
當然可以!以下是一個簡單的貪吃蛇游戲的 Python 代碼示例:
import pygame import random
游戲窗口大小
WINDOW_WIDTH = 800 WINDOW_HEIGHT = 600
蛇身和食物大小
CELL_SIZE = 20
顏色定義
BLACK = (0, 0, 0) GREEN = (0, 255, 0) RED = (255, 0, 0)
初始化 Pygame
pygame.init() window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)) pygame.display.set_caption("貪吃蛇游戲")
蛇類
class Snake: def init(self): self.body = [(WINDOW_WIDTH/2, WINDOW_HEIGHT/2)] self.direction = random.choice(["UP", "DOWN", "LEFT", "RIGHT"])
python
復制代碼
def move(self): x, y = self.body[0] if self.direction == "UP": y -= CELL_SIZE elif self.direction == "DOWN": y += CELL_SIZE elif self.direction == "LEFT": x -= CELL_SIZE elif self.direction == "RIGHT": x += CELL_SIZE self.body.insert(0, (x, y)) self.body.pop() def change_direction(self, direction): if direction == "UP" and self.direction != "DOWN": self.direction = direction elif direction == "DOWN" and self.direction != "UP": self.direction = direction elif direction == "LEFT" and self.direction != "RIGHT": self.direction = direction elif direction == "RIGHT" and self.direction != "LEFT": self.direction = direction def draw(self): for x, y in self.body: pygame.draw.rect(window, GREEN, (x, y, CELL_SIZE, CELL_SIZE)) def check_collision(self): x, y = self.body[0] if x < 0 or x >= WINDOW_WIDTH or y < 0 or y >= WINDOW_HEIGHT: return True for body_part in self.body[1:]: if body_part == (x, y): return True return False def eat_food(self, food): if self.body[0] == food.position: self.body.append((food.position[0], food.position[1])) food.generate()
食物類
class Food: def init(self): self.position = ( random.randint(0, (WINDOW_WIDTH-CELL_SIZE) // CELL_SIZE) * CELL_SIZE, random.randint(0, (WINDOW_HEIGHT-CELL_SIZE) // CELL_SIZE) * CELL_SIZE )
ruby
復制代碼
def draw(self): pygame.draw.rect(window, RED, (self.position[0], self.position[1], CELL_SIZE, CELL_SIZE)) def generate(self): self.position = ( random.randint(0, (WINDOW_WIDTH-CELL_SIZE) // CELL_SIZE) * CELL_SIZE, random.randint(0, (WINDOW_HEIGHT-CELL_SIZE) // CELL_SIZE) * CELL_SIZE )
snake = Snake() food = Food()
clock = pygame.time.Clock()
游戲主循環(huán)
running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: snake.change_direction("UP") elif event.key == pygame.K_DOWN: snake.change_direction("DOWN") elif event.key == pygame.K_LEFT: snake.change_direction("LEFT") elif event.key == pygame.K_RIGHT: snake.change_direction("RIGHT")
scss
復制代碼
snake.move() if snake.check_collision(): running = False snake.eat_food(food) window.fill(BLACK) snake.draw() food.draw() pygame.display.flip() clock.tick(10)
pygame.quit()
這段代碼使用了 Pygame 庫來實現(xiàn)游戲窗口和圖形的繪制,以及捕獲鍵盤事件。貪吃蛇的移動、吃食物和碰撞檢測等邏輯也在代碼中實現(xiàn)。你可以將代碼復制到 Python 環(huán)境中執(zhí)行,就可以玩這個簡單的貪吃蛇游戲了。(附帶圖片)
人工智能真的要改變我們未來的生活環(huán)境了,如還在排斥著不去接收新鮮事物。未來淘汰的就是我們這些不去主動學習,不去接收新鮮事物的一群人。
-
IT
+關(guān)注
關(guān)注
2文章
845瀏覽量
63443 -
人工智能
+關(guān)注
關(guān)注
1789文章
46652瀏覽量
237060 -
代碼
+關(guān)注
關(guān)注
30文章
4722瀏覽量
68229 -
python
+關(guān)注
關(guān)注
55文章
4767瀏覽量
84375 -
ChatGPT
+關(guān)注
關(guān)注
29文章
1546瀏覽量
7353
發(fā)布評論請先 登錄
相關(guān)推薦
評論