这是代码,请详细说明帮助。我需要知道如何设置射击子弹的限制。如果你能告诉我如何使用我的代码来做这件事,那就太好了。我包括玩家类,子弹类,主循环和射击机械。
# 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()
答案 0 :(得分:1)
您可以创建一个将子弹计数作为属性的枪类,并且射击将是该类的函数。
class Gun(object):
def __init__(self):
self.bullet_count = 5
def __shoot(self, bullet_sprite, bullet_list):
if self.bullet_count > 0:
self.bullet_count -= 1
bullet_sprite.rect.x = player.rect.x
bullet_sprite.rect.y = player.rect.y
all_sprites_list.add(bullet_sprite)
bullet_list.add(bullet_sprite)
def shoot_left(self, bullet_list):
self.__shoot(Bullet(), bullet_list)
def shoot_right(self, bullet_list):
self.__shoot(Shoot(), bullet_list)
-------------------------------------------------------
然后,在您输入while循环之前,您将创建一个枪类的实例并按如下方式使用它:
if event.type == pygame.KEYDOWN and gun.bullet_count > 0:
print "KEY DOWN"
if event.key == K_LEFT:
gun.shoot_left(bullet_list)
if event.key == K_RIGHT:
gun.shoot_right(shoot_list)