发生特定事件时如何停止计时器?

时间:2019-04-25 12:24:27

标签: python-2.7 pygame

我正在为高中时期的考试创建视频游戏,我们有一个名为“ ISN”的类,其中仅使用python。我应该创建一个视频游戏,所以我制作了一个视频游戏,但是我的计时器存在问题:我希望在某个事件发生时停止它。就我而言,当我的火箭与流星碰撞时,+ = 1。 如果a> 3,计时器停止。我该怎么办?

# Créé par amiota, le 19/03/2019 en Python 3.4
import pygame
from pygame.locals import *
pygame.init()
import sys
from random import *

#définition des images, sons et positions

win = pygame.display.set_mode((1280, 720))
pygame.display.set_caption('idée')
fond = pygame.image.load('fond.png')
rocket = pygame.image.load('rocket.png').convert_alpha()
meteor = pygame.image.load('meteor.png').convert_alpha()
lost = pygame.image.load('loose.png').convert_alpha()
son = pygame.mixer.music.load('music.wav')
vie = pygame.image.load('viefull.png').convert_alpha()
vie1 = pygame.image.load('vie1.png').convert_alpha()
vie2 = pygame.image.load('vie2.png').convert_alpha()
vie3 = pygame.image.load('vie3.png').convert_alpha()
vie4 = pygame.image.load('vie4.png').convert_alpha()
#pygame.mixer.init(frequency=22050, size=-16, channels=2, buffer=4096)
#sound = pygame.mixer.Sound('music.wav').play()

x = 640
y = 600
x1 = uniform(10,1200)
y1 = - 200
x2= 50
y2= 50
x3=500
y3=550
x4= 500
y4= 20
x5=10000
y5=10000
#Les rect pour les collisions
rect_lost=lost.get_rect()
rect_char = rocket.get_rect()
rect_char.center= (x,y)
rect_meteor = meteor.get_rect()
rect_meteor.center=(x1,y1)

speed = 80
b = 1
a = 0

temps = pygame.font.SysFont('MS Serif',50)

run = True

while run == True:

    timer = pygame.time.get_ticks()/1000
    compteur = temps.render(str(timer),0,(255,201,14))
#Mouvemements
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

        keys = pygame.key.get_pressed()
        if keys [pygame.K_LEFT]:
            rect_char.x -= speed
        if keys [pygame.K_RIGHT]:
            rect_char.x += speed
        if keys [pygame.K_RETURN]:
            rect_meteor.center=(uniform(10,1000),-100)
            rect_lost.center=(x5,y5)

    if rect_char.x > 1200:
        rect_char.x = 1200
#Quand il meurt
    if rect_char.y > 650 :
        rect_char.center = (uniform(10,1000),y)

#Gestion des collisions et du météor
    if rect_meteor.y<1280:
        rect_meteor.y+=15
    if rect_meteor.y>1200:
        rect_meteor.x = uniform(10,1100)
        rect_meteor.y = -100
    if rect_meteor.colliderect(rect_char):
        rect_char.top = rect_meteor.bottom
    if rect_meteor.colliderect(rect_char):
        rect_char.right = rect_meteor.left
    if rect_meteor.colliderect(rect_char):
        rect_char.left = rect_meteor.right

    if rect_char.y>650:
        a+=1
    if a>0:
        vie=vie1
    if a>1:
        vie1=vie2
    if a>2:
        vie2=vie3
    if a>3:
        vie3=vie4
        x5=360
        y5=300
        rect_meteor.x = 10000
        rect_char.center=(x,y)

    win.blit(fond,(0,0))
    win.blit(rocket,rect_char)
    win.blit(meteor,rect_meteor)
    win.blit(vie,(x4,y4))
    win.blit(lost,(x5,y5))
    win.blit(compteur,(120,45))
    pygame.display.update()

pygame.quit()

0 个答案:

没有答案