实例介绍
【实例简介】一款模拟贪吃蛇的游戏
【实例截图】
【核心代码】
import pygame,sys,random,time
from pygame.locals import *
from pygame.locals import *
redColour = pygame.Color(255,0,0)
blackColour = pygame.Color(0,0,0)
whiteColour = pygame.Color(255,255,255)
greyColour = pygame.Color(150,150,150)
blackColour = pygame.Color(0,0,0)
whiteColour = pygame.Color(255,255,255)
greyColour = pygame.Color(150,150,150)
def gameOver(playSurface,score):
gOF = pygame.font.SysFont(None,54)
gOS = gOF.render('Game Over!', True, greyColour)
gOR = gOS.get_rect()
gOR.midtop = (300, 10)
pS.blit(gOS, gOR)
sF = pygame.font.SysFont(None,54)
sS = sF.render('Score:' str(score), True, greyColour)
sR = sS.get_rect()
sR.midtop = (300, 50)
pS.blit(sS, sR)
pygame.display.flip()
time.sleep(5)
pygame.quit()
sys.exit()
gOF = pygame.font.SysFont(None,54)
gOS = gOF.render('Game Over!', True, greyColour)
gOR = gOS.get_rect()
gOR.midtop = (300, 10)
pS.blit(gOS, gOR)
sF = pygame.font.SysFont(None,54)
sS = sF.render('Score:' str(score), True, greyColour)
sR = sS.get_rect()
sR.midtop = (300, 50)
pS.blit(sS, sR)
pygame.display.flip()
time.sleep(5)
pygame.quit()
sys.exit()
def main():
pygame.init()
clock = pygame.time.Clock()
pygame.display.set_caption('贪吃蛇')
sP = [100,100]#贪吃蛇 蛇头的位置
sS = [[100,100]]#贪吃蛇 蛇的身体,初始为一个单位
fP = [300,300]#食物的初始位置
fS= 1 #食物的个数为1
direction = 'right' #初始方向为右
cD= direction
score = 0 #初始得分
while True:
# 检测例如按键等pygame事件
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
# 判断键盘事件
if event.key == K_RIGHT or event.key == ord('d'):
cD = 'right'
if event.key == K_LEFT or event.key == ord('a'):
cD = 'left'
if event.key == K_UP or event.key == ord('w'):
cD = 'up'
if event.key == K_DOWN or event.key == ord('s'):
cD = 'down'
if event.key == K_ESCAPE:
pygame.event.post(pygame.event.Event(QUIT))
pygame.init()
clock = pygame.time.Clock()
pygame.display.set_caption('贪吃蛇')
sP = [100,100]#贪吃蛇 蛇头的位置
sS = [[100,100]]#贪吃蛇 蛇的身体,初始为一个单位
fP = [300,300]#食物的初始位置
fS= 1 #食物的个数为1
direction = 'right' #初始方向为右
cD= direction
score = 0 #初始得分
while True:
# 检测例如按键等pygame事件
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
# 判断键盘事件
if event.key == K_RIGHT or event.key == ord('d'):
cD = 'right'
if event.key == K_LEFT or event.key == ord('a'):
cD = 'left'
if event.key == K_UP or event.key == ord('w'):
cD = 'up'
if event.key == K_DOWN or event.key == ord('s'):
cD = 'down'
if event.key == K_ESCAPE:
pygame.event.post(pygame.event.Event(QUIT))
if cD== 'right' and not direction == 'left':
direction = cD
if cD == 'left' and not direction == 'right':
direction = cD
if cD == 'up' and not direction == 'down':
direction = cD
if cD == 'down' and not direction == 'up':
direction = cD
# 根据方向移动蛇头的坐标
if direction == 'right':
sP[0] = 20
if direction == 'left':
sP[0] -= 20
if direction == 'up':
sP[1] -= 20
if direction == 'down':
sP[1] = 20
# 增加蛇的长度
sS.insert(0,list(sP))
# 判断是否吃掉了树莓
if sP[0] == fP[0] and sP[1] == fP[1]:
fP = 0
else:
sS.pop()
# 如果吃掉树莓,则重新生成树莓
if fP == 0:
x = random.randrange(1,30)
y = random.randrange(1,23)
fP = [int(x*20),int(y*20)]
fS = 1
score = 1
# 绘制pygame显示层
pS.fill(blackColour)
for position in sS:
pygame.draw.rect(pS,whiteColour,Rect(position[0],position[1],20,20))
pygame.draw.rect(pS,redColour,Rect(fP[0], fP[1],20,20))
# 刷新pygame显示层
pygame.display.flip()
# 判断是否死亡
if sP[0] > 600 or sP[0] < 0:
gameOver(pS,score)
if sP[1] > 460 or sP[1] < 0:
gameOver(pS,score)
for sB in sS[1:]:
if sP[0] == sB[0] and sP[1] == sB[1]:
gameOver(pS,score)
# 控制游戏速度
if 0 <= score <3:
clock.tick(5)
elif 3 <= score <5:
clock.tick(6)
elif 5 <= score <10:
clock.tick(7)
elif 10 <= score <15:
clock.tick(8)
elif 15 <= score <20:
clock.tick(9)
elif 20 <= score <25:
clock.tick(10)
elif 25 <= score <30:
clock.tick(12)
elif 30 <= score <35:
clock.tick(15)
elif 35 <= score <40:
clock.tick(17)
else:
clock.tick(20)
if __name__ == "__main__":
pS = pygame.display.set_mode((600,460))
main()
direction = cD
if cD == 'left' and not direction == 'right':
direction = cD
if cD == 'up' and not direction == 'down':
direction = cD
if cD == 'down' and not direction == 'up':
direction = cD
# 根据方向移动蛇头的坐标
if direction == 'right':
sP[0] = 20
if direction == 'left':
sP[0] -= 20
if direction == 'up':
sP[1] -= 20
if direction == 'down':
sP[1] = 20
# 增加蛇的长度
sS.insert(0,list(sP))
# 判断是否吃掉了树莓
if sP[0] == fP[0] and sP[1] == fP[1]:
fP = 0
else:
sS.pop()
# 如果吃掉树莓,则重新生成树莓
if fP == 0:
x = random.randrange(1,30)
y = random.randrange(1,23)
fP = [int(x*20),int(y*20)]
fS = 1
score = 1
# 绘制pygame显示层
pS.fill(blackColour)
for position in sS:
pygame.draw.rect(pS,whiteColour,Rect(position[0],position[1],20,20))
pygame.draw.rect(pS,redColour,Rect(fP[0], fP[1],20,20))
# 刷新pygame显示层
pygame.display.flip()
# 判断是否死亡
if sP[0] > 600 or sP[0] < 0:
gameOver(pS,score)
if sP[1] > 460 or sP[1] < 0:
gameOver(pS,score)
for sB in sS[1:]:
if sP[0] == sB[0] and sP[1] == sB[1]:
gameOver(pS,score)
# 控制游戏速度
if 0 <= score <3:
clock.tick(5)
elif 3 <= score <5:
clock.tick(6)
elif 5 <= score <10:
clock.tick(7)
elif 10 <= score <15:
clock.tick(8)
elif 15 <= score <20:
clock.tick(9)
elif 20 <= score <25:
clock.tick(10)
elif 25 <= score <30:
clock.tick(12)
elif 30 <= score <35:
clock.tick(15)
elif 35 <= score <40:
clock.tick(17)
else:
clock.tick(20)
if __name__ == "__main__":
pS = pygame.display.set_mode((600,460))
main()
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论