python 双人贪吃蛇代码
时间: 2024-02-19 16:56:29 浏览: 169
python 贪吃蛇代码
5星 · 资源好评率100%
当然可以!以下是一个简单的Python双人贪吃蛇戏的代码示例:
```python
import pygame
import random
# 游戏窗口大小
WIDTH = 800
HEIGHT = 600
# 蛇身和食物大小
BLOCK_SIZE = 20
# 颜色定义
BLACK = (0, 0, 0)
WHITE = (255, 255, )
RED = (255, 0, 0# 初始化pygame
.init()
# 创建游戏窗口
window pygame.display.set_modeWIDTH, HEIGHT))
pygame.display.set_caption("双人贪吃蛇")
# 定义蛇的类
class Snake:
def __init__(self, color, x, y):
self.color = color
self.x = x
self.y y
self.direction = "right"
self.body = []
def move(self):
if self.direction == "up":
self.y -= BLOCK_SIZE
elif self.direction == "down":
self.y += BLOCK_SIZE
elif self.direction == "left":
self.x -= BLOCK_SIZE
elif self.direction == "right":
self.x += BLOCK_SIZE
def draw(self):
for segment in self.body pygame.draw.rect(window, self.color, [segment[0], segment[1], BLOCK_SIZE, BLOCK_SIZE])
def check_collision(self):
if self.x >= WIDTH or self.x < 0 or self.y >= HEIGHT or self.y < 0:
return True
for segment in self.body[1:]:
if segment[0] == self.x and segment[1] == self.y:
return True
return False
def eat_food(self, food):
if self.x == food.x and self.y == food.y:
return True
return False
def grow(self):
self.body.insert(0, (self.x, self.y))
def update(self):
self.move()
self.body.insert(0, (self.x, self.y))
if len(self.body) > 1:
self.body.pop()
# 定义食物的类
class Food:
def __init__(self):
self.x = random.randint(0, WIDTH // BLOCK_SIZE - 1) * BLOCK_SIZE
self.y = random.randint(0, HEIGHT // BLOCK_SIZE - 1) * BLOCK_SIZE
def draw(self):
pygame.draw.rect(window, RED, [self.x, self.y, BLOCK_SIZE, BLOCK_SIZE])
# 创建两条蛇和一个食物
snake1 = Snake(WHITE, WIDTH // 4, HEIGHT // 2)
snake2 = Snake(WHITE, WIDTH * 3 // 4, HEIGHT // 2)
food = Food()
# 游戏主循环
running = True
clock = pygame.time.Clock()
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_w and snake1.direction != "down":
snake1.direction = "up"
elif event.key == pygame.K_s and snake1.direction != "up":
snake1.direction = "down"
elif event.key == pygame.K_a and snake1.direction != "right":
snake1.direction = "left"
elif event.key == pygame.K_d and snake1.direction != "left":
snake1.direction = "right"
elif event.key == pygame.K_UP and snake2.direction != "down":
snake2.direction = "up"
elif event.key == pygame.K_DOWN and snake2.direction != "up":
snake2.direction = "down"
elif event.key == pygame.K_LEFT and snake2.direction != "right":
snake2.direction = "left"
elif event.key == pygame.K_RIGHT and snake2.direction != "left":
snake2.direction = "right"
window.fill(BLACK)
snake1.update()
snake1.draw()
snake2.update()
snake2.draw()
food.draw()
if snake1.check_collision() or snake2.check_collision():
running = False
if snake1.eat_food(food):
snake1.grow()
food = Food()
if snake2.eat_food(food):
snake2.grow()
food = Food()
pygame.display.update()
clock.tick(10)
# 退出游戏
pygame.quit()
```
这是一个使用pygame库实现的简单双人贪吃蛇游戏。其中,Snake类表示蛇,包含移动、绘制、碰撞检测等方法;Food类表示食物,包含随机生成位置和绘制方法。游戏主循环中,根据键盘事件改变蛇的移动方向,更新蛇和食物的状态,并进行碰撞检测和绘制。
阅读全文