|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
这是我写的一个吃鱼的游戏,两种结束游戏方式1、限时60s,2、没吃到鱼
我的问题是:
还没到60s呢,我没吃到鱼,游戏结束了,可以先择重新开始,重新开局了(不是关掉游戏再打开)计时却没有重置
如何解决这个pygame.time.get_ticks()函数的问题
还有就是我是个新手,我写的代码肯定比较笨重,希望有大神指点
这个是游戏内的图片,素材做的不是很认真不要见怪
- import pygame
- from pygame.locals import *
- import random
- import easygui as g
- g.msgbox(msg = "限时60秒 Let's Go!!!", ok_button = "开始游戏")
- def main():
- pygame.init()
- white = 255, 255, 255
- width, length = 500, 700
- screen = pygame.display.set_mode((width, length))
- keys = [False,False]
- playerpos = [225, 650]
- fishpos = [[random.randint(0,450), 0]]
- timer = 100
- score = 0
- runing = 1
- fish = pygame.image.load("resources/images/Fish.png")
- mouse = pygame.image.load("resources/images/Mouse.png")
- fishrect = pygame.Rect(fish.get_rect())
- playerect = pygame.Rect(mouse.get_rect())
- while runing:
- timer -= 1/10
- screen.fill(white)
- if timer <= 0:
- fishpos.append([random.randint(0,450), 0])
- timer = 100
- playerect.top = playerpos[1]
- playerect.left = playerpos[0]
- index = 0
- for fishp in fishpos:
- fishrect.top = fishp[1]
- fishrect.left = fishp[0]
- if fishrect.colliderect(playerect):
- fishpos.pop(index)
- score += 1
- if fishp[1] >= 650:
- fishpos.pop(index)
- runing = 0
- fishp[1] += 1/5
- index += 1
- for fishp in fishpos:
- screen.blit(fish, fishp)
- font = pygame.font.SysFont("youyuan", 24)
- timescore = pygame.time.get_ticks() // 1000 % 61
- survivedtext = font.render((str(score) + "条鱼" + "_" + str(timescore) + "秒钟"), True ,(0, 0, 0))
- screen.blit(survivedtext, (5, 5))
- screen.blit(mouse, playerpos)
- pygame.display.update()
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- exit()
- if event.type == pygame.KEYDOWN:
- if event.key == K_a:
- keys[0] = True
- elif event.key == K_d:
- keys[1] = True
- if event.type == pygame.KEYUP:
- if event.key == pygame.K_a:
- keys[0] = False
- elif event.key ==pygame.K_d:
- keys[1] = False
- if keys[0] and playerpos[0] >= 0 :
- playerpos[0] -= 1/3
- elif keys[1] and playerpos[0] <= 450:
- playerpos[0] += 1/3
- if timescore == 60:
- runing = 0
- if runing == 0:
- choice = g.buttonbox("Game Over!\n一共吃了" + str(score) + "鱼", choices = ("重新开始", "退出游戏"))
- timescore = pygame.time.get_ticks() - timescore
- return choice
- while True:
- choice = main()
- if choice == "重新开始":
- main()
- else:
- exit(0)
复制代码
time2赋值为后面的计时,time1赋值为开始的计时 time2 - time1 >= 60 游戏结束
选择重玩如果要重新计时,可以把整个游戏放在一个循环里面,循环的开始就导入计时函数。循环的最后,如果点击重新开始游戏,
这样每一次循环开始就重新计时 OK了 ,问题搞定
|
-
|