0 Nhiều viên đạn trong chương trình Alien_invasion của tôi không bắn khi tôi nhấn phím cách

câu hỏi được tạo ra tại Wed, May 8, 2019 12:00 AM

Đạn không bắn ra từ tàu vũ trụ của tôi khi tôi cố chạy chương trình python Alien_invasion của mình

Tôi đã tạo 6 tệp, nhưng tôi sẽ chỉ liệt kê bốn trong số các tệp vì chúng có liên quan đến vấn đề của tôi.

từ Bullet.py:

import pygame
from pygame.sprite import Sprite
class Bullet(Sprite):
"""A class to manage bullets fired from a ship """
def __init__(self,ai_settings,screen,ship):
    """Create a bullet object at the ships current position"""
    super(Bullet,self).__init__()
    self.screen=screen
    #Create a bullet rect at (0,0) and then set the correct posiiton
    self.rect=pygame.Rect(0,0,ai_settings.bullet_width,ai_settings.bullet_height)
    self.rect.centerx=ship.rect.centerx
    self.rect.top=ship.rect.top

    #Store bullets position as a decimal value
    self.y=float(self.rect.y)
    self.color=ai_settings.bullet_color
    self.speed_factor=ai_settings.bullet_speed_factor

def update(self):
    """Move the bullet up pthe screen"""
    #Update the decimal position of the bullets
    self.y-=self.speed_factor
    #Update the rect position
    self.rect.y=self.y
def draw_bullet(self):
    """Draw the bullet to the screen"""
    pygame.draw.rect(self.screen,self.color,self.rect)

từ game_feft.py:

   import sys

   import pygame
   from bullet import Bullet

def check_keydown_events(event,ai_settings,screen,ship,bullets):
     """Respond to keypresses"""
    if event.key==pygame.K_RIGHT:
        ship.moving_right=True
    elif event.key==pygame.K_LEFT:
        ship.moving_left=True
    elif event.key==pygame.K_SPACE:
        #Create a new bullet and add it to the bullets group
        new_bullet=Bullet(ai_settings,screen,ship)
        bullets.add(new_bullet)
   def check_keyup_events(event,ship):
     """Respond to key releases"""
    if event.key==pygame.K_RIGHT:
        ship.moving_right=False
    elif event.key==pygame.K_LEFT:
        ship.moving_left=False

   def check_events(ai_settings,screen,ship,bullets):
     """Respond to keypresses and mouse events"""
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            sys.exit()
        elif event.type==pygame.KEYDOWN:
            check_keydown_events(event,ai_settings,screen,ship,bullets)
        elif event.type==pygame.KEYUP:
            check_keyup_events(event,ship)

  def update_screen(ai_settings,screen,ship,bullets):
    """Update images on the screen and flip to the new screen"""
#Redraw all bullets behind ship and aliens

    for bullet in bullets.sprites():
       bullet.draw_bullet()
#Redraw he screen during each pass through the loop.

screen.fill(ai_settings.bg_color)
ship.blitme()

#Make the most recently drawn screen VISIBLE
pygame.display.flip()

từ Alien_invasion.py:

import sys
import pygame
from settings import Settings
from ship import Ship
import game_functions as gf
from pygame.sprite import Group
def run_game():
#Initialize game and create  a screen object
pygame.init()
ai_settings=Settings()
#Make a group that stores bullet
screen=pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height))    

#set the background color.
bg_color=(0,191,255)
#Start the main cloop for the game
#Make a Ship
ship=Ship(ai_settings,screen)
bullets=Group()
while True:
    gf.check_events(ai_settings,screen,ship,bullets)

    ship.update()
    bullets.update()
    gf.update_screen(ai_settings,screen,ship,bullets)


    #Watch for keyboard and mouse events.
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            sys.exit()



    #mAKE THE MOST RECENTLY DRAWN SCREEN VISIBLE
    #redraw the screen during each pass through the VISIBLE
    screen.fill(ai_settings.bg_color)
    ship.blitme()

    pygame.display.flip()

run_game()

từ settings.py:

class Settings():
"""A class to store all settings for Alien Invasion"""
    def __init__(self):
        #Initialize the games Settings
        #screen Settings
        self.screen_width=1200
        self.screen_height=800
        self.bg_color=(255,255,255)
            #Ship Settings
            self.ship_speed_factor=1.5

        #Bullet settings
        self.bullet_speed_factor=1
        self.bullet_width=3
        self.bullet_height=15
        self.bullet_color=0,0,0
    
0
  1. Để làm rõ, bạn có nghĩa là chỉ có một viên đạn được bắn khi giữ phím cách? Hay những viên đạn sẽ không bay chút nào?
    2019-05-08 17: 03: 28Z
  2. CNTT hoàn toàn không kích hoạt.
    2019-05-09 05: 06: 17Z
  3. Được rồi, tôi cố gắng đưa ra câu trả lời.
    2019-05-09 12: 29: 39Z
  4. Tôi vẫn không thể thực hiện bằng cách bắn đạn
    2019-05-09 15: 58: 38Z
0 Câu trả lời                              0                         
nguồn đặt đây