alien_invision游戏,无法射击

huangapple go评论78阅读模式
英文:

alien_invision game ,can not shooting

问题

以下是您提供的代码的翻译部分:

alien_invision game,我不知道为什么

alien_invision.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():
    '''初始化游戏并创建一个屏幕对象'''
    pygame.init()

    '''导入设置'''
    ai_settings = Settings()

    #创建一个屏幕对象
    screen = pygame.display.set_mode((1200, 600))

    #创建游戏标题
    pygame.display.set_caption('快乐游戏')

    #创建一艘飞船
    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)

    

run_game()

bullet.py

import pygame
from pygame.sprite import Sprite

class Bullet(Sprite):
    '''一个对飞船发射的子弹进行管理的类'''
    def __init__(self, ai_settings, screen, ship):
        '''在飞船所处的位置创建一个子弹对象'''
        super().__init__()
        self.screen = screen

        # 在(0,0)处创建一个表示子弹的矩形,再设置正确的位置
        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

        #存储用小数表示的子弹位置
        self.y = float(self.rect.y)

        self.color = ai_settings.bullet_color
        self.speed_factor = ai_settings.bullet_speed_factor


    def update(self):
        '''向上移动子弹'''
        # 更新表示子弹位置的小数值
        self.y -= self.speed_factor

        #更新表示子弹的rect的位置
        self.rect.y = self.y
    
    def draw_bullet(self):
        '''在屏幕上绘制子弹'''
        pygame.draw.rect(self.screen, self.color, self.rect)

game_functions.py

import sys
import pygame
from bullet import Bullet

def check_keydown_events(event, ai_settings, ship, bullets):
    '''响应按键'''
    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:
        # 创建一颗子弹,并将其加入到编组bullets中
        new_bullet = Bullet(ai_settings, screen, ship)
        '''为什么会报错?screen is not defined'''
        bullets.add(new_bullet)


def check_keyup_events(event, ai_settings, ship, ):
    '''响应松开'''
    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):
    '''响应鼠标和键盘按键'''
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit(0)
        elif event.type == pygame.KEYDOWN:
            check_keydown_events(event, ai_settings, ship, bullets)
        elif event.type == pygame.KEYUP:
            check_keyup_events(event, ai_settings, ship)

                    
def update_screen(ai_settings, screen, ship, bullets): 
    """更新屏幕上的图像,并切换到新屏幕"""
    # 每次循环时都重绘屏幕
    screen.fill(ai_settings.bg_color)
    #在飞船和外星人后面重绘所有子弹
    for bullet in bullets.sprites():
        bullet.draw_bullet()
    ship.biltme()

    #让最近绘制的屏幕可见
    pygame.display.flip()

settings.py

class Settings():
    '''存储所有设置的类'''

    def __init__(self):
        '''初始化游戏的设置'''
        #屏幕设置
        self.screen_width = 1200
        self.screen_height = 600

        #设置背景色
        self.bg_color = (87, 250, 255)

        #飞船速度的设置因子
        self.ship_speed_factor = 1.5

        # 子弹设置
        self.bullet_speed_factor = 1
        self.bullet_width = 3
        self.bullet_height = 15
        self.bullet_color = 60, 60, 60

ship.py

import pygame

class Ship():
    def __init__(self, ai_settings, screen):
        '''初始化飞船位置并设置其初始位置'''
        self.screen = screen
        self.ai_settings = ai_settings

        #加载飞船图像并获取其外接矩形
        self.image = pygame.image.load('images/ship.bmp')
        self.rect = self.image.get_rect()
        self.screen_rect = screen.get_rect()

        #将每艘新飞船放在屏幕底部中央
        self.rect.centerx = self.screen_rect.centerx
        self.rect.bottom = self.screen_rect.bottom

        #在飞船的属性center中存储小数值
        self.center = float(self.rect.centerx)

        # 移动标识
        self.moving_right = False
        self.moving_left = False

    def update(self):
        # 根据移动标志调整飞船的位置
        # 更新飞船的center值而不是rect
        if self.moving_right:
            self.center += self.ai_settings.ship_speed_factor
        if self.moving_left:
            self.center -= self.ai_settings.ship_speed_factor

        #根据self.center更新rect对象
        self.rect.centerx = self.center

    def biltme(self):
        '''在指定位置绘制飞船'''
        self.screen.blit(self.image, self.rect)

希望这些翻译对您有所帮助。如果您需要任何进一步

英文:

alien_invision game,i don't know why

alien_invision游戏,无法射击

alien_invision.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():
    '''初始化游戏并创建一个屏幕对象'''
    pygame.init()

    '''导入设置'''
    ai_settings = Settings()

    #创建一个屏幕对象
    screen = pygame.display.set_mode((1200, 600))

    #创建游戏标题
    pygame.display.set_caption('happy game')

    #创建一艘飞船
    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)

    

run_game()

bullet.py

import pygame
from pygame.sprite import Sprite

class Bullet(Sprite):
    '''一个对飞船发射的子弹进行管理的类'''
    def __init__(self, ai_settings, screen, ship):
        '''在飞船所处的位置创建一个子弹对象'''
        super().__init__()
        self.screen = screen

        # 在(0,0)处创建一个表示子弹的矩形,再设置正确的位置
        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

        #存储用小数表示的子弹位置
        self.y = float(self.rect.y)

        self.color = ai_settings.bullet_color
        self.speed_factor = ai_settings.bullet_speed_factor


    def update(self):
        '''向上移动子弹'''
        # 更新表示子弹位置的小数值
        self.y -= self.speed_factor

        #更新表示子弹的rect的位置
        self.rect.y = self.y
    
    def draw_bullet(self):
        '''在屏幕上绘制子弹'''
        pygame.draw.rect(self.screen, self.color, self.rect)

