使用使用Python第三方库第三方库pygame写个贪吃蛇小游戏写个贪吃蛇小游戏
今天看到几个关于pygame模块的博客和视频,感觉非常有趣,这里照猫画虎写了一个贪吃蛇小游戏,目前还有待完善,但是
基本游戏功能已经实现,下面是代码:
# 导入模块
import pygame
import random
# 初始化
pygame.init()
w = 720 #窗口宽度
h = 600 #窗口高度
ROW = 30 #行数
COL = 36 #列数
#将所有的坐标看作是一个个点,定义点类
class Point:
row = 0
col = 0
def __init__(self, row, col):
self.row = row
self.col = col
def copy(self):
return Point(row = self.row,col = self.col)
#显示窗口和标题
size = (w, h)
window = pygame.display.set_mode(size)
pygame.display.set_caption('贪吃蛇')
#定义蛇头坐标
head = Point(row = ROW/2, col = COL/2)
#蛇身体
snake_list = [
Point(row = head.row,col = head.col+1),
Point(row = head.row,col = head.col+2),
Point(row = head.row,col = head.col+3)
] #产生食物
def pro_food():
#食物不能与蛇重叠
while True:
pos = Point(row=random.randint(1,ROW-2), col=random.randint(1,COL-2))
is_coll = False
if head.row == pos.row and head.col == pos.col:
is_coll = True
for snake in snake_list:
if snake.col == pos.col and snake.row == pos.row:
is_coll = True
break
if not is_coll:
return pos
food = pro_food()
#定义颜色
bg_color = (255, 255, 255)
head_color = (0, 128, 128)
food_color = (255, 255, 0)
snake_color = (200,200,200)
#给定初始方向
dire = 'left'
def rect(point, color):
cell_width = w/COL
cell_height = h/ROW
left = point.col*cell_width
top = point.row*cell_height
pygame.draw.rect(
window, color,
(left,top,cell_width, cell_height, )
)
pass
# 游戏循环
quit = True
clock = pygame.time.Clock()
while quit:
for event in pygame.event.get():
#退出方式
if event.type == pygame.QUIT:
quit = False
elif event.type == pygame.KEYDOWN:
#键盘控制
if event.key == 273 or event.key == 119:
if dire == 'left' or dire == 'right':
评论0