我正在尝试在pygame中制作一个游戏,你需要点击图像才能获得分数。我的程序有效,但我仍然需要修复2个错误:
当用户按下再次播放的'c'时,**时间不会重置为0.我希望它回到0,这样游戏每10秒就会结束,你就不能再玩了。< / p>
即使用户没有按'c',即玩游戏,时间也会开始。所以当用户阅读规则时(假设他们花了5秒钟阅读规则),然后他们只按'c'再玩5秒钟。我希望它能够这样做,这样只有当用户按下“c”时才开始游戏时间。
这是我的游戏运行源代码:
def runGame():
#Initial score is 0
score=0
gameExit = False
gameOver = False
while not gameExit:
while gameOver == True:
#Game Over message
gameDisplay.fill(black)
message_to_screen("Game over",
red,
y_displace=-50,
size="large")
message_to_screen("Press C to play again or Q to quit.",
white,
y_displace=50,
size="medium")
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
gameExit = True
gameOver = False
if event.key == pygame.K_c: #If user press 'c' , game starts
gameLoop()
gameDisplay.fill((white))
#Possible Error from here
# ~~~~~~~~~~~~~~~
#My time function
time=pygame.time.get_ticks()/1000
if time==11: #Checks if time hit 10 (i took as 11 as it should complete the 10th second)
global time
global score
score=0
time=0
gameOver=True
#~~~~~~~~~~~~~ Till here
for event in pygame.event.get():
if event.type==pygame.QUIT:
running=False
pygame.quit()
quit()
if event.type == pygame.MOUSEBUTTONDOWN and event.button == LEFT:
# Set the x, y postions of the mouse click
x, y = event.pos
if foodrect.collidepoint(x, y):
foodrect.center=(random.randint(5,1060),random.randint(5,700))
print "New Position: ",foodrect.center
score+=1
continue
gameDisplay.blit(foodimg,foodrect)
showtime=font.render("Time: "+str(time),0,(0,0,0))
gameDisplay.blit(showtime,(950,10))
scoredisplay(score)
pygame.display.update()
def gameLoop():
clock.tick(FPS)
runGame()
pygame.quit()
quit()
gameLoop()
按Herman Yanush的要求。
import pygame
import time
import random
pygame.init()
foodimg=pygame.image.load("food.png")
foodrect=foodimg.get_rect()
#Colours
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
LEFT =1
#Game Display
display_width = 1080
display_height = 720
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Click It! ~ Snapnel Productions')
gameDisplay.fill((white))
font=pygame.font.SysFont("Arial",30)
#Clock
clock = pygame.time.Clock()
FPS = 30
cellSize=10
#Font Size
exsmallfont = pygame.font.SysFont("comicsansms", 17)
smallfont = pygame.font.SysFont("comicsansms", 25)
medfont = pygame.font.SysFont("comicsansms", 50)
largefont = pygame.font.SysFont("comicsansms", 80)
#The score function - displays the score on top right
def scoredisplay(scoredef=0):
text=smallfont.render("Score :%s" %(scoredef) ,True ,white)
gameDisplay.blit(text,[0,0])
def scoredisplay1(musicname):
text=exsmallfont.render("Now Playing :%s" %(musicname) ,True ,white)
gameDisplay.blit(text,[690,5])
#Starting Of the game
def game_intro():
intro = True
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key ==pygame.K_c:
intro = False
if event.key ==pygame.K_q:
pygame.quit()
quit()
gameDisplay.fill(black)
#Game Initial display message
message_to_screen("Click It!",
red,
-200,
size="large")
message_to_screen("This Game is created by Shrapnel",
white,
-30,
size="small")
message_to_screen(" the food to gain a point!",
white,
10,
size="small")
message_to_screen("Press 'C' to play the game or 'Q' to quit.",
white,
150,
size="small")
pygame.display.update()
clock.tick(15)
#Text Size
def text_objects(text,color, size):
if size=="small":
textSurface=smallfont.render(text, True ,color)
elif size=="medium":
textSurface=medfont.render(text, True ,color)
elif size=="large":
textSurface=largefont.render(text, True ,color)
return textSurface,textSurface.get_rect()
#Message to screen
def message_to_screen(msg,color,y_displace=0,size="small"):
textSurf,textRect=text_objects(msg,color,size)
textRect.center = (display_width / 2),(display_height / 2)+y_displace
gameDisplay.blit(textSurf,textRect)
#Score Display
def scoredisplay(scoredef=0):
text=smallfont.render("Score :%s" %(scoredef) ,True ,black)
gameDisplay.blit(text,[0,0])
score=0
global a
a=10
#print score
def runGame():
score=0
gameExit = False
gameOver = False
thing_startx = random.randrange(0,display_width)
thing_starty = -600
thing_speed = 7
thing_width = 100
thing_height = 100
while not gameExit:
deadZones=[]
while gameOver == True:
#Game Over message
gameDisplay.fill(black)
message_to_screen("Game over",
red,
y_displace=-50,
size="large")
message_to_screen("Press C to play again or Q to quit.",
white,
y_displace=50,
size="medium")
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
gameExit = True
gameOver = False
if event.key == pygame.K_c:
gameLoop()
gameDisplay.fill((white))
time=pygame.time.get_ticks()/1000
if time==11:
global time
global score
score=0
time=0
gameOver=True
#print score
for event in pygame.event.get():
if event.type==pygame.QUIT:
running=False
pygame.quit()
quit()
if event.type == pygame.MOUSEBUTTONDOWN and event.button == LEFT:
# Set the x, y postions of the mouse click
x, y = event.pos
if foodrect.collidepoint(x, y):
foodrect.center=(random.randint(5,1060),random.randint(5,700))
print "New Position: ",foodrect.center
score+=1
continue
gameDisplay.blit(foodimg,foodrect)
showtime=font.render("Time: "+str(time),0,(0,0,0))
gameDisplay.blit(showtime,(950,10))
#scoredisplay(score)
pygame.display.update()
def gameLoop():
clock.tick(FPS)
runGame()
pygame.quit()
quit()
game_intro()
gameLoop()
提前感谢您的帮助。
答案 0 :(得分:0)
我想我可能有一个至少我用过的解决方案。虽然有时使用pygame.time.get_ticks很好,但有一个更简单的解决方案。在while循环之前,将一个名为frame的变量设置为0.然后在while循环结束时输入:
if gameEnd == True:
frame=0
score=0
else:
frame+=1
然后你只需要在框架> 0时绘制图片。您还可以轻松操纵框架。如果您需要更多帮助,请给我留言。