目录
游戏规则
使用↑ ↓ ← →四个键进行游玩,蛇头不能碰到自己
在结束时会有分数打在上面
游戏代码
#声明编码
# -*- coding utf-8 -*-
# 导入框架
import pygame
import random
# 设置大小长度
W = 800
H = 600
size = (W, H)
# 设定游戏行列数
ROW = 30
COL = 40
# 定义要使用的类(用来记录点坐标)
class Point:
def __init__(self, row, col):
self.row = row
self.col = col
def copy(self):
return Point(row=self.row, col=self.col)
# 定义绘画的函数
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(screen, color, (left, top, int(cell_width), int(cell_height)))
# 随机生成食物
def genfood():
while True:
food_x = Point(row=random.randint(0, ROW - 1), col=random.randint(0, COL