鱼柒哥哥 发表于 2021-3-26 11:26:08

各位帮忙看看错在哪里?(飞机大战)谢谢

import random
import time
import pygame# 导入pygame模块
from pygame.constants import *


# 我方飞机类属性
class HeroPlane(pygame.sprite.Sprite):
    def __init__(self, screen):
      pygame.sprite.Sprite.__init__(self)
      self.player = pygame.image.load("./feiji/hero.gif")# 导入我方飞机
      self.rect = self.image.get_rect()
      self.rect.topleft =
      self.speed = 10# 飞机速度
      self.screen = screen
      self.bullste = pygame.sprite.Group()

    def key_control(self):
      key_pressed = pygame.key.get_pressed()
      if key_pressed or key_pressed:
            self.rect.top -= self.speed
      if key_pressed or key_pressed:
            self.rect.bottom += self.speed
      if key_pressed or key_pressed:
            self.rect.left -= self.speed
      if key_pressed or key_pressed:
            self.rect.right += self.speed
      if key_pressed:
            bullet = Bullet(self.screen, self.rect.left, self.rect.top)
            self.bullste.add(bullet)

    def update(self):
      self.key_control()
      self.display()

    def display(self):
      self.screen.blit(self.player, self.rect)# 我方飞机坐标
      self.bullste.update()
      self.bullste.draw(self.screen)


# 敌方飞机类属性
class EnemyPlane(pygame.sprite.Sprite):
    def __init__(self, screen):
      pygame.sprite.Sprite.__init__(self)
      self.player = pygame.image.load("./feiji/enemy0.png.gif")# 导入我方飞机
      self.rect = self.image.get_rect()
      self.rect.topleft =
      self.speed = 10# 飞机速度
      self.screen = screen
      self.bullste = pygame.sprite.Group()
      self.dirextion = 'right'

    def display(self):
      self.screen.blit(self.player, self.rect)# 我方飞机坐标
      for _ in self.bullste:
            self.bullste.update()
            self.bullste.draw(self.screen)

    def update(self):
      self.auto_move()
      self.auto_fire()
      self.display()

    def auto_move(self):
      if self.dirextion == 'right':
            self.rect.right += self.speed
      elif self.dirextion == 'left':
            self.rect.right -= self.speed
      if self.rect.right > 480 - 51:
            self.dirextion = 'left'
      elif self.rect.right < 0:
            self.dirextion = 'right'

    def auto_fire(self):
      random_num = random.randint(1, 20)
      if random_num == 8:
            bullet = EnemyBullet(self.screen, self.rect.left, self.rect.top)
            self.bullste.add(bullet)


# 子弹类属性
class Bullet(pygame.sprite.Sprite):
    def __init__(self, screen, x, y):
      pygame.sprite.Sprite.__init__(self)
      self.image = pygame.image.load("./feiji/bullet.png")
      self.rect = self.image.get_rect()
      self.rect.topleft =
      self.screen = screen
      self.speed = 20

    def update(self):
      self.rect.top -= self.speed
      if self.rect.top < -22:
            self.kill()


# 敌方子弹类属性
class EnemyBullet(pygame.sprite.Sprite):
    def __init__(self, screen, x, y):
      pygame.sprite.Sprite.__init__(self)
      self.image = pygame.image.load("./feiji/bullet1.png.png")
      self.rect = self.image.get_rect()
      self.rect.topleft =
      self.screen = screen
      self.speed = 20

    def update(self):
      self.rect.top += self.speed
      if self.rect.top > 852:
            self.kill()


def main():
    """完成整个程序的控制"""
    screen = pygame.display.set_mode((480, 852), 0, 32)# 创建窗口
    backgeound = pygame.image.load("./feiji/background.png")# 导入图片,做背景图
    player = HeroPlane(screen)
    enemyplane = EnemyPlane(screen)
    while True:
      screen.blit(backgeound, (0, 0))# 背景图的坐标
      for event in pygame.event.get():# 获取事件
            if event.type == pygame.QUIT:# 判断事件类型
                pygame.quit()# 执行pygame程序退出
                exit()# pygame程序退出
      player.key_control()
      player.display()
      enemyplane.display()
      enemyplane.auto_move()
      enemyplane.auto_fire()
      pygame.display.update()# 显示窗口内容
      time.sleep(0.01)# 操作键盘的反应时间


if __name__ == '__main__':
    main()

小伤口 发表于 2021-3-26 11:27:19

你的报错内容是什么呀{:5_102:}

鱼柒哥哥 发表于 2021-3-26 11:32:41

小伤口 发表于 2021-3-26 11:27
你的报错内容是什么呀

C:\Users\lenovo\AppData\Local\Programs\Python\Python39\python.exe D:/pythonProject/003飞机大战.py
pygame 2.0.1 (SDL 2.0.14, Python 3.9.0)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "D:\pythonProject\003飞机大战.py", line 136, in <module>
    main()
File "D:\pythonProject\003飞机大战.py", line 118, in main
    player = HeroPlane(screen)
File "D:\pythonProject\003飞机大战.py", line 12, in __init__
    self.rect = self.image.get_rect()
AttributeError: 'HeroPlane' object has no attribute 'image'

进程已结束,退出代码为 1
这是报错内容

鱼柒哥哥 发表于 2021-3-26 11:33:21

C:\Users\lenovo\AppData\Local\Programs\Python\Python39\python.exe D:/pythonProject/003飞机大战.py
pygame 2.0.1 (SDL 2.0.14, Python 3.9.0)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "D:\pythonProject\003飞机大战.py", line 136, in <module>
    main()
File "D:\pythonProject\003飞机大战.py", line 118, in main
    player = HeroPlane(screen)
File "D:\pythonProject\003飞机大战.py", line 12, in __init__
    self.rect = self.image.get_rect()
AttributeError: 'HeroPlane' object has no attribute 'image'

进程已结束,退出代码为 1

鱼柒哥哥 发表于 2021-3-26 11:34:09

C:\Users\lenovo\AppData\Local\Programs\Python\Python39\python.exe D:/pythonProject/003飞机大战.py
pygame 2.0.1 (SDL 2.0.14, Python 3.9.0)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "D:\pythonProject\003飞机大战.py", line 136, in <module>
    main()
File "D:\pythonProject\003飞机大战.py", line 118, in main
    player = HeroPlane(screen)
File "D:\pythonProject\003飞机大战.py", line 12, in __init__
    self.rect = self.image.get_rect()
AttributeError: 'HeroPlane' object has no attribute 'image'

进程已结束,退出代码为 1

鱼柒哥哥 发表于 2021-3-26 11:36:56

小伤口 发表于 2021-3-26 11:27
你的报错内容是什么呀

C:\Users\lenovo\AppData\Local\Programs\Python\Python39\python.exe D:/pythonProject/003飞机大战.py
pygame 2.0.1 (SDL 2.0.14, Python 3.9.0)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "D:\pythonProject\003飞机大战.py", line 136, in <module>
    main()
File "D:\pythonProject\003飞机大战.py", line 118, in main
    player = HeroPlane(screen)
File "D:\pythonProject\003飞机大战.py", line 12, in __init__
    self.rect = self.image.get_rect()
AttributeError: 'HeroPlane' object has no attribute 'image'

进程已结束,退出代码为 1

鱼柒哥哥 发表于 2021-3-26 11:43:16

小伤口 发表于 2021-3-26 11:27
你的报错内容是什么呀

C:\Users\lenovo\AppData\Local\Programs\Python\Python39\python.exe D:/pythonProject/003飞机大战.py
pygame 2.0.1 (SDL 2.0.14, Python 3.9.0)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "D:\pythonProject\003飞机大战.py", line 136, in <module>
    main()
File "D:\pythonProject\003飞机大战.py", line 118, in main
    player = HeroPlane(screen)
File "D:\pythonProject\003飞机大战.py", line 12, in __init__
    self.rect = self.image.get_rect()
AttributeError: 'HeroPlane' object has no attribute 'image'

进程已结束,退出代码为 1

鱼柒哥哥 发表于 2021-3-26 11:58:12

C:\Users\lenovo\AppData\Local\Programs\Python\Python39\python.exe D:/pythonProject/003飞机大战.py
pygame 2.0.1 (SDL 2.0.14, Python 3.9.0)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "D:\pythonProject\003飞机大战.py", line 136, in <module>
    main()
File "D:\pythonProject\003飞机大战.py", line 118, in main
    player = HeroPlane(screen)
File "D:\pythonProject\003飞机大战.py", line 12, in __init__
    self.rect = self.image.get_rect()
AttributeError: 'HeroPlane' object has no attribute 'image'

进程已结束,退出代码为 1

小伤口 发表于 2021-3-26 14:10:23

你再好好看是不是你的图片地址写错了呢heroplan那段函数的图片{:10_254:}

lixiangyv 发表于 2021-3-28 19:41:50

报错的内容的意思是 第 12 行,HeroPlane 这个类中没有 image 属性,
应该把第 12 行 self.rect = self.image.get_rect() 中的 image 换成 player,因为你上面导入图片赋值的变量是 player,
或者把第 11 行的 player 换成 image。
页: [1]
查看完整版本: 各位帮忙看看错在哪里?(飞机大战)谢谢