今天我们开始Pygame的第六课,前几节课的内容在这里【点我】,欢迎大家前去考古:
今天我们一起来学习制作一个小游戏【贪吃蛇】,这是一个非常经典的小游戏,那么我们一起开始吧
1、游戏准备工作
import pygame, random,os
os.environ["SDL_VIDEO_WINDOW_POS"]="100,100"
pygame.init()
time_clock = pygame.time.Clock()
sc = pygame.display.set_mode((640, 480))
pygame.display.set_caption("贪吃蛇")
font = pygame.font.SysFont("宋体", 48 , True)
pink = (255, 182, 193)
violet = (238,130,238)
white = (255, 255, 255)
green = (0,255,0)
red = (255,0,0)
这里的代码每一行都加了注释
os.environ["SDL_VIDEO_WINDOW_POS"]="100,100"
:这一行是指运行代码时,Pygame以左上角 的初始位置
time_clock = pygame.time.Clock()
: 创建一个Clock对象用于控制游戏的速度
2、接下来我们创建一个 【贪吃蛇】类
class Snake():
def __init__(self):
self.direction = "right"
self.body = [[100, 100], [80, 100]]
self.head = list(self.body[0])
def draw_me(self):
for b in self.body:
pygame.draw.rect(sc, green, (b[0], b[1], 20, 20))
def move_head(self):
if self.direction == "right":
self.head[0] += 20
elif self.direction == "left":
self.head[0] -= 20
elif self.direction == "up":
self.head[1] -= 20
elif self.direction == "down":
self.head[1] += 20
def add_body(self):
self.body.insert(0, list(self.head))
def cut_tail(self):
self.body.pop()
def move_snake(self):
self.add_body()
self.cut_tail()
代码注释:
self.direction = "right"
:指【贪吃蛇】出生时的初始方向
self.body = [[100, 100], [80, 100]]
:初始的小方格坐标(以左上角为原点)
self.head = list(self.body[0])
# 蛇头位置,蛇头的坐标
函数【draw_me
】,利用*pygame.draw.rect(sc, green, (b[0], b[1], 20, 20))
*,这个在第三节课中有讲过:点我复习第三节课
函数【move_head
】,移动函数,左右移动和X轴有关系,所以代码里取头的*head[0]
,上下移动和Y轴有关系,所以代码里取头的head[1]
*
函数【add_body
】:增加身体的小方格,这个是吃到食物调用的函数
函数【cut_tail
】:这个是删除尾巴的小方格
函数【move_snake
】,这个函数中调用了【add_body
,cut_tail
】,头加一个,尾巴减一个,头加一个,尾巴减一个,头加一个,尾巴减一个…,这就是移动
3、接下来我们创建一个 【贪吃蛇】类
class Food():
def __init__(self):
self.color=white
x = random.randrange(0, 640,20)
y = random.randrange(0, 480,20)
self.postion = [x,y]
def draw_me(self):
pygame.draw.rect(sc, self.color, (self.postion[0], self.postion[1], 20, 20))
def reset(self):
x = random.randrange(0, 640,20)
y = random.randrange(0, 480,20)
self.postion = [x,y]
代码注释
self.color=white
,食物初始颜色是白色
x = random.randrange(0, 640,20)
,y = random.randrange(0, 480,20)
,这是指食物不可超过游戏的界面大小,X轴在0到640步长为20,Y轴上为0到480步长为20,都是随机的
知识进阶-双倍奖励 self.color=random.choice([white,pink])
,颜色可随机挑选
4、收尾
food = Food()
snake = Snake()
while True:
len_text = font.render("Length: "+str(len(snake.body)),True,red)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
if snake.direction != "left":
snake.direction = "right"
if event.key == pygame.K_LEFT:
if snake.direction != "right":
snake.direction = "left"
if event.key == pygame.K_UP:
if snake.direction != "down":
snake.direction = "up"
if event.key == pygame.K_DOWN:
if snake.direction != "up":
snake.direction = "down"
if snake.head == food.postion:
food.reset()
snake.add_body()
'''
#知识进阶-双倍奖励
if food.color==white:
food.reset()
snake.add_body()
else:
food.reset()
snake.add_body()
snake.add_body()
'''
if snake.head[0] > 620 or snake.head[0] < 0:
break
if snake.head[1] > 460 or snake.head[1] < 0:
break
snake.move_head()
snake.move_snake()
sc.fill(violet)
snake.draw_me()
food.draw_me()
sc.blit(len_text ,(50,20))
pygame.display.update()
time_clock.tick(3)
len_text = font.render("GAME OVER",True,red)
sc.blit(len_text ,(200,200))
pygame.display.update()
input()
代码注释
# 判断是否碰到边缘,这是判断小蛇是否移动出窗口的外面,否则就停止游戏
if snake.head[0] > 620 or snake.head[0] < 0:
break
if snake.head[1] > 460 or snake.head[1] < 0:
break
sc.fill(violet):游戏背景颜色,可以自己更换
time_clock.tick(3): 控制游戏速度 控制游戏速度,越大越快
5、完整版代码:
import pygame, random,os
os.environ["SDL_VIDEO_WINDOW_POS"]="100,100"
pygame.init()
time_clock = pygame.time.Clock()
sc = pygame.display.set_mode((640, 480))
pygame.display.set_caption("贪吃蛇")
font = pygame.font.SysFont("宋体", 48 , True)
pink = (255, 182, 193)
violet = (238,130,238)
white = (255, 255, 255)
green = (0,255,0)
red = (255,0,0)
class Snake():
def __init__(self):
self.direction = "right"
self.body = [[100, 100], [80, 100]]
self.head = list(self.body[0])
def draw_me(self):
for b in self.body:
pygame.draw.rect(sc, green, (b[0], b[1], 20, 20))
def move_head(self):
if self.direction == "right":
self.head[0] += 20
elif self.direction == "left":
self.head[0] -= 20
elif self.direction == "up":
self.head[1] -= 20
elif self.direction == "down":
self.head[1] += 20
def add_body(self):
self.body.insert(0, list(self.head))
def cut_tail(self):
self.body.pop()
def move_snake(self):
self.add_body()
self.cut_tail()
class Food():
def __init__(self):
self.color=white
x = random.randrange(0, 640,20)
y = random.randrange(0, 480,20)
self.postion = [x,y]
def draw_me(self):
pygame.draw.rect(sc, self.color, (self.postion[0], self.postion[1], 20, 20))
def reset(self):
x = random.randrange(0, 640,20)
y = random.randrange(0, 480,20)
self.postion = [x,y]
food = Food()
snake = Snake()
while True:
len_text = font.render("Length: "+str(len(snake.body)),True,red)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
if snake.direction != "left":
snake.direction = "right"
if event.key == pygame.K_LEFT:
if snake.direction != "right":
snake.direction = "left"
if event.key == pygame.K_UP:
if snake.direction != "down":
snake.direction = "up"
if event.key == pygame.K_DOWN:
if snake.direction != "up":
snake.direction = "down"
if snake.head == food.postion:
food.reset()
snake.add_body()
'''
#知识进阶-双倍奖励
if food.color==white:
food.reset()
snake.add_body()
else:
food.reset()
snake.add_body()
snake.add_body()
'''
if snake.head[0] > 620 or snake.head[0] < 0:
break
if snake.head[1] > 460 or snake.head[1] < 0:
break
snake.move_head()
snake.move_snake()
sc.fill(violet)
snake.draw_me()
food.draw_me()
sc.blit(len_text ,(50,20))
pygame.display.update()
time_clock.tick(3)
len_text = font.render("GAME OVER",True,red)
sc.blit(len_text ,(200,200))
pygame.display.update()
input()
希望对大家有帮助
致力于办公自动化的小小程序员一枚#
都看到这了,关注+点赞+收藏=不迷路!!
如果你想知道更多关于Python办公自动化的知识各位大佬给个关注吧!