python游戏结束显⽰分数代码_【程序源代码】python贪吃蛇
⼩游戏
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)
purpleColour =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()
gameOverRect.midtop = (320, 10)
playSurface.blit(gameOverSurf, gameOverRect)
pygame.display.flip()
time.sleep(5)
main()
def main():
pygame.init()  #pygame初始化
#显⽰欢迎界⾯,有开始游戏和退出游戏两个选项
title_font = pygame.font.SysFont('arial', 32)
welcome_words = der('Welcome to My Snake', True, (0, 0, 0), (255, 255, 255))
tips_font = pygame.font.SysFont('arial', 24)
start_game_words = der('Click to Start Game', True, (0, 0, 0), (255, 255, 255))
close_game_words = der('Press ESC to Close', True, (0, 0, 0), (255, 255, 255))
playSurface = pygame.display.set_mode((640, 480), FULLSCREEN, 32)  #全屏显⽰
fpsClock = pygame.time.Clock()
pygame.display.set_caption('贪吃蛇')  #欢迎界⾯,此时未开始游戏
game_started = False
#初始化两条蛇的起始位置和长度
snakePosition1 = [100, 100]
snakeSegments1 = [[100, 100], [80, 100], [60, 100]]
snakePosition2 = [200, 200]
snakeSegments2 = [[200, 200], [180, 200], [160, 200]]
#初始化树莓的起始位置
raspberryPosition = [300, 300]
raspberrySpawned = 1
#初始化两条蛇的起始⽅向
direction1 = 'right'
direction2 = 'right'
changeDirection1 = direction1
changeDirection2 = direction2
while True:  #游戏循环主体
for event in ():  #获取事件
pe == QUIT:
pe == KEYDOWN:  #键盘输⼊
#⽅向键控制玩家1
if event.key == K_RIGHT :
changeDirection1 = 'right'
if event.key == K_LEFT:
changeDirection1 = 'left'
if event.key == K_UP :
changeDirection1 = 'up'
if event.key == K_DOWN :
changeDirection1 = 'down'
#‘W’‘A’‘S’‘D’控制玩家2
if event.key == ord('d'):
changeDirection2 = 'right'
if event.key == ord('a'):
changeDirection2= 'left'
if event.key == ord('w'):
changeDirection2 = 'up'
if event.key == ord('s'):
changeDirection2 = 'down'
if event.key == K_ESCAPE:  #按动ESC退出游戏
pygame.event.post(pygame.event.Event(QUIT))
elif (not game_started) pe == pygame.MOUSEBUTTONDOWN: #在游戏欢迎界⾯时,根据⿏标位置判断是否开始游戏
x, y = _pos()
if 213 <= x <= 422 and 304 <= y <= 342:
game_started = True
playSurface.fill((255, 255, 255))  #游戏画⾯背景为⽩⾊if game_started: #开始游戏
# 判断是否输⼊了反⽅向,如果输⼊相反⽅向,则⽅向不改变if changeDirection1 == 'right' and direction1 != 'left': direction1 = changeDirection1
if changeDirection1 == 'left' and direction1 != 'right': direction1 = changeDirection1
if changeDirection1 == 'up' and direction1 != 'down': direction1 = changeDirection1
if changeDirection1 == 'down' and direction1 != 'up': direction1 = changeDirection1
if changeDirection2 == 'right' and direction2 != 'left': direction2 = changeDirection2
if changeDirection2 == 'left' and direction2 != 'right': direction2 = changeDirection2
if changeDirection2 == 'up' and direction2 != 'down': direction2 = changeDirection2
if changeDirection2 == 'down' and direction2 != 'down': direction2 = changeDirection2
# 根据⽅向移动蛇头的坐标
if direction1 == 'right':
snakePosition1[0] += 20贪吃蛇的编程代码
if direction1 == 'left':
snakePosition1[0] -= 20
if direction1 == 'up':
snakePosition1[1] -= 20
if direction1 == 'down':
snakePosition1[1] += 20
if direction2 == 'right':
snakePosition2[0] += 20
if direction2 == 'left':
snakePosition2[0] -= 20
if direction2 == 'up':
snakePosition2[1] -= 20
if direction2 == 'down':
snakePosition2[1] += 20
# 增加蛇的长度
snakeSegments1.insert(0, list(snakePosition1))
snakeSegments2.insert(0, list(snakePosition2))
# 判断是否吃掉了树莓
if snakePosition1[0] == raspberryPosition[0] and snakePosition1[1] == raspberryPosition[1]:
raspberrySpawned = 0
else:
snakeSegments1.pop()
if snakePosition2[0] == raspberryPosition[0] and snakePosition2[1] == raspberryPosition[1]:
raspberrySpawned = 0
else:
snakeSegments2.pop()
# 如果吃掉树莓,则重新⽣成树莓
if raspberrySpawned == 0:
x = random.randrange(1, 32)
y = random.randrange(1, 24)
raspberryPosition = [int(x * 20), int(y * 20)]  #先随机在任意位置⽣成⼀个树莓
while raspberryPosition in snakePosition1 or raspberryPosition in snakePosition2:  #判断树莓是否声称在蛇的⾝体上,如果是,则重新⽣成树莓
x = random.randrange(1, 32)
y = random.randrange(1, 24)
raspberryPosition = [int(x * 20), int(y * 20)]
raspberrySpawned = 1
# 刷新pygame显⽰层,显⽰两条蛇以及树莓
playSurface.fill(whiteColour)
for position in snakeSegments1:
(playSurface, DeepPinkColour, Rect(position[0], position[1], 20, 20))
for position in snakeSegments2:
(playSurface, blueColour, Rect(position[0], position[1], 20, 20))

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