import pygame
from sys import exit
import random
#点定义
class Point:
row = 0
col = 0
def __init__(self,row,col):
self.row = row
self.col = col
def copy(self):
return Point(self.row,self.col)
def __str__(self):
return "Point(%d,%d)" %(self.row,self.col)
def __eq__(self, other):
return self.row == other.row and self.col == other.col
#初始化全局变量
width = 800
hight = 600
ROW = 30
COL = 40
direct = 'left' #方向
Running = True
SPEED = 100
#窗口初始化
pygame.init()
window = pygame.display.set_mode((width,hight,))
pygame.display.set_caption('贪吃蛇游戏')
CUSTOMEVENT = pygame.USEREVENT + 1
pygame.time.set_timer(CUSTOMEVENT,SPEED)
#初始化蛇的位置
head = Point(row = int(ROW/2),col = int(COL/2))
snake =[Point(head.row,head.col+1),
Point(head.row,head.col+2),
Point(head.row,head.col+3)]
#初始化颜色变量
head_color = (0,158,128)
snake_food_color = (255,255,0)
snake_color = (200,147,158)
#随机食物
def gen_food():
while 1:
position = Point(row = random.randint(0,ROW-1),col = random.randint(0,COL-1))
is_coll = False
# if head.row == position.row and head.col == position.col:
# is_coll = True
if head == position:
is_coll = True
for body in snake:
if body.row == position.row and body.col == position.col:
is_coll = True
if not is_coll:
break
return position
snake_food = gen_food()
#画格子
def rect(point,color):
left = point.col * width/COL
top = point.row *hight/ROW
pygame.draw.rect(window,color,(left,top,width/COL,hight/ROW))
#处理事件
def HandleEvent():
global direct
global Running
for event in pygame.event.get():
if event.type == pygame.QUIT:
Running = False
elif event.type == pygame.KEYDOWN:
if event.key == 273 or event.key ==119:
if direct =='left' or direct == 'right':
direct = 'down'
if event.key == 274 or event.key == 115:
if direct == 'left' or direct == 'right':
direct = 'up'
if event.key == 276 or event.key == 97:
if direct == 'up' or direct == 'down':
direct = 'left'
if event.key == 275 or event.key == 100:
if direct == 'up' or direct == 'down':
direct = 'right'
elif event.type == CUSTOMEVENT:
MoveSnake()
#移动蛇位置
def MoveSnake():
global snake_food
head_orgin = head.copy();
if direct == 'left':
head.col -= 1
if direct == 'right':
head.col += 1
if direct == 'up':
head.row += 1
if direct == 'down':
head.row -= 1
eat = head == snake_food
if eat:
snake_food = gen_food()
else:
snake.pop()
snake.insert(0, head_orgin)
print("---------begin------------")
print(head)
for body in snake:
print(body)
print("---------end------------")
#检查碰撞
def CheckGameRun():
if head.col < 0 or head.row < 0 or head.row >= ROW or head.col >= COL:
print("crah the wall")
return False
for body in snake:
if head == body:
print("crash self")
return False
return True
#渲染窗口
def DrawGameWnd():
pygame.draw.rect(window, (245, 135, 155), (0, 0, width, hight))
rect(head, head_color)
rect(snake_food, snake_food_color)
for body in snake:
rect(body, snake_color)
clock = pygame.time.Clock()
#游戏循环
while Running:
clock.tick(30)
HandleEvent()
# MoveSnake()
is_continue = CheckGameRun()
if is_continue:
DrawGameWnd()
pygame.display.flip()
else:
print("Game Over")
break
参考已有的贪吃蛇项目,并在在原项目上做了优化,使用了定时器控制速度
原文:https://blog.csdn.net/weixin_44051713/article/details/90042927