用Python写一个贪吃蛇游戏(代码体量有一点点大,我相信你一定会看完的),使用Pygame库实现:
```
import pygame
import random
# 初始化Pygame
pygame.init()
# 设置窗口大小
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
# 加载游戏资源
background_img = pygame.image.load('background.png').convert()
snake_head_img = pygame.image.load('snake_head.png').convert_alpha()
snake_body_img = pygame.image.load('snake_body.png').convert_alpha()
apple_img = pygame.image.load('apple.png').convert_alpha()
# 定义游戏元素
class Snake:
def __init__(self, x, y):
self.x = x
self.y = y
self.speed = 20
self.direction = 'right'
self.length = 3
self.body = [(self.x, self.y)]
self.img_head = snake_head_img
self.img_body = snake_body_img
self.rect_head = self.img_head.get_rect()
self.rect_body = self.img_body.get_rect()
def update(self):
if self.direction == 'right':
self.x += self.speed
elif self.direction == 'left':
self.x -= self.speed
elif self.direction == 'up':
self.y -= self.speed
elif self.direction == 'down':
self.y += self.speed
self.body.insert(0, (self.x, self.y))
if len(self.body) > self.length:
self.body.pop()
self.rect_head.x = self.x
self.rect_head.y = self.y
def draw(self):
screen.blit(self.img_head, (self.x, self.y))
for x, y in self.body[1:]:
screen.blit(self.img_body, (x, y))
def eat_apple(self, apple):
if self.rect_head.colliderect(apple.rect):
self.length += 1
return True
return False
def check_collision(self):
if self.x < 0 or self.x > screen_width - self.rect_head.width:
return True
if self.y < 0 or self.y > screen_height - self.rect_head.height:
return True
for x, y in self.body[1:]:
if self.x == x and self.y == y:
return True
return False
class Apple:
def __init__(self, x, y):
self.x = x
self.y = y
self.img = apple_img
self.rect = self.img.get_rect()
self.rect.x = self.x
self.rect.y = self.y
def draw(self):
screen.blit(self.img, (self.x, self.y))
# 创建贪吃蛇和苹果
snake = Snake(400, 300)
apple = Apple(random.randint(0, screen_width - 40), random.randint(0, screen_height - 40))
# 游戏循环
clock = pygame.time.Clock()
font = pygame.font.SysFont(None, 50)
while True:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT and snake.direction != 'left':
snake.direction = 'right'
elif event.key == pygame.K_LEFT and snake.direction != 'right':
snake.direction = 'left'
elif event.key == pygame.K_UP and snake.direction != 'down':
snake.direction = 'up'
elif event.key == pygame.K_DOWN and snake.direction != 'up':
snake.direction = 'down'
# 更新游戏元素
snake.update()
# 检测碰撞
if snake.check_collision():
pygame.quit()
exit()
# 检测吃到苹果
if snake.eat_apple(apple):
apple.x = random.randint(0, screen_width - 40)
apple.y = random.randint(0, screen_height - 40)
apple.rect.x = apple.x
apple.rect.y = apple.y
# 绘制游戏元素
screen.blit(background_img, (0, 0))
snake.draw()
apple.draw()
score_text = font.render('Score: ' + str(snake.length - 3), True, (255, 255, 255))
screen.blit(score_text, (10, 10))
# 刷新屏幕
pygame.display.update()
clock.tick(10)
```
在上面的示例程序中,我们创建了`Snake`和`Apple`两个类来表示贪吃蛇和苹果。我们使用Pygame库来处理游戏的图形界面和事件处理。在游戏循环中,我们不断更新贪吃蛇和苹果的位置,并检测碰撞和吃到苹果等情况,然后绘制游戏元素和分数等信息。
看一下效果:
6不6啊[抠鼻]
你可以根据自己的需求修改上面的代码,添加音效、难度等设置,让游戏更加有趣。祝你写出一个你以为的贪吃蛇游戏![可爱]
页面更新:2024-03-01
本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828
© CopyRight 2008-2024 All Rights Reserved. Powered By bs178.com 闽ICP备11008920号-3
闽公网安备35020302034844号