外星人入侵 - 创建模块时需要什么样的参数?
我是一个新手学习python,我遇到了参数问题。该代码来自 python速成课程,我正在尝试练习外星人的入侵代码。我的问题是创建新模块时需要哪种参数?这样的一个:
在模块中:“ game_functions”:
import sys
import pygame
def check_events():
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
def update_screen(ai_settings, screen, ship):
"""update images on the screen, and flip to the new screen."""
screen.fill(ai_settings.bg_color)
ship.blitme()
pygame.display.flip()
定义def def update_screen(ai_settings,屏幕,船,ship)
时,我不知道为什么当参数“ ship” ship”时需要参数“屏幕”已经包括“屏幕”作为参数? “船”已经在主要代码中包含“屏幕”参数:
主代码:
import sys
import pygame
from settings import Settings
from ship import Ship
import game_functions as gf
def run_game():
# initializing game and create a screen object
pygame.init()
ai_settings = Settings()
screen = pygame.display.set_mode(
(ai_settings.screen_width, ai_settings.screen_height))
pygame.display.set_caption("Alien Invasion")
#create a ship
ship = Ship(screen)
#start the main loop of the game
while True:
gf.check_events()
gf.update_screen(ai_settings, screen, ship)
run_game()
我的问题是: 当主要代码中已经定义了新模块时,如何决定将哪些参数放入新模块中?
对不起,我的英语表情不佳,并感谢您能提供帮助的任何人!
更多模块:
船舶模块是:
import pygame
class Ship():
def __init__(self,screen):
"""Initialize the ship and set its starting position"""
self.screen = screen
#Load the ship image and get its rect.
self.image = pygame.image.load('images/ship.bmp')
self.rect = self.image.get_rect()
self.screen_rect = screen.get_rect()
#Start each new ship at the bottom center of the screen
self.rect.centerx = self.screen_rect.centerx
self.rect.bottom = self.screen_rect.bottom
def blitme(self):
"""Draw the ship at its current location"""
self.screen.blit(self.image, self.rect)
设置模块是:
class Settings():
"""A class to store all settings for Alien Invasion."""
def __init__(self):
"""Initialize the game's settings."""
#Screen settings
self.screen_width = 1200
self.screen_height = 800
self.bg_color = (230, 230, 230)
对不起,这是我第一次提出问题。我的问题是,当我要创建一个函数时(这是在我发布的第一个代码中,称为def update_screen(ai_settings,屏幕,ship)
(将导入到原始代码中),一个参数对于原始代码中已经定义的称为“船”的功能,我是否仍需要将所需的参数放置在新功能中,这是“屏幕”,为什么感谢您的帮助!
I'm a newbie learning Python and I ran into the problem of parameters. The code is from the book Python Crash Course and I'm trying to practice the Alien Invasion code. My question is what kind of parameters are needed when a new module is created? Such as this one:
in the module "game_functions":
import sys
import pygame
def check_events():
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
def update_screen(ai_settings, screen, ship):
"""update images on the screen, and flip to the new screen."""
screen.fill(ai_settings.bg_color)
ship.blitme()
pygame.display.flip()
when defining def update_screen(ai_settings, screen, ship)
, I don't know why the parameter "screen" is required when the parameter "ship" already includes "screen" as its parameter? "ship" already includes "screen" parameter in the main code:
main code:
import sys
import pygame
from settings import Settings
from ship import Ship
import game_functions as gf
def run_game():
# initializing game and create a screen object
pygame.init()
ai_settings = Settings()
screen = pygame.display.set_mode(
(ai_settings.screen_width, ai_settings.screen_height))
pygame.display.set_caption("Alien Invasion")
#create a ship
ship = Ship(screen)
#start the main loop of the game
while True:
gf.check_events()
gf.update_screen(ai_settings, screen, ship)
run_game()
my question is:
how to decide what parameters are to be put into the new module, when they are already defined in the main code?
Sorry for my poor English expression and thank you for anyone who could help!
more modules:
the ship module is:
import pygame
class Ship():
def __init__(self,screen):
"""Initialize the ship and set its starting position"""
self.screen = screen
#Load the ship image and get its rect.
self.image = pygame.image.load('images/ship.bmp')
self.rect = self.image.get_rect()
self.screen_rect = screen.get_rect()
#Start each new ship at the bottom center of the screen
self.rect.centerx = self.screen_rect.centerx
self.rect.bottom = self.screen_rect.bottom
def blitme(self):
"""Draw the ship at its current location"""
self.screen.blit(self.image, self.rect)
the setting module is:
class Settings():
"""A class to store all settings for Alien Invasion."""
def __init__(self):
"""Initialize the game's settings."""
#Screen settings
self.screen_width = 1200
self.screen_height = 800
self.bg_color = (230, 230, 230)
sorry for the confusion this is the first time I asked a question. My question is, when I want to create a function(here is in the first code I posted, called def update_screen(ai_settings, screen, ship)
(that will be imported into original code), a parameter for the function called "ship" is already defined in the original code, do I still need to put parameter that ship required, which is called "screen", in the new function and why? Thank you for helping!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果
屏幕
被传递到update_screen
与ship.screen
相同,那么您可以使用ship.spreen
而是避免额外的参数。但是,有时您可能想将船吹到与主屏幕不同的表面上,因此将其分开使其更容易做到。如果在任何时候要卸下船,那么您仍然被迫将ship
参数保留在功能中,因为它包含屏幕
,即使您不会在画船。此外,保持屏幕
参数可提高可读性。If the
screen
being passed intoupdate_screen
is the same as theship.screen
then you could useship.screen
instead, avoiding the extra parameter. However, sometimes you might want to blit the ship onto a different surface than the main screen, so keeping them separate allows that to be done easier. If at any point you want to remove the ship, then you would still be forced to keep theship
parameter in the function because it contains thescreen
even though you wouldn't be drawing the ship. Additionally, keeping thescreen
parameter improves readibility.