-1
下面是代碼,請詳細說明幫助。我需要知道如何設定射擊子彈的限制。如果你能告訴我如何用我的代碼做到這一點很好。我包括球員類,子彈類,主循環和射擊機器人。如何限制子彈射擊
# This class represents the Player
class Player(pygame.sprite.Sprite):
def __init__(self):
# Call the parent class (Sprite) constructor
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface([20,20])
self.image.fill(red)
self.rect = self.image.get_rect()
# This class represents the bullet
class Bullet(pygame.sprite.Sprite):
def __init__(self):
# Call the parent class (Sprite) constructor
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface([10, 4])
self.image.fill(black)
self.rect = self.image.get_rect()
class Shoot(pygame.sprite.Sprite):
def __init__(self):
# Call the parent class (Sprite) constructor
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface([10, 4])
self.image.fill(black)
self.rect = self.image.get_rect()
# 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])
# This is a list of every sprite. All blocks and the player block as well.
all_sprites_list = pygame.sprite.Group()
# List of each bullet
bullet_list = pygame.sprite.Group()
shoot_list = pygame.sprite.Group()
# Create a red player block
player = Player()
all_sprites_list.add(player)
#Loop until the user clicks the close button.
done=False
# Used to manage how fast the screen updates
clock=pygame.time.Clock()
score = 0
player.rect.y=370
bullet_count=0
# -------- Main Program Loop -----------
while done==False:
# ALL EVENT PROCESSING SHOULD GO BELOW THIS COMMENT
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done=True # Flag that we are done so we exit this loop
if event.type == KEYDOWN:
if event.key == K_LEFT:
bullet = Bullet()
bullet.rect.x = player.rect.x
bullet.rect.y = player.rect.y
all_sprites_list.add(bullet)
bullet_list.add(bullet)
if event.key == K_RIGHT:
shoot = Shoot()
shoot.rect.x = player.rect.x
shoot.rect.y = player.rect.y
all_sprites_list.add(shoot)
shoot_list.add(shoot)
# ALL EVENT PROCESSING SHOULD GO ABOVE THIS COMMENT
# ALL GAME LOGIC SHOULD GO BELOW THIS COMMENT
# Calculate mechanics for each bullet
bulletCounter = 0
for bullet in bullet_list:
bullet.rect.x -= 5
for shoot in shoot_list:
shoot.rect.x -= -5
# Remove the bullet if it flies up off the screen
if bullet.rect.y < -10:
bullet_list.remove(bullet)
all_sprites_list.remove(bullet)
# Get the current mouse position. This returns the position
# as a list of two numbers.
pos = pygame.mouse.get_pos()
# Set the player x position to the mouse x position
player.rect.x=pos[0]
# ALL GAME LOGIC SHOULD GO ABOVE THIS COMMENT
# ALL CODE TO DRAW SHOULD GO BELOW THIS COMMENT
# Clear the screen
screen.fill(white)
# Draw all the spites
all_sprites_list.draw(screen)
# ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT
# Limit to 20 frames per second
clock.tick(20)
# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
pygame.quit()
我想這個時候deatailed解釋 –
*警告:變量' bulletCounter'已定義,但從未使用* ...... *警告:變量'bullet_count'已定義,但從未使用* ..... *警告:變量'score'已定義,但從未使用過* – scohe001
Count有多少人已被解僱,然後使用這些信息來防止在該數字超過限制時再拍攝。 – martineau