game_functions.py

import sys
import pygame
from bullet import Bullet

def check_keydown_events(event, ai_settings, ship, bullets):
    '''响应按键'''
    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:
        # 创建一颗子弹,并将其加入到编组bullets中
        new_bullet = Bullet(ai_settings, screen, ship)
        '''为什么会报错?screen is not defined'''
        bullets.add(new_bullet)


def check_keyup_events(event, ai_settings, ship, ):
    '''响应松开'''
    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):
    '''响应鼠标和键盘按键'''
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit(0)
        elif event.type == pygame.KEYDOWN:
            check_keydown_events(event, ai_settings, ship, bullets)
        elif event.type == pygame.KEYUP:
            check_keyup_events(event, ai_settings, ship)

                    
def update_screen(ai_settings, screen, ship, bullets): 
    """更新屏幕上的图像,并切换到新屏幕"""
    # 每次循环时都重绘屏幕
    screen.fill(ai_settings.bg_color)
    #在飞船和外星人后面重绘所有子弹
    for bullet in bullets.sprites():
        bullet.draw_bullet()
    ship.biltme()

    #让最近绘制的屏幕可见
    pygame.display.flip()

settings.py

class Settings():
    '''存储所有设置的类'''

    def __init__(self):
        '''初始化游戏的设置'''
        #屏幕设置
        self.screen_width = 1200
        self.screen_height = 600

        #设置背景色
        self.bg_color = (87, 250, 255)

        #飞船速度的设置因子
        self.ship_speed_factor = 1.5

        # 子弹设置
        self.bullet_speed_factor = 1
        self.bullet_width = 3
        self.bullet_height = 15
        self.bullet_color = 60, 60, 60

ship.py

import pygame

class Ship():
    def __init__(self, ai_settings, screen):
        '''初始化飞船位置并设置其初始位置'''
        self.screen = screen
        self.ai_settings = ai_settings

        #加载飞船图像并获取其外接矩形
        self.image = pygame.image.load('images/ship.bmp')
        self.rect = self.image.get_rect()
        self.screen_rect = screen.get_rect()

        #将每艘新飞船放在屏幕底部中央
        self.rect.centerx = self.screen_rect.centerx
        self.rect.bottom =self.screen_rect.bottom

        #在飞船的属性center中存储小数值
        self.center = float(self.rect.centerx)

        # 移动标识
        self.moving_right = False
        self.moving_left = False

    def update(self):
        # 根据移动标志调整飞船的位置
        # 更新飞船的center值而不是rect
        if self.moving_right:
            self.center += self.ai_settings.ship_speed_factor
        if self.moving_left:
            self.center -= self.ai_settings.ship_speed_factor

        #根据self.center更新rect对象
        self.rect.centerx = self.center

    def biltme(self):
        '''在指定位置绘制飞船'''
        self.screen.blit(self.image, self.rect)

答案1

得分: 1

screen 未在函数 check_keydown_events 中定义。

在函数中添加一个名为 screen 的参数:

def check_keydown_events(event, ai_settings, screen, ship, bullets):
    # [...]

然后在 check_events 中将 screen 传递给 check_keydown_events

def check_events(ai_settings, screen, ship, bullets):
    # 响应鼠标和键盘按键
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit(0)
        elif event.type == pygame.KEYDOWN:
            check_keydown_events(event, ai_settings, screen, ship, bullets)
        elif event.type == pygame.KEYUP:
            check_keyup_events(event, ai_settings, ship)
英文:

screen not defined in the function check_keydown_events.

Add a parameter screen to the function:

def check_keydown_events(event, ai_settings, screen, ship, bullets):
    # [...]

And pass screen to check_keydown_events in check_events:

def check_events(ai_settings, screen, ship, bullets):
    '''响应鼠标和键盘按键'''
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit(0)
        elif event.type == pygame.KEYDOWN:
            check_keydown_events(event, ai_settings, screen, ship, bullets)
        elif event.type == pygame.KEYUP:
            check_keyup_events(event, ai_settings, ship)

答案2

得分: 0

屏幕在这些函数的范围内不存在,因为您已经将其传递给check_events,您可以直接传递它(请注意check_keydown_events的额外参数)。

def check_keydown_events(event, ai_settings, ship, bullets, screen):
    '''响应按键'''
    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:
        # 创建一颗子弹,并将其加入到编组bullets中
        new_bullet = Bullet(ai_settings, screen, ship)
        '''为什么会报错?screen is not defined'''
        bullets.add(new_bullet)


def check_events(ai_settings, screen, ship, bullets):
    '''响应鼠标和键盘按键'''
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit(0)
        elif event.type == pygame.KEYDOWN:
            check_keydown_events(event, ai_settings, ship, bullets, screen)
        elif event.type == pygame.KEYUP:
            check_keyup_events(event, ai_settings, ship)
英文:

screen doesn't exist within the scope of those functions, since you already pass it to check_events you can just pass it along, (note the extra parameter to check_keydown_events)

def check_keydown_events(event, ai_settings, ship, bullets, screen):
    '''响应按键'''
    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:
        # 创建一颗子弹,并将其加入到编组bullets中
        new_bullet = Bullet(ai_settings, screen, ship)
        '''为什么会报错?screen is not defined'''
        bullets.add(new_bullet)


def check_events(ai_settings, screen, ship, bullets):
    '''响应鼠标和键盘按键'''
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit(0)
        elif event.type == pygame.KEYDOWN:
            check_keydown_events(event, ai_settings, ship, bullets, screen)
        elif event.type == pygame.KEYUP:
            check_keyup_events(event, ai_settings, ship)

huangapple
  • 本文由 发表于 2020年1月6日 21:27:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612986.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定