[英]How do you get a wait time for a conditional statement in python/pygame
我是編程新手,這是我制作/制作的第一個真實游戲。 基本上,游戲讓您作為玩家向敵人射擊“子彈”。 某些物體在中間移動,以阻止所有子彈穿過。
我覺得玩家發射子彈的速度似乎太快了。 因此,我做了一些研究,發現有幫助的地方。 time.sleep(sec)。 當我使用它時,整個游戲將停止並等待時間結束。
當玩家按下鼠標按鈕發射子彈時,我在主程序循環中使用了time.sleep(sec)。 反過來,這會使整個游戲延遲5秒鍾,從而影響整個游戲。
那么我將如何等待它而不影響整個游戲?
這是我的游戲代碼:
import pygame
import random
import time
enemy_speed = 5
running = 1
# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
# --- Classes
class Enemy(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("enemy.png").convert()
self.rect = self.image.get_rect()
self.image.set_colorkey(WHITE)
def update(self):
self.rect.x += 7
if self.rect.x >= 610:
self.rect.x = random.randrange(50,610)
class Hand(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("hand.png").convert()
self.rect = self.image.get_rect()
self.image.set_colorkey(WHITE)
def update(self):
self.rect.y = 300
self.rect.x += 5
if self.rect.x >= 610:
self.rect.x = random.randrange(50,610)
class Hand2(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("hand2.png").convert()
self.rect = self.image.get_rect()
self.image.set_colorkey(WHITE)
def update (self):
self.rect.y = 100
self.rect.x -= 5
if self.rect.x <= 90:
self.rect.x = random.randrange(50,610)
class Hand3(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("hand3.png").convert()
self.rect = self.image.get_rect()
self.image.set_colorkey(WHITE)
def update (self):
self.rect.y = 200
self.rect.x += 3
if self.rect.x >= 610:
self.rect.x = random.randrange(50,610)
class Hand4(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("hand4.png").convert()
self.rect = self.image.get_rect()
self.image.set_colorkey(WHITE)
def update (self):
self.rect.y = 50
self.rect.x += 3
if self.rect.x >= 610:
self.rect.x = random.randrange(50,610)
class Hand5(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("hand5.png").convert()
self.rect = self.image.get_rect()
self.image.set_colorkey(WHITE)
def update (self):
self.rect.y = 350
self.rect.x -= 3
if self.rect.x >= 610:
self.rect.x = random.randrange(50,610)
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("player.png").convert()
self.rect = self.image.get_rect()
self.image.set_colorkey(WHITE)
def update(self):
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
self.rect.x -= 4
if event.key == pygame.K_RIGHT:
self.rect.x += 4
if self.rect.x <= 50:
self.rect.x = 60
if self.rect.x >= 610:
self.rect.x = 600
class Pellet(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.Surface([4,10])
self.image.fill(RED)
self.rect = self.image.get_rect()
def update(self):
speed_p = -3
self.rect.y -= speed_p
class Bullet(pygame.sprite.Sprite):
""" This class represents the bullet . """
def __init__(self):
# Call the parent class (Sprite) constructor
super().__init__()
self.image = pygame.Surface([4, 10])
self.image.fill(BLUE)
self.rect = self.image.get_rect()
def update(self):
""" Move the bullet. """
speed_b = 3
self.rect.y -= speed_b
# --- Create the window
# Initialize Pygame
pygame.init()
# Set the height and width of the screen
screen_width = 700
screen_height = 400
screen = pygame.display.set_mode([screen_width, screen_height])
pygame.display.set_caption("Cannon Battle")
all_sprites_list = pygame.sprite.Group()
enemy_list = pygame.sprite.Group()
player_list = pygame.sprite.Group()
hand_list = pygame.sprite.Group()
hand2_list = pygame.sprite.Group()
hand3_list = pygame.sprite.Group()
hand4_list = pygame.sprite.Group()
hand5_list = pygame.sprite.Group()
bullet_list = pygame.sprite.Group()
pellet_list = pygame.sprite.Group()
enemy = Enemy()
enemy_list.add(enemy)
all_sprites_list.add(enemy_list)
hand = Hand()
hand_list.add(hand)
all_sprites_list.add(hand_list)
hand2 = Hand2()
hand2_list.add(hand2)
all_sprites_list.add(hand2)
hand3 = Hand3()
hand3_list.add(hand3)
all_sprites_list.add(hand3)
hand4 = Hand4()
hand4_list.add(hand4)
all_sprites_list.add(hand4)
hand5 = Hand5()
hand5_list.add(hand5)
all_sprites_list.add(hand5)
player = Player()
player_list.add(player)
all_sprites_list.add(player_list)
done = False
clock = pygame.time.Clock()
player.rect.y = 370
behind = pygame.image.load("grassland.png").convert()
# -------- Main Program Loop
while not done:
screen.blit(behind, [0,0])
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.MOUSEBUTTONDOWN:
bullet = Bullet()
bullet.rect.x = player.rect.x
bullet.rect.y = player.rect.y
all_sprites_list.add(bullet)
bullet_list.add(bullet)
time.sleep(5)
elif event.type == pygame.MOUSEBUTTONUP:
pellet = Pellet()
pellet.rect.x = enemy.rect.x
pellet.rect.y = enemy.rect.y
all_sprites_list.add(pellet)
pellet_list.add(pellet)
# --- Game logic
# Call the update() method on all the sprites
all_sprites_list.update()
for pellet in pellet_list:
player_hit_list = pygame.sprite.spritecollide(pellet,player_list,True)
if pellet.rect.y >= 510:
pellet_list.remove(pellet)
all_sprites_list.remove(pellet)
for player in player_hit_list:
done = True
print("You Lost!")
print("But thanks for playing!")
print("Made by Oankar Studios")
for bullet in bullet_list:
enemy_hit_list = pygame.sprite.spritecollide(bullet,enemy_list,True)
if bullet.rect.y <= -10:
bullet_list.remove(bullet)
all_sprites_list.remove(bullet)
if bullet.rect.x == hand.rect.x:
bullet_list.remove(bullet)
all_sprites_list.remove(bullet)
if bullet.rect.x == hand2.rect.x:
bullet_list.remove(bullet)
all_sprites_list.remove(bullet)
if bullet.rect.x == hand3.rect.x:
bullet_list.remove(bullet)
all_sprites_list.remove(bullet)
if bullet.rect.x == hand4.rect.x:
bullet_list.remove(bullet)
all_sprites_list.remove(bullet)
if bullet.rect.x == hand5.rect.x:
bullet_list.remove(bullet)
all_sprites_list.remove(bullet)
for enemy in enemy_hit_list:
done = True
print("You Won!")
print("Made by Oankar Studios")
# Clear the screen
# Draw all the spites
all_sprites_list.draw(screen)
# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# --- Limit to 20 frames per second
clock.tick(60)
pygame.quit()
這是我使用time.sleep(sec)的地方,它在主程序循環中:
elif event.type == pygame.MOUSEBUTTONDOWN:
bullet = Bullet()
bullet.rect.x = player.rect.x
bullet.rect.y = player.rect.y
all_sprites_list.add(bullet)
bullet_list.add(bullet)
time.sleep(5)
您可以存儲最后一發子彈的時間,並忽略所有按下,直到經過5秒。 所以,在頂部
last_bullet_shot_time = None
然后
elif event.type == pygame.MOUSEBUTTONDOWN:
if last_bullet_shot_time is None or
time.now() - last_bullet_shot_time > datetime.timedelta(seconds = 5):
bullet = Bullet()
bullet.rect.x = player.rect.x
bullet.rect.y = player.rect.y
all_sprites_list.add(bullet)
bullet_list.add(bullet)
last_bullet_shot_time = time.now()
如您所知,time.sleep()會暫停整個程序(或者確切地說是線程),這通常不是您想要的。 一種可能的解決方案是比較@maniexx提到的時間。
當您使用pygame時,我建議您以基本上相同的方式使用pygame.time.get_ticks(),如本答案所述。
您可以使用pygame的功能根據計時器生成事件以及唯一的用戶定義事件,以使其在經過所需時間后創建通知事件。 這是您需要進行的更改:
# globals
TIMEOUT_EXPIRED = pygame.USEREVENT+1
shooting_allowed = True
然后在游戲主體中:
# in the event processing loop
elif event.type == pygame.MOUSEBUTTONDOWN:
if shooting_allowed:
bullet = Bullet()
bullet.rect.x = player.rect.x
bullet.rect.y = player.rect.y
all_sprites_list.add(bullet)
bullet_list.add(bullet)
# disable shooting for 5 seconds
shooting_allowed = False
pygame.time.set_timer(TIMEOUT_EXPIRED, 5000)
elif event.type == TIMEOUT_EXPIRED:
# re-enable shooting
shooting_allowed = True
pygame.time.set_timer(TIMEOUT_EXPIRED, 0) # stop generation of event
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.