代码1
_import sys
import pygame
from settings import Settings
from ship import Ship
def run_game():
#初始化Pygame、并设置和屏幕对象#
pygame.init()
ai_settings = S_ettings()
screen = pygame.display.set_mode(
(ai_settings.screen_wi_dth, ai_settings.screen_height))
pygame.display.set_caption("Alien Invasion")
#创建一艘飞船#
ship = Ship(screen)
# 开始游戏主循环#
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
#每次循环时都重绘屏幕#
screen.fill(ai_settings.bg_color)
ship.blitme()
#让最近绘制的屏幕可见#
pygame.display.flip()
run_game()_
代码2
import pygame
class Ship():
def __int__(self, screen):
"""初始化飞船并设置其初始位置"""
self.screen = screen
#加载飞船图像并获取外接矩形#
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
def blitme(self):
"""在指定位置绘制飞船"""
self.screen.blit(self.image, self.rect)
代码3.
class Settings():
#储存《外星人入侵》的所有设置的类#
def __init__(self):
"""初始化游戏设置"""
self.screen_width = 1200
self.screen_height = 800
self.bg_color = (230, 230, 230)
运行alien_invasion.py 之后,出现全黑的窗口,然后窗口自动退出
请问什么问题呢?
下面是提示信息:
C:\Users\pc\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/pc/python_tool/alien_invasion/alien_invasion.py
pygame 2.1.2 (SDL 2.0.18, Python 3.10.2)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "C:\Users\pc\python_tool\alien_invasion\alien_invasion.py", line 29, in
run_game()
File "C:\Users\pc\python_tool\alien_invasion\alien_invasion.py", line 15, in run_game
ship = Ship(screen)
TypeError: Ship() takes no arguments
Process finished with exit code 1
恳请大家指正