나는 공간 침략자/총알 지옥 게임 인 파이 게임 코드에서 나의 적 샷 중 하나를 지연 시키려고합니다. 불행히도, 나는 적의 총을 늦추는 것에 문제가있는 것 같다.적을 지연
import pygame
import random
import time
import os
import threading
pygame.time.set_timer (pygame.USEREVENT , 450)
event_500ms = pygame.USEREVENT + 1
pygame.time.set_timer(event_500ms, 500)
class Alien2 (pygame.sprite.Sprite):
def __init__(self):
# Call the parent class (Sprite) constructor
super().__init__()
self.image = pygame.Surface([20, 15])
self.image = pygame.image.load("alien3.png").convert()
self.image.set_colorkey(WHITE)
self.rect = self.image.get_rect()
def update(self):
pos = pygame.mouse.get_pos()
dx = pos[0] - self.rect.x + 18
dx *= .05
self.rect.x += dx
def BossPowers():
bullet3 = Bullet3()
# Set the bullet so it is where the player is
bullet3.rect.x = alien2.rect.x + 47
bullet3.rect.y = alien2.rect.y + 57
# Add the bullet to the lists
all_sprites_list.add(bullet3)
bullet2_list.add(bullet3)
def BossPowers2():
bullet4 = Bullet4()
# Set the bullet so it is where the player is
bullet4.rect.x = alien2.rect.x + 47
bullet4.rect.y = alien2.rect.y + 57
# Add the bullet to the lists
all_sprites_list.add(bullet4)
bullet2_list.add(bullet4)
def delay():
#threading.Timer(5.0, delay).start()
bullet2 = Bullet2()
# Set the bullet so it is where the player is
bullet2.rect.x = alien2.rect.x + 47
bullet2.rect.y = alien2.rect.y + 57
# Add the bullet to the lists
all_sprites_list.add(bullet2)
bullet2_list.add(bullet2)
BossPowers()
BossPowers2()
내가
while not done:
# --- Event Processing
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if level % 2 == 0:
all_sprites_list.add(alien2)
alien2_list.add(alien2)
for block in block_list:
block_list.remove(block)
all_sprites_list.remove(block)
#t = threading.Timer(2, delay)
#t.start()
#if event.type == event_500ms:
#delay()
if level % 2 == 1:
block_list.add(block)
all_sprites_list.add(block)
괜찮아 그래서 여기 내 문제는 내 마음 효율적으로 미끄러 져되고 때로는 조금이 게임에 노력하고 봤는데, 여기 내 스파게티 코드를 무시합니다. 적의 배가 발사를 지연 시키지만, 배가 움직이는 경우에만 발사가 중지됩니다. 배가 움직이는 것을 멈 추면 하나의 총알이 아닌 여러 개의 시냇물이 발사됩니다. 나는 발사를 지연시키고, 1 초에 1 발의 총알을 발사하고, 실제로 우주선이 움직이는 동안 발사해야합니다. 지금 당장 적의 적을 거의 쓸모가 없다. 마치 약간 움직이면 발사되지 않을 것이고, 실제로는 쉽다. 도움을 주시면 감사하겠습니다.
내가 사용하는 것'next_fire = pygame.time.get_ticks() + N * 1000'는 다음 총알을 촬영 할 수 있습니다 시간을 설정 한 후 나는'확인 할 pygame.time.get_ticks()의 경우> = next_fire :'화재 전. 나는 다시 설정하겠다. next_fire = pygame.time.get_ticks() + n * 1000' – furas
코드에 올바른 들여 쓰기가 있는지 확실하지 않다. 올바르게 포맷하려면 '{}'버튼을 사용한다. – furas
BTW : 우주선이 움직일 때 우주선이 멈 추면 잘못된 들여 쓰기가 발생하여 일부 코드가 잘못된 경우 'if/else/for'에 있습니다. – furas