大家好啊,今天我就来跟大家分享一下用Python和Pygame制作贪吃蛇的游戏吧。
1.导入所需模块
首先,这个游戏用了pygame、random、time模块,而这些都不是Python标准库的内容,所以我们需要导入这些模块。在这些模块里面,pygame模块是一个第三方模块,需要用pip另外安装。
安装pygame模块:
pip install pygame
如果你不放心,你可以在终端输入:
python.exe
然后再输入:
import pygame
导入pygame、random、time模块:
import pygame
import time
import random
2.初始化pygame模块
pygame模块中的一些模块需要在初始化后才能使用,否则会引发pygame.error,而这里我们可以把这么多模块的初始化写成:
pygame.init()
程序:
import pygame
import time
import random
pygame.init()
3.定义颜色常量
在这个游戏中我们需要绘制字体以及绘制一些图形,而绘制这些东西都要设置颜色,但是pygame中只有使用RGB表示颜色的功能,为了方便使用,我们需要定义一些颜色常量,由于常量在Python中一般使用大写字母表示常量,所以我们就用BLACK、WHITE、RED、GREEN、BLUE作为这些颜色常量的变量名。
import pygame
import time
import random
pygame.init()
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
4.定义变量
在游戏中,不仅有一直不变的量,还有变化的量,也就是变量。现在我们就需要定义一些变量,如screen(窗口)、clock(帧率)等。
import pygame
import time
import random
pygame.init()
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("贪吃蛇游戏")
clock = pygame.time.Clock()
snake_block_size = 20
font_style = pygame.font.SysFont('kaiti', 50)
score_font = pygame.font.SysFont('kaiti', 35)
5.定义基础函数
现在就需要定义一些基础函数以便以后使用。
import pygame
import time
import random
pygame.init()
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("贪吃蛇游戏")
clock = pygame.time.Clock()
snake_block_size = 20
font_style = pygame.font.SysFont('kaiti', 50)
score_font = pygame.font.SysFont('kaiti', 35)
def your_score(score):
value = score_font.render("Your Score: " + str(score), True, WHITE)
screen.blit(value, [0, 0])
def our_snake(snake_block_size, snake_list):
for x in snake_list:
pygame.draw.rect(screen, GREEN, [x[0], x[1], snake_block_size, snake_block_size])
6.主函数
最后就需要定义主函数了。
import pygame
import time
import random
pygame.init()
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("贪吃蛇游戏")
clock = pygame.time.Clock()
snake_block_size = 20
font_style = pygame.font.SysFont('kaiti', 50)
score_font = pygame.font.SysFont('kaiti', 35)
def your_score(score):
value = score_font.render("Your Score: " + str(score), True, WHITE)
screen.blit(value, [0, 0])
def our_snake(snake_block_size, snake_list):
for x in snake_list:
pygame.draw.rect(screen, GREEN, [x[0], x[1], snake_block_size, snake_block_size])
def game_loop():
game_over = False
game_quit = False
x1 = screen_width / 2
y1 = screen_height / 2
x1_change = 0
y1_change = 0
snake_list = []
snake_length = 1
foodx = round(random.randrange(0, screen_width - snake_block_size) / 20.0) * 20.0
foody = round(random.randrange(0, screen_height - snake_block_size) / 20.0) * 20.0
while not game_quit:
while game_over == True:
screen.fill(BLACK)
message = font_style.render("游戏结束!按Q/QUIT退出,按C复活", True, WHITE)
rect = message.get_rect()
rect.centerx = 400
rect.centery = 300
screen.blit(message, rect)
your_score(snake_length - 1)
pygame.display.update()
# 游戏结束等待用户输入
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_quit = True
game_over = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
game_quit = True
game_over = False
if event.key == pygame.K_c:
game_loop()
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_quit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x1_change = -snake_block_size
y1_change = 0
elif event.key == pygame.K_RIGHT:
x1_change = snake_block_size
y1_change = 0
elif event.key == pygame.K_UP:
y1_change = -snake_block_size
x1_change = 0
elif event.key == pygame.K_DOWN:
y1_change = snake_block_size
x1_change = 0
if x1 >= screen_width or x1 < 0 or y1 >= screen_height or y1 < 0:
game_over = True
x1 += x1_change
y1 += y1_change
screen.fill(BLACK)
pygame.draw.rect(screen, RED, [foodx, foody, snake_block_size, snake_block_size])
snake_head = []
snake_head.append(x1)
snake_head.append(y1)
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_over = True
our_snake(snake_block_size, snake_list)
your_score(snake_length - 1)
pygame.display.update()
if x1 == foodx and y1 == foody:
foodx = round(random.randrange(0, screen_width - snake_block_size) / 20.0) * 20.0
foody = round(random.randrange(0, screen_height - snake_block_size) / 20.0) * 20.0
snake_length += 1
clock.tick(10)
pygame.quit()
quit()
7.最终程序
最后,上最终代码:
import pygame
import time
import random
pygame.init()
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("贪吃蛇游戏")
clock = pygame.time.Clock()
snake_block_size = 20
font_style = pygame.font.SysFont('kaiti', 50)
score_font = pygame.font.SysFont('kaiti', 35)
def your_score(score):
value = score_font.render("Your Score: " + str(score), True, WHITE)
screen.blit(value, [0, 0])
def our_snake(snake_block_size, snake_list):
for x in snake_list:
pygame.draw.rect(screen, GREEN, [x[0], x[1], snake_block_size, snake_block_size])
def game_loop():
game_over = False
game_quit = False
x1 = screen_width / 2
y1 = screen_height / 2
x1_change = 0
y1_change = 0
snake_list = []
snake_length = 1
foodx = round(random.randrange(0, screen_width - snake_block_size) / 20.0) * 20.0
foody = round(random.randrange(0, screen_height - snake_block_size) / 20.0) * 20.0
while not game_quit:
while game_over == True:
screen.fill(BLACK)
message = font_style.render("游戏结束!按Q/QUIT退出,按C复活", True, WHITE)
rect = message.get_rect()
rect.centerx = 400
rect.centery = 300
screen.blit(message, rect)
your_score(snake_length - 1)
pygame.display.update()
# 游戏结束等待用户输入
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_quit = True
game_over = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
game_quit = True
game_over = False
if event.key == pygame.K_c:
game_loop()
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_quit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x1_change = -snake_block_size
y1_change = 0
elif event.key == pygame.K_RIGHT:
x1_change = snake_block_size
y1_change = 0
elif event.key == pygame.K_UP:
y1_change = -snake_block_size
x1_change = 0
elif event.key == pygame.K_DOWN:
y1_change = snake_block_size
x1_change = 0
if x1 >= screen_width or x1 < 0 or y1 >= screen_height or y1 < 0:
game_over = True
x1 += x1_change
y1 += y1_change
screen.fill(BLACK)
pygame.draw.rect(screen, RED, [foodx, foody, snake_block_size, snake_block_size])
snake_head = []
snake_head.append(x1)
snake_head.append(y1)
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_over = True
our_snake(snake_block_size, snake_list)
your_score(snake_length - 1)
pygame.display.update()
if x1 == foodx and y1 == foody:
foodx = round(random.randrange(0, screen_width - snake_block_size) / 20.0) * 20.0
foody = round(random.randrange(0, screen_height - snake_block_size) / 20.0) * 20.0
snake_length += 1
clock.tick(10)
pygame.quit()
quit()
game_loop()
大家再见