python⼩游戏代码⼤全-【程序源代码】python贪吃蛇⼩游戏关键字:python 游戏 贪吃蛇
正⽂ | 内容
在⽹络还不发达,没有平板电脑和⼿机的童年;电视机⾥的动画⽚和⼩游戏曾经陪伴我们度过了欢乐的时光.扫雷、贪吃蛇、俄罗斯⽅块、58坦克⼤战、超级玛丽、魂⽃罗...每⼀个都是回忆满满的游戏
02
python基础代码大全黑客
【⼀条蛇】# ⽆论何时 都在前端增加蛇块
def addnode(self):
left,top = (0,0)
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:

elif self.dirction == pygame.K_DOWN:

self.body.insert(0,node)
# 删除最后⼀个块
def delnode(self):
self.body.pop()
# 死亡判断
def isdead(self):
# 撞墙
if self.body[0].x not in range(SCREEN_X):
return True
if self.body[0].y not in range(SCREEN_Y):
return True
# 撞⾃⼰
if self.body[0] in self.body[1:]:
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 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
# ⾷物类怎么自己制作游戏代码
# ⽅法:放置/移除
# 点以25为单位
class Food:
def __init__(self):
< = pygame.Rect(-25,0,25,25)
def remove(self):
<=-25
def set(self):
== -25:
allpos = []
# 不靠墙太近 25 ~ SCREEN_X-25 之间
for pos in range(25,SCREEN_X-25,25):
allpos.append(pos)
)
def show_text(screen, pos, text, color, font_bold = False, font_size = 60, font_italic = False):
#获取系统字体,并设置⽂字⼤⼩
cur_font = pygame.font.SysFont("宋体", font_size) #设置是否加粗属性
cur_font.set_bold(font_bold)
#设置是否斜体属性
cur_font.set_italic(font_italic)
springerlink有中文吗#设置⽂字内容
text_fmt = der(text, 1, color)
#绘制⽂字
screen.blit(text_fmt, pos)
def main():
pygame.init()
screen_size = (SCREEN_X,SCREEN_Y)
screen = pygame.display.set_mode(screen_size) pygame.display.set_caption('Snake')
clock = pygame.time.Clock()
scores = 0
isdead = False
# 蛇/⾷物
snake = Snake()
food = Food()
while True:
for event in ():
pe == pygame.QUIT:
stackoverflowapp
pe == pygame.KEYDOWN:
snake.changedirection(event.key)
# 死后按space重新
if event.key == pygame.K_SPACE and isdead: return main()
数造词screen.fill((255,255,255))
# 画蛇⾝ / 每⼀步+1分
if not isdead:
scores+=1
for rect in snake.body:
(screen,(20,220,39),rect,0)
# 显⽰死亡⽂字
isdead = snake.isdead()
if isdead:
show_text(screen,(100,200),'YOU DEAD!',(227,29,18),False,100)
show_text(screen,(150,260),'press space to ',(0,0,22),False,30) # ⾷物处理 / 吃到+50分
# 当⾷物rect与蛇头重合,吃掉 -> Snake增加⼀个Node
== snake.body[0]:
scores+=50
snake.addnode()
# ⾷物投递
food.set()
(screen,(136,0,21),,0)
# 显⽰分数⽂字
show_text(screen,(50,500),'Scores: '+str(scores),(223,223,223)) pygame.display.update()
clock.tick(10)
if __name__ == '__main__':
main()
02
【两条蛇】import pygame, sys, time, random
from pygame.locals import *
redColour = pygame.Color(255, 0, 0)
greenColour = pygame.Color(0, 255, 0)
blueColour = pygame.Color(0, 0, 255)
blackColour = pygame.Color(0, 0, 0)
whiteColour = pygame.Color(255, 255, 255)
greyColour = pygame.Color(150, 150, 150)
yellowColour =pygame.Color(255, 255, 0)
截取字符串excelpurpleColour =pygame.Color(255, 0, 255)
brownColour =pygame.Color(150, 75, 75)
DeepPinkColour=pygame.Color(255 ,20 ,147)
#初始化并播放背景⾳乐
#pygame.mixer.init() #初始化混⾳器
#pygame.mixer.music.load('Ken Arai - NEXT TO YOU.mp3') #加载背景⾳乐
#pygame.mixer.music.set_volume(0.2) #设置⾳量
#pygame.mixer.music.play() #播放背景⾳乐
# 定义游戏结束函数。当双⽅长度相等且头头相撞时,为平局。游戏结束,并显⽰双⽅分数。
def gameOver (playSurface,score1,score2):
gameOverFont = pygame.font.SysFont('arial', 32)
gameOverSurf = der('tie '+'pink:'+str(score1-3)+' '+'blue:'+str(score2-3), True, greyColour)
gameOverRect = _rect()
gameOverRect.midtop = (320, 10)
playSurface.blit(gameOverSurf, gameOverRect)
pygame.display.flip()
time.sleep(5)
main()
# 定义游戏结束函数。当1号玩家撞墙或被2号玩家吃了时,游戏结束,显⽰1号玩家失败,以及双⽅分数。
def gameOver1(playSurface,score1,score2):
gameOverFont = pygame.font.SysFont('arial', 32)
gameOverSurf = der('green snake Game Over '+'pink:'+str(score1-3)+' '+'blue:'+str(score2-3), True, greyColour)
gameOverRect = _rect()
gameOverRect.midtop = (320, 10)
playSurface.blit(gameOverSurf, gameOverRect)
pygame.display.flip()
time.sleep(5)
main()
# 定义游戏结束函数。当2号玩家撞墙或被1号玩家吃了时,游戏结束,显⽰2号玩家失败,以及双⽅分数。
def gameOver2(playSurface,score1,score2):
gameOverFont = pygame.font.SysFont('arial', 32)
gameOverSurf = der(' blue snake Game Over '+'pink'+str(score1-3)+' '+'blue:'+str(score2-3), True, greyColour)
gameOverRect = _rect()

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。