Python 2.7 在python游戏中添加“再次播放”选项
我正在使用python为我的编程类制作一个游戏。我不知道当玩家输了,或者退出游戏时,如何再次给他们选择权。我正在使用python 2.7。这是我的游戏代码:Python 2.7 在python游戏中添加“再次播放”选项,python-2.7,Python 2.7,我正在使用python为我的编程类制作一个游戏。我不知道当玩家输了,或者退出游戏时,如何再次给他们选择权。我正在使用python 2.7。这是我的游戏代码: import pygame, sys, time, random from pygame.locals import * # set up pygame pygame.init() mainClock = pygame.time.Clock() # set up the window WINDOWWIDTH = 1000 WINDOW
import pygame, sys, time, random
from pygame.locals import *
# set up pygame
pygame.init()
mainClock = pygame.time.Clock()
# set up the window
WINDOWWIDTH = 1000
WINDOWHEIGHT = 500
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
pygame.display.set_caption('Dankest Memes')
# set up the colors
PINK = (223, 61, 163)
TEXTCOLOR = (223, 61, 163)
def waitForPlayerToPressKey():
while True:
for event in pygame.event.get():
if event.type == QUIT:
terminate()
if event.type == KEYDOWN:
if event.key == K_ESCAPE: # pressing escape quits
terminate()
return
def terminate():
while True:
for event in pygame.event.get():
if event.type == QUIT:
terminate()
if event.type == KEYDOWN:
if event.key == K_ESCAPE: # pressing escape quits
terminate()
return
def drawText(text, font, surface, x, y):
textobj = font.render(text, 1, TEXTCOLOR)
textrect = textobj.get_rect()
textrect.topleft = (x, y)
surface.blit(textobj, textrect)
font = pygame.font.SysFont(None, 48)
TEXTCOLOR = (255,250,250)
Score=4
# set up fonts
font = pygame.font.SysFont(None, 48)
myscore=4
# set up the block data structure
file = 'score.txt'
player = pygame.Rect(300, 100, 40, 40)
playerImage = pygame.image.load('player.png')
playerStretchedImage = pygame.transform.scale(playerImage, (40, 40))
foodImage = pygame.image.load('meme.png')
foods = []
for i in range(20):
foods.append(pygame.Rect(random.randint(0, WINDOWWIDTH - 20), random.randint(0, WINDOWHEIGHT - 20), 20, 20))
foodCounter = 0
NEWFOOD = 40
baddie = pygame.Rect(300, 100, 40, 40)
baddieImage = pygame.image.load('baddie.png')
baddieStretchedImage = pygame.transform.scale(baddieImage, (40, 40))
baddies = []
for i in range(20):
baddies.append(pygame.Rect(random.randint(0, WINDOWWIDTH - 20), random.randint(0, WINDOWHEIGHT - 20), 20, 20))
baddieCounter = 0
NEWBADDIE = 120
# set up keyboard variables
moveLeft = False
moveRight = False
moveUp = False
moveDown = False
MOVESPEED = 6
# set up music
pickUpSound = pygame.mixer.Sound('pickup.wav')
pygame.mixer.music.load('background.mp3')
pygame.mixer.music.play(-1, 0.0)
musicPlaying = True
# show the "Start" screen
drawText('Dankest Memes', font, windowSurface, (WINDOWWIDTH / 3), (WINDOWHEIGHT / 3))
drawText('Press any key to start.', font, windowSurface, (WINDOWWIDTH / 3) - 50, (WINDOWHEIGHT / 3) + 50)
drawText('Move with WASD or the arrow keys.', font, windowSurface, (WINDOWWIDTH / 3) - 50, (WINDOWHEIGHT / 3) + 100)
drawText('Collect green Pepes to get points.', font, windowSurface, (WINDOWWIDTH / 3) - 50, (WINDOWHEIGHT / 3) + 150)
drawText('Avoid the chickens.', font, windowSurface, (WINDOWWIDTH / 3) - 50, (WINDOWHEIGHT / 3) + 200)
pygame.display.update()
waitForPlayerToPressKey()
# run the game loop
while True:
# check for the QUIT event
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
# change the keyboard variables
if event.key == K_LEFT or event.key == ord('a'):
moveRight = False
moveLeft = True
if event.key == K_RIGHT or event.key == ord('d'):
moveLeft = False
moveRight = True
if event.key == K_UP or event.key == ord('w'):
moveDown = False
moveUp = True
if event.key == K_DOWN or event.key == ord('s'):
moveUp = False
moveDown = True
if event.type == KEYUP:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
if event.key == K_LEFT or event.key == ord('a'):
moveLeft = False
if event.key == K_RIGHT or event.key == ord('d'):
moveRight = False
if event.key == K_UP or event.key == ord('w'):
moveUp = False
if event.key == K_DOWN or event.key == ord('s'):
moveDown = False
if event.key == ord('x'):
player.top = random.randint(0, WINDOWHEIGHT - player.height)
player.left = random.randint(0, WINDOWWIDTH - player.width)
if event.key == ord('m'):
if musicPlaying:
pygame.mixer.music.stop()
else:
pygame.mixer.music.play(-1, 0.0)
musicPlaying = not musicPlaying
if event.type == MOUSEBUTTONUP:
foods.append(pygame.Rect(event.pos[0] - 10, event.pos[1] - 10, 20, 20))
foodCounter += 1
if foodCounter >= NEWFOOD:
# add new food
foodCounter = 0
foods.append(pygame.Rect(random.randint(0, WINDOWWIDTH - 20), random.randint(0, WINDOWHEIGHT - 20), 20, 20))
baddieCounter += 1
if baddieCounter >= NEWBADDIE:
baddieCounter = 0
baddies.append(pygame.Rect(random.randint(0, WINDOWWIDTH - 20), random.randint(0, WINDOWHEIGHT - 20), 20, 20))
# draw the pink background onto the surface
windowSurface.fill(PINK)
# move the player
if moveDown and player.bottom < WINDOWHEIGHT:
player.top += MOVESPEED
if moveUp and player.top > 0:
player.top -= MOVESPEED
if moveLeft and player.left > 0:
player.left -= MOVESPEED
if moveRight and player.right < WINDOWWIDTH:
player.right += MOVESPEED
drawText('Score: %s' % (Score), font, windowSurface, 10,0)
# draw the block onto the surface
windowSurface.blit(playerStretchedImage, player)
# check if the block has intersected with any food squares.
for food in foods[:]:
if player.colliderect(food):
foods.remove(food)
Score+=2
if musicPlaying:
pickUpSound.play()
for baddie in baddies[:]:
if player.colliderect(baddie):
baddies.remove(baddie)
Score-=4
if musicPlaying:
pickUpSound.play()
if Score < 0:
drawText('You have lost', font, windowSurface, (WINDOWWIDTH / 3), (WINDOWHEIGHT / 3))
drawText('Press escape to quit the game', font, windowSurface, (WINDOWWIDTH / 3) - 50, (WINDOWHEIGHT / 3) + 50)
drawText('Press any other key to play again', font, windowSurface, (WINDOWWIDTH / 3) - 50, (WINDOWHEIGHT / 3) + 100)
pygame.display.update()
waitForPlayerToPressKey()
# draw the food
for food in foods:
windowSurface.blit(foodImage, food)
# draw the baddie
for baddie in baddies:
windowSurface.blit(baddieImage, baddie)
# draw the window onto the screen
pygame.display.update()
mainClock.tick(40)
if event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
导入pygame、sys、time、random
从pygame.locals导入*
#设置pygame
pygame.init()
mainClock=pygame.time.Clock()
#开窗
窗宽=1000
窗高=500
windowSurface=pygame.display.set_模式((窗口宽度,窗口高度),0,32)
pygame.display.set_标题('Dankest Memes'))
#设置颜色
粉红色=(22361163)
TEXTCOLOR=(22361163)
def waitForPlayerToPressKey():
尽管如此:
对于pygame.event.get()中的事件:
如果event.type==退出:
终止()
如果event.type==KEYDOWN:
如果event.key==K_ESCAPE:#按ESCAPE退出
终止()
返回
def terminate():
尽管如此:
对于pygame.event.get()中的事件:
如果event.type==退出:
终止()
如果event.type==KEYDOWN:
如果event.key==K_ESCAPE:#按ESCAPE退出
终止()
返回
def drawText(文本、字体、曲面、x、y):
textobj=font.render(text,1,TEXTCOLOR)
textrect=textobj.get_rect()
textrect.topleft=(x,y)
surface.blit(textobj,textrect)
font=pygame.font.SysFont(无,48)
TEXTCOLOR=(255250250)
分数=4
#设置字体
font=pygame.font.SysFont(无,48)
myscore=4
#设置块数据结构
文件='score.txt'
player=pygame.Rect(300、100、40、40)
playerImage=pygame.image.load('player.png')
playerStretchDimage=pygame.transform.scale(playerImage,(40,40))
foodImage=pygame.image.load('meme.png')
食品=[]
对于范围(20)内的i:
食品.append(pygame.Rect(random.randint(0,WINDOWWIDTH-20),random.randint(0,WINDOWHEIGHT-20),20,20))
foodCounter=0
新食品=40
baddie=pygame.Rect(3001004040)
baddieImage=pygame.image.load('baddie.png')
baddieStretchedImage=pygame.transform.scale(baddieImage,(40,40))
坏人=[]
对于范围(20)内的i:
baddies.append(pygame.Rect(random.randint(0,WINDOWWIDTH-20),random.randint(0,WINDOWHEIGHT-20),20,20))
baddieCounter=0
纽巴迪=120
#设置键盘变量
moveLeft=False
moveRight=False
moveUp=False
向下移动=错误
移动速度=6
#设置音乐
pickUpSound=pygame.mixer.Sound('pickUpSound.wav'))
pygame.mixer.music.load('background.mp3')
pygame.mixer.music.play(-1,0.0)
音乐播放=真
#显示“开始”屏幕
drawText('Dankest Memes',字体,窗口表面,(窗口宽度/3),(窗口高度/3))
drawText('按任意键开始'),字体,窗口表面,(窗口宽度/3)-50,(窗口高度/3)+50)
drawText('使用WASD或箭头键移动'),字体,窗口表面,(窗口宽度/3)-50,(窗口高度/3)+100)
drawText('收集绿色Pepes以获得分数'),字体,窗口表面,(窗口宽度/3)-50,(窗口高度/3)+150)
drawText('避免鸡'),字体,窗口表面,(窗口宽度/3)-50,(窗口高度/3)+200)
pygame.display.update()
waitForPlayerToPressKey()
#运行游戏循环
尽管如此:
#检查退出事件
对于pygame.event.get()中的事件:
如果event.type==退出:
pygame.quit()
sys.exit()
如果event.type==KEYDOWN:
#更改键盘变量
如果event.key==K_LEFT或event.key==ord('a'):
moveRight=False
moveLeft=True
如果event.key==K_RIGHT或event.key==ord('d'):
moveLeft=False
moveRight=True
如果event.key==K_UP或event.key==ord('w'):
向下移动=错误
向上移动=真
如果event.key==K_DOWN或event.key==ord('s'):
moveUp=False
向下移动=真
如果event.type==KEYUP:
如果event.key==K_转义:
pygame.quit()
sys.exit()
如果event.key==K_LEFT或event.key==ord('a'):
moveLeft=False
如果event.key==K_RIGHT或event.key==ord('d'):
moveRight=False
如果event.key==K_UP或event.key==ord('w'):
moveUp=False
如果event.key==K_DOWN或event.key==ord('s'):
向下移动=错误
如果event.key==ord('x'):
player.top=random.randint(0,WINDOWHEIGHT-player.height)
player.left=random.randint(0,WINDOWWIDTH-player.width)
如果event.key==ord('m'):
如果音乐播放:
pygame.mixer.music.stop()
其他:
pygame.mixer.music.play(-1,0.0)
音乐播放=非音乐播放
如果event.type==MOUSEBUTTONUP:
foods.append(pygame.Rect(event.pos[0]-10,event.pos[1]-10,20,20))
foodCounter+=1
如果foodCounter>=新食品:
#添加新食物
foodCounter=0
食品.append(pygame.Rect(random.randint(0,WINDOWWIDTH-20),random.randint(0,WINDOWHEIGHT-20),20,20))
Baddie计数器+=1
如果baddieCounter>=NEWBADDIE:
baddieCounter=0
baddies.append(pygame.Rect(random.randint(0,WINDOWWIDTH-20),random.randint(0,WINDOWHEIGHT-20),20,20))
#在曲面上绘制粉红色背景
窗台表面填充(粉红色)
#移动玩家
如果向下移动且player.bottom<窗口高度:
player.top+=移动速度
如果moveUp和player.top>0:
player.top-=移动速度
如果moveLeft和player.left>0:
player.left-=移动速度
如果moveRight和player.right<窗口宽度:
player.right+=移动速度
drawText('分数:%s'(分数),字体,窗口表面,10,0)
#将块绘制到曲面上
窗口表面.blit(播放器屏幕、播放器)
#检查方块是否与任何食物方块相交。
对于
def game_loop():
while True:
# check for the QUIT event
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
....
#pseudo code
if game_has_ended == True:
game_over_screen()