每个程序员都有一个游戏梦,都想开发一款让别人爱不释手的游戏。游戏开发引擎很多像Unity 3D,cocos2d,白鹭(Egret),LayaBox,threeJs等等。一个偶然的机会,看到gitee上举办趣味魔改贪吃蛇游戏,出于程序员的自信心的驱使,加上爱折腾的心思便果断报名参加了。今天和大家一起看看Python小游戏的开发。Python有一个游戏开发的库pygame,可以非常简单方便地开发游戏。
先看一下游戏运行的效果:
游戏有几个角色
- 贪吃蛇(Snake)
- 青蛙(Food)
- 障碍物 (Stone)
针对这三个角色,分别用三个类来描述。Snake, Food, Stone
Food类需要做的是: 初始化,移动,删除,投食物
import param
import pygame
import random
# 食物类
# 方法: 放置/移除
# 点以25为单位
class Food:
def __init__(self):
self.rect = pygame.Rect(-25, 0, 25, 25)
self.allposX = []
self.allposY = []
for pos in range(25, param.SCREEN_X - 25, 25):
self.allposX.append(pos)
for pos in range(25, param.SCREEN_Y - 25, 25):
self.allposY.append(pos)
def remove(self):
self.rect.x = -25
def set(self, stone):
if self.rect.x <= -25:
allpos = []
# 不靠墙太近 25 ~ SCREEN_X-25 之间
for pos in range(25, param.SCREEN_X - 25, 25):
allpos.append(pos)
self.rect.left = random.choice(self.allposX)
self.rect.top = random.choice(self.allposY)
if stone.isContain(self.rect.left, self.rect.top):
self.rect.left = -25
self.set(stone)
def move(self):
self.rect.x -= 25
Stone类需要做的是: 初始化,生成墙,移动墙,新增墙,判断是否碰撞
import param
import pygame
import random
# 石头障碍物
# 点以50为单位
class Stone:
def __init__(self):
self.body = []
self.allStoneHeigh = []
for y in range(125, param.HALF_Y - 50, param.STONE_WIDTH):
self.allStoneHeigh.append(y)
for x in range(0, param.SCREEN_X, 200):
self.body.append(self.generateWall(x, True))
self.body.append(self.generateWall(x, False))
def addOne(self):
self.body.insert(0, self.generateWall(
param.SCREEN_X - param.STONE_WIDTH, True))
self.body.insert(0, self.generateWall(
param.SCREEN_X - param.STONE_WIDTH, False))
def generateWall(self, left, isTop):
heigh = random.choice(self.allStoneHeigh)
top = 0
if isTop:
top = 0
else:
top = param.SCREEN_Y - heigh
wall = []
for i in range(0, heigh, 50):
wall.append(pygame.Rect(left, top + i,
param.STONE_WIDTH, param.STONE_WIDTH))
return wall
def movePixel(self, pixel):
print(len(self.body))
if len(self.body) > 0:
for wall in self.body:
for stone in wall:
stone.left -= pixel
if len(self.body) > 12:
self.body.pop()
def isContain(self, left, top):
for wall in self.body:
for stoneRect in wall:
if (stoneRect.left == left or stoneRect.left == left - 25) and (stoneRect.top == top or stoneRect.top == top - 25):
return True
return False
贪吃蛇类:
import pygame
import random
import param
from stone import Stone
# 蛇类
# 点以25为单位
class Snake(object):
# 初始化各种需要的属性 [开始时默认向右/身体块x5]
def __init__(self):
self.dirction = pygame.K_RIGHT
self.body = []
for x in range(5):
self.addnode()
# 无论何时 都在前端增加蛇块
def addnode(self):
left, top = (0, param.HALF_Y)
if self.body:
left, top = (self.body[0].left, self.body[0].top)
node = pygame.Rect(left, top, 25, 25)
if self.dirction == pygame.K_LEFT:
node.left -= 25
elif self.dirction == pygame.K_RIGHT:
node.left += 25
elif self.dirction == pygame.K_UP:
node.top -= 25
elif self.dirction == pygame.K_DOWN:
node.top += 25
self.body.insert(0, node)
# 删除最后一个块
def delnode(self):
self.body.pop()
# 死亡判断
def isdead(self, stone):
# 撞墙
if self.body[0].x not in range(param.SCREEN_X):
return True
if self.body[0].y not in range(param.SCREEN_Y):
return True
# 撞自己
if self.body[0] in self.body[1:]:
return True
# 撞障碍物
for rect in self.body:
if stone.isContain(rect.left, rect.top):
return True
return False
# 移动!
def move(self):
self.addnode()
self.delnode()
# 改变方向 但是左右、上下不能被逆向改变
def changedirection(self, curkey):
LR = [pygame.K_LEFT, pygame.K_RIGHT]
UD = [pygame.K_UP, pygame.K_DOWN]
if curkey == self.dirction:
self.move()
if curkey in LR + UD:
if (curkey in LR) and (self.dirction in LR):
return
if (curkey in UD) and (self.dirction in UD):
return
self.dirction = curkey
def deadAction(self, screen, clock):
failedImg1 = pygame.image.load('res/tail_up.png')
failedImg2 = pygame.image.load('res/tail_right.png')
failedImg3 = pygame.image.load('res/tail_down.png')
failedImg4 = pygame.image.load('res/tail_left.png')
imgList = [failedImg1, failedImg2, failedImg3, failedImg4]
count = 0
for img in imgList:
count += 1
if count >= 4:
break
screen.blit(img, (350, 300))
clock.tick(2)
pygame.display.update()
有了3个对象之后,接下来就是怎么控制,让他们动起来
pygame库有个时钟类Clock,通过设置Clock的帧率,来控制各个物体移动的速度来达到页面移动的效果。
clock = pygame.time.Clock()
clock.tick(3)
再添加一些吃到食物计分功能,当分数达到一定的值,再触发升级提高游戏难度。使整个游戏的可玩度提高。为了提高游戏的体验,再加一些背景音乐和音效和死亡效果等等,整个游戏就丰满了。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。