新手学习《python编程:从入门到实践》,在外星人入侵卡了两天。。。
向各位大佬求助,刚入门不到一个月的新人,在学习《python编程:从入门到实践》,第一个项目第12章“12.8.3将子弹存储到编组中”一直报错:Traceback (most recent call last):
File "D:/snake/venv/Alien_Invasion/main_game.py", line 2, in <module>
from pygame import Group
ImportError: cannot import name 'Group' from 'pygame' (D:\snake\venv\lib\site-packages\pygame\__init__.py)
import pygame
from settings import Settings
from ship import Ship
import game_functions as gf
import bullet
def run_game():
#初始化游戏并创建一个屏幕对象
pygame.init()
ai_settings = Settings()
screen = pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height))
pygame.display.set_caption("Alien_Invasion")
#创建一艘飞船
ship = Ship(ai_settings, screen)
#创建一个子弹编组
bullets = Group()
#游戏主循环
while True:
gf.check_events(ai_settings, screen, ship, bullets)
ship.update_ship()
bullets.update_bullets()
gf.update_screen(ai_settings, screen, ship, bullets)
run_game()
import pygame
class Bullet(pygame.sprite.Sprite):
"""一个对飞船发射的子弹管理的类"""
def __init__(self, ai_settings, screen, ship):
"""在飞船所在位置创建一个子弹对象"""
self.screen = screen
super(Bullet, self).__init__()
#在(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_bullets(self):
"""向上移动子弹"""
#更新浮点数的子弹纵坐标
self.y -= self.speed_factor
#更新子弹纵坐标位置
self.rect.y = self.y
def draw_bullet(self):
"""在屏幕上绘制子弹"""
pygame.draw.rect(screen, self.color, self.rect)
import sys
import pygame
def check_keydown_event(event, ai_settings, screen, ship, bullets):
if event.key == pygame.K_RIGHT:
ship.moving_right = True
elif event.key == pygame.K_LEFT:
ship.moving_left = True
#创建一个子弹,并加入bullets编组
elif event.key == pygame.K_SPACE:
new_bullet = Bullet(ai_settings, screen, ship)
bullets.add(new_bullet)
def check_keyup_event(event, 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()
elif event.type == pygame.KEYDOWN:
check_keydown_event(ai_settings, screen, ship, bullets)
elif event.type == pygame.KEYUP:
check_keyup_event(event, ship)
def update_screen(ai_settings, screen, ship, bullets):
"""更新屏幕上的图像并切换到新屏幕"""
#每次循环时都重新绘制屏幕
screen.fill(ai_settings.bg_color)
#在飞船和外星人后面重绘所有子弹
for bullet in bullets.sprites():
bullet.draw_bullet()
ship.blitme()
#最近绘制屏幕可见
pygame.display.flip()
from pygame import Group..
导入错误,pygame下面没这个Group模块。 你这代码是照书上敲的吗?
我是照书上敲的,但是和你这有很多不一样。
我这里有运行程序的类,还有两个重构方法(辅助方法),
可是我这个运行了以后就不行了,左右键都不好使,只有退出键还能用,不知道是什么原因。
import sys
import pygame
class AlienInvasion:
"""管理游戏资源和行为的类"""
def __int__(self):
"""初始化游戏,并创建游戏资源"""
pygame.init()
self.screen = pygame.display.set_mode((800,600))
def run_game(self):
"""开始游戏主循环"""
while True:
# 监测键盘与鼠标
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.display.flip()
if __name__ == '__main__':
# 创建游戏实例并运行
ai = AlienInvasion()
ai.run_game()我在一开始就卡住了,提示video system not initialized,你那是怎么通过 的? 我的解决了,正在向前看...... 是呀,和书上不一样
页:
[1]