初学Python,练习pygame时出现如下错误?

题目:
初学Python,练习pygame时出现如下错误?

错误如下:
pygame 2.1.2 (SDL 2.0.18, Python 3.8.0)
Hello from the pygame community. https://www.pygame.org/contri...
Traceback (most recent call last):
File "12-5.py", line 45, in <module>

run_game()

File "12-5.py", line 34, in run_game

zidans.zidan_yidong()

AttributeError: 'Group' object has no attribute 'zidan_yidong'


具体代码:
1、12-5.py

import pygame

from pygame.sprite import Group

from settings import Settings

from ship import Ship

from zidan import Zidan

import game_hanshu as gh

def run_game():

"""初始化游戏设置"""

pygame.init()

youxi_set = Settings()

youxi_screen = pygame.display.set_mode((youxi_set.youxi_screen_width,

youxi_set.youxi_screen_height))

pygame.display.set_caption("我的游戏")

# 创建一艘飞船

ship = Ship(youxi_screen)

# 创建一个用于存储子弹的编组

zidans = Group()

# 开始游戏的主循环

while True:

# 监视鼠标键盘事件

gh.check_events(youxi_set,youxi_screen,ship,zidans)

# 控制飞船移动

gh.ship_yidong(youxi_set,youxi_screen,ship)

# 子弹移动

zidans.zidan_yidong()

# 绘制屏幕

youxi_screen.fill(youxi_set.youxi_bjs)

for zidan in zidans.sprites():

zidan.zidan_huizhi()

ship.blit_ship()

# 显示最新绘制的屏幕

pygame.display.flip()

run_game()

2、game_hanshu.py

import sys

import pygame

from settings import Settings

from ship import Ship

from zidan import Zidan

def check_events(youxi_set,youxi_screen,ship,zidans):

"""监视鼠标键盘事件"""

for event in pygame.event.get():

if event.type == pygame.QUIT:

sys.exit()

elif event.type == pygame.KEYDOWN:

if event.key == pygame.K_UP:

youxi_set.ship_shangyi = True

if event.key == pygame.K_DOWN:

youxi_set.ship_xiayi = True

if event.key == pygame.K_SPACE:

xinzidan = Zidan(youxi_set,youxi_screen,ship)

zidans.add(xinzidan)

elif event.type == pygame.KEYUP:

if event.key == pygame.K_UP:

youxi_set.ship_shangyi = False

if event.key == pygame.K_DOWN:

youxi_set.ship_xiayi = False

def ship_yidong(youxi_set,youxi_screen,ship):

"""移动飞船"""

youxi_screen_rect = youxi_screen.get_rect()

if youxi_set.ship_shangyi and ship.ship_image_rect.y > 0:

ship.ship_image_rect.y -= 2

if youxi_set.ship_xiayi and ship.ship_image_rect.bottom < youxi_screen_rect.bottom:

ship.ship_image_rect.y += 2

3、settings.py

class Settings():

"""游戏相关设置"""

def __init__(self):

"""初始化游戏的设置"""

# 屏幕设置

self.youxi_screen_width = 1200

self.youxi_screen_height = 800

self.youxi_bjs = (230, 230, 230)

# 飞船相关设置

self.ship_xiayi = False

self.ship_shangyi = False

# 子弹相关设置

self.zidan_width = 3

self.zidan_height = 15

self.zidan_yidong_sudu = 4

self.zidan_color = 60, 60, 60

4、ship.py

import pygame

class Ship():

"""初始化飞船并设置其初始位置"""

def __init__(self,youxi_screen):

self.youxi_screen = youxi_screen

# 加载飞船图像并设置其初始位置

self.youxi_screen_rect = self.youxi_screen.get_rect()

self.ship_image = pygame.image.load('images/ship.bmp')

self.ship_image_rect = self.ship_image.get_rect()

self.ship_image_rect.centery = self.youxi_screen_rect.centery

self.ship_image_rect.left = self.youxi_screen_rect.left

def blit_ship(self):

# 在指定位置绘制飞船

self.youxi_screen.blit(self.ship_image,self.ship_image_rect)

5、zidan.py

import pygame

from pygame.sprite import Sprite

class Zidan(Sprite):

"""一个对飞船发射子弹进行管理的类"""

def __init__(self,youxi_set,youxi_screen,ship):

"""在飞船所处的位置创建一个子弹对象"""

super(Zidan,self).__init__()

self.youxi_screen = youxi_screen

# 在(0,0)处创建一个表示子弹的矩形,在设置正确的位置

self.zidan_rect = pygame.Rect(0, 0, youxi_set.zidan_width,

youxi_set.zidan_height)

self.zidan_rect.centerx = ship.ship_image_rect.centerx

self.zidan_rect.top = ship.ship_image_rect.top

# 存储用小数表示的子弹位置

self.zidan_x = float(self.zidan_rect.x)

self.zidan_color = youxi_set.zidan_color

self.zidan_yidong_sudu = youxi_set.zidan_yidong_sudu

def zidan_yidong(self):

"""移动子弹"""

self.zidan_x += self.zidan_yidong_sudu

self.zidan_rect.x = self.zidan_x

def zidan_huizhi(self):

"""绘制子弹"""

pygame.draw.rect(self.youxi_screen,self.zidan_color,self.zidan_rect)

请各位大神帮忙看看问题出在哪了啊,感谢。

以上是 初学Python,练习pygame时出现如下错误? 的全部内容, 来源链接: utcz.com/p/938590.html

回到顶部