赞
踩
下面是一个简单的贪吃蛇游戏的Python代码示例,使用了Pygame库:
```python
import pygame
import time
import random
# 初始化Pygame
pygame.init()
# 设置屏幕大小和标题
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('贪吃蛇')
# 定义颜色
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
# 设置蛇和食物的大小
block_size = 20
# 设置蛇的移动速度
snake_speed = 15
# 定义字体
font_style = pygame.font.SysFont(None, 50)
# 定义得分显示函数
def show_score(score):
score_text = font_style.render("得分: " + str(score), True, black)
screen.blit(score_text, [0, 0])
# 定义蛇的绘制函数
def draw_snake(block_size, snake_list):
for x in snake_list:
pygame.draw.rect(screen, green, [x[0], x[1], block_size, block_size])
# 主函数
def gameLoop():
game_over = False
game_close = False
# 初始化蛇的起始位置和长度
lead_x = screen_width / 2
lead_y = screen_height / 2
lead_x_change = 0
lead_y_change = 0
snake_list = []
snake_length = 1
# 初始化食物位置
food_x = round(random.randrange(0, screen_width - block_size) / block_size) * block_size
food_y = round(random.randrange(0, screen_height - block_size) / block_size) * block_size
# 游戏循环
while not game_over:
while game_close == True:
screen.fill(white)
message = font_style.render("游戏结束!按Q退出,按C重新开始", True, black)
screen.blit(message, [screen_width / 5, screen_height / 3])
show_score(snake_length - 1)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
game_close = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
game_over = True
game_close = False
if event.key == pygame.K_c:
gameLoop()
# 控制蛇的移动
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
lead_x_change = -block_size
lead_y_change = 0
elif event.key == pygame.K_RIGHT:
lead_x_change = block_size
lead_y_change = 0
elif event.key == pygame.K_UP:
lead_y_change = -block_size
lead_x_change = 0
elif event.key == pygame.K_DOWN:
lead_y_change = block_size
lead_x_change = 0
# 边界判断
if lead_x >= screen_width or lead_x < 0 or lead_y >= screen_height or lead_y < 0:
game_close = True
lead_x += lead_x_change
lead_y += lead_y_change
screen.fill(black)
# 绘制食物
pygame.draw.rect(screen, red, [food_x, food_y, block_size, block_size])
snake_head = []
snake_head.append(lead_x)
snake_head.append(lead_y)
snake_list.append(snake_head)
# 控制蛇的长度
if len(snake_list) > snake_length:
del snake_list[0]
for x in snake_list[:-1]:
if x == snake_head:
game_close = True
draw_snake(block_size, snake_list)
show_score(snake_length - 1)
pygame.display.update()
# 如果蛇吃到了食物
if lead_x == food_x and lead_y == food_y:
food_x = round(random.randrange(0, screen_width - block_size) / block_size) * block_size
food_y = round(random.randrange(0, screen_height - block_size) / block_size) * block_size
snake_length += 1
# 设置蛇的移动速度
clock = pygame.time.Clock()
clock.tick(snake_speed)
pygame.quit()
quit()
gameLoop()
```
这是一个简单的贪吃蛇游戏,使用了Pygame库来实现图形化界面和游戏逻辑。你可以将这段代码保存为一个`.py`文件,然后在你的计算机上运行它。希望你喜欢!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。