Python是一种功能强大且易于学习的编程语言,非常适合初学者学习编程。今天,我将带大家一步步制作一个简单有趣的图形界面小游戏——吃苹果游戏。通过这个项目,你将学会如何使用Python的pygame库来创建一个基本的游戏。
一、准备工作
首先,我们需要安装pygame库。打开命令提示符(Windows)或终端(Mac/Linux),输入以下命令来安装pygame:
pip install pygame
安装完成后,我们就可以开始编写游戏代码了。
二、编写游戏代码
打开一个你喜欢的文本编辑器(例如Notepad、Sublime Text、VSCode等),将下面的代码复制粘贴到编辑器中。
import pygame
import random
# 初始化 Pygame
pygame.init()
# 设置屏幕大小
screen_width = 600
screen_height = 400
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)
# 设置游戏时钟
clock = pygame.time.Clock()
# 设置蛇的初始位置和大小
snake_block = 10
snake_speed = 15
snake_list = []
snake_length = 1
# 设置苹果的初始位置
apple_size = 10
apple_x = round(random.randrange(0, screen_width - apple_size) / 10.0) * 10.0
apple_y = round(random.randrange(0, screen_height - apple_size) / 10.0) * 10.0
# 蛇的初始位置
snake_x = screen_width / 2
snake_y = screen_height / 2
# 蛇的移动方向
snake_x_change = 0
snake_y_change = 0
# 结束游戏的标志
game_over = False
game_close = False
# 显示分数
def show_score(score):
font = pygame.font.SysFont(None, 35)
value = font.render("Score: " + str(score), True, black)
screen.blit(value, [0, 0])
# 画蛇
def draw_snake(snake_block, snake_list):
for x in snake_list:
pygame.draw.rect(screen, black, [x[0], x[1], snake_block, snake_block])
# 主游戏循环
while not game_over:
while game_close:
screen.fill(white)
font = pygame.font.SysFont(None, 55)
message = font.render("You Lost! Press Q-Quit or C-Play Again", True, red)
screen.blit(message, [screen_width / 6, screen_height / 3])
show_score(snake_length - 1)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
game_over = True
game_close = False
if event.key == pygame.K_c:
snake_list = []
snake_length = 1
snake_x = screen_width / 2
snake_y = screen_height / 2
snake_x_change = 0
snake_y_change = 0
apple_x = round(random.randrange(0, screen_width - apple_size) / 10.0) * 10.0
apple_y = round(random.randrange(0, screen_height - apple_size) / 10.0) * 10.0
game_close = False
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:
snake_x_change = -snake_block
snake_y_change = 0
elif event.key == pygame.K_RIGHT:
snake_x_change = snake_block
snake_y_change = 0
elif event.key == pygame.K_UP:
snake_y_change = -snake_block
snake_x_change = 0
elif event.key == pygame.K_DOWN:
snake_y_change = snake_block
snake_x_change = 0
if snake_x >= screen_width or snake_x < 0 or snake_y >= screen_height or snake_y < 0:
game_close = True
snake_x += snake_x_change
snake_y += snake_y_change
screen.fill(white)
pygame.draw.rect(screen, green, [apple_x, apple_y, apple_size, apple_size])
snake_head = []
snake_head.append(snake_x)
snake_head.append(snake_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(snake_block, snake_list)
show_score(snake_length - 1)
pygame.display.update()
if snake_x == apple_x and snake_y == apple_y:
apple_x = round(random.randrange(0, screen_width - apple_size) / 10.0) * 10.0
apple_y = round(random.randrange(0, screen_height - apple_size) / 10.0) * 10.0
snake_length += 1
clock.tick(snake_speed)
pygame.quit()
quit()
三、运行游戏
将上述代码保存为snake_game.py文件。然后,在命令提示符(Windows)或终端(Mac/Linux)中导航到保存该文件的目录,运行以下命令启动游戏:
python snake_game.py
四、游戏玩法
使用方向键控制蛇的移动方向。
吃掉绿色的苹果,蛇会变长,得分也会增加。
如果蛇撞到墙壁或者自身,游戏结束。
五、总结
通过这个简单的小游戏,我们学习了如何使用pygame库来创建图形界面游戏。希望你在编写和玩这个游戏的过程中,能够体验到编程的乐趣并激发你的创造力!
如果你有任何问题或改进建议,欢迎在评论区留言。祝大家玩得开心!