猪猪虾 发表于 2020-5-8 12:25:09

打飞机 ,182行报错,'Myplane' object has no attribute 'rect'


import sys
import traceback
from pygame.locals import *
import pygame
import random
from random import *
import math

pygame.init()
pygame.mixer.init()


#载入游戏音乐
pygame.mixer.music.load("sound/game_music.ogg")
pygame.mixer.music.set_volume(0.2)
bullet_sound = pygame.mixer.Sound("sound/bullet.wav")
bullet_sound.set_volume(0.2)
bomb_sound = pygame.mixer.Sound("sound/use_bomb.wav")
bomb_sound.set_volume(0.2)
supply_sound = pygame.mixer.Sound("sound/supply.wav")
supply_sound.set_volume(0.2)
get_bomb_sound = pygame.mixer.Sound("sound/get_bomb.wav")
get_bomb_sound.set_volume(0.2)
get_bullet_sound = pygame.mixer.Sound("sound/get_bullet.wav")
get_bullet_sound.set_volume(0.2)
upgrade_sound = pygame.mixer.Sound("sound/upgrade.wav")
upgrade_sound.set_volume(0.2)
enemy3_fly_sound = pygame.mixer.Sound("sound/enemy3_flying.wav")
enemy3_fly_sound.set_volume(0.2)
enemy1_down_sound = pygame.mixer.Sound("sound/enemy1_down.wav")
enemy1_down_sound.set_volume(0.2)
enemy2_down_sound = pygame.mixer.Sound("sound/enemy2_down.wav")
enemy2_down_sound.set_volume(0.2)
enemy3_down_sound = pygame.mixer.Sound("sound/enemy3_down.wav")
enemy3_down_sound.set_volume(0.2)
me_down_sound = pygame.mixer.Sound("sound/me_down.wav")
me_down_sound.set_volume(0.2)


class Myplane(pygame.sprite.Sprite):
    def __init__(self,bg_size):
      pygame.sprite.Sprite.__init__(self)#继承类

      self.myplane_1_image = pygame.image.load("image/me1.png").convert_alpha()
      self.myplane_2_image = pygame.image.load("image/me2.png").convert_alpha()
      self.destroy_image1 = pygame.image.load("image/me_destroy_1.png").convert_alpha()
      self.destroy_image2 = pygame.image.load("image/me_destroy_2.png").convert_alpha()
      self.destroy_image3 = pygame.image.load("image/me_destroy_3.png").convert_alpha()
      self.myplane_1_rect = self.myplane_1_image.get_rect()
      self.myplane_1_rect.left,self.myplane_1_rect.top = \
                                 (bg_size - self.myplane_1_rect.width) // 2 ,\
                                  bg_size - self.myplane_1_rect.height
      self.screen_width,self.screen_height = bg_size,bg_size
      self.speed =

      #如果按下W,A,S,D,表示飞机分别向上下左右移动
      self.press_can_K_w = True
      self.press_can_K_s = True
      self.press_can_K_a = True
      self.press_can_K_d = True

      
    def move(self):      
      if self.myplane_1_rect.left< 0:#超过左边界,键盘a按下无效
            self.myplane_1_rect.left = 0
            self.press_can_K_a = False
            
      elif self.myplane_1_rect.right >self.screen_width:
            self.myplane_1_rect.right = self.screen_width
            self.press_can_K_d = False

      elifself.myplane_1_rect.top< 0 :
            self.myplane_1_rect.top = 0
            self.press_can_K_w = False
            
      elif self.myplane_1_rect.bottom >self.screen_height:
            self.myplane_1_rect.bottom = self.screen_height
            self.press_can_K_d = False
            
      else:
            self.press_can_K_w = True
            self.press_can_K_s = True
            self.press_can_K_a = True
            self.press_can_K_d = True


class Enemyplane(pygame.sprite.Sprite):
    def __init__(self,bg_size,position):
      pygame.sprite.Sprite.__init__(self)#继承类

      self.image = pygame.image.load("image/enemy1.png").convert_alpha()
      self.breakdown_image1 = pygame.image.load("image/enemy1_down1.png").convert_alpha()
      self.breakdown_image2 = pygame.image.load("image/enemy2_down1.png").convert_alpha()

      self.rect = self.image.get_rect()
      self.width,self.height =bg_size, bg_size
      self.rect.left,self.rect.top = position
      self.speed = 5
                  
    def enemy_move(self):
      if self.rect.top < self.height:
            self.rect.top += self.speed
      else:
            self.reset()

    def reset(self):
      self.rect.left,self.rect.top = \
                                     randint(0,self.width-self.rect.width),\
                                     randint(-2*self.height,0)


def main():
    pygame.init()
    bg_size = width,height = 480,700
    screen = pygame.display.set_mode(bg_size)
    pygame.display.set_caption("飞机大战")
    background = pygame.image.load("image/background.png").convert()

    BLACK = (0,0,0)
    GREEN = (0,255,0)
    RED = (255,0,0)

   
    pygame.mixer.music.play(-1)   #无限循环背景
    clock = pygame.time.Clock()
    pygame.key.set_repeat(10,10)

    #生成我方飞机
    myplane = Myplane(bg_size)
   
    group = pygame.sprite.Group()
    #在屏幕最上方随机产生小飞机
    position = randint(0,width-40),0
    you = Enemyplane(bg_size,position)
    group.add(you)

    #延时
    delay = 100
      
    #用于切换图片
    switch_image = True

   
    running = True
    while running:
      for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()

            elif event.type == KEYDOWN:
                myplane.move()
                if event.key == K_w and myplane.press_can_K_w == True :
                  myplane.myplane_1_rect.top -= 10
                     
                if event.key == K_s and myplane.press_can_K_s == True :
                  myplane.myplane_1_rect.bottom += 10   

                if event.key == K_a and myplane.press_can_K_a == True :
                  myplane.myplane_1_rect.left -= 10
                     
                if event.key == K_d and myplane.press_can_K_d == True :
                  myplane.myplane_1_rect.right += 10
                     
      screen.blit(background,(0,0))


      #绘制我方飞机
      switch_image = not switch_image#取反,实现不断切换
      if switch_image:
            screen.blit(myplane.myplane_1_image,myplane.myplane_1_rect)
      else:
            screen.blit(myplane.myplane_2_image,myplane.myplane_1_rect)


      #绘制敌方飞机
      for each in group:
            each.enemy_move()
            screen.blit(you.image,you.rect)

      if pygame.sprite.spritecollide(myplane, group, False):
            screen.blit(you.breakdown_image1,you.rect)
            screen.blit(myplane.destroy_image1,myplane.myplane_1_rect)
            screen.blit(myplane.destroy_image2,myplane.myplane_1_rect)
            screen.blit(myplane.destroy_image3,myplane.myplane_1_rect)
            you.speed = 0
            myplane.speed = 0
      
      
      pygame.display.flip()
      clock.tick(60)
      
      
      
if __name__ =="__main__":
    #在双击打开文件时,如果出现异常,将异常显示出来
    try:
      main()
    except SystemExit:
      pass
    except:
      traceback.print_exc()
      pygame.quit()
      input()



qiuyouzhi 发表于 2020-5-8 12:26:22

不应该是myplane_1_rect吗?你看你Myplane里面的定义

老八秘制 发表于 2020-5-8 12:49:22

检查代码

猪猪虾 发表于 2020-5-8 12:54:24

qiuyouzhi 发表于 2020-5-8 12:26
不应该是myplane_1_rect吗?你看你Myplane里面的定义

185行,我写的是myplane_1_rect,没懂你的意思

猪猪虾 发表于 2020-5-8 12:57:47

老八秘制 发表于 2020-5-8 12:49
检查代码

我其他模块运行都没有问题,就是碰撞检测这,我看来看去,还是没找到啥问题

txxcat 发表于 2020-5-8 12:58:00

56行加一句:
self.rect = self.myplane_1_image.get_rect()

qiuyouzhi 发表于 2020-5-8 13:00:45

猪猪虾 发表于 2020-5-8 12:54
185行,我写的是myplane_1_rect,没懂你的意思

我觉得spritecollide的对象需要一个rect属性

猪猪虾 发表于 2020-5-8 13:03:29

qiuyouzhi 发表于 2020-5-8 13:00
我觉得spritecollide的对象需要一个rect属性

不需要吧,我写的另一个参考教学视频的版本,和小甲鱼的不全一样,不用属性也可以直接运行,那个是对的,我发给你看一下
import sys
import traceback
from pygame.locals import *
import pygame
import random
from random import *
import math
import sched,time

pygame.init()
pygame.mixer.init()

bg_size = width,height = 480,700
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption("飞机大战")

background = pygame.image.load("image/background.png").convert()

BLACK = (0,0,0)
GREEN = (0,255,0)
RED = (255,0,0)

#载入游戏音乐
pygame.mixer.music.load("sound/game_music.ogg")
pygame.mixer.music.set_volume(0.2)
bullet_sound = pygame.mixer.Sound("sound/bullet.wav")
bullet_sound.set_volume(0.2)
bomb_sound = pygame.mixer.Sound("sound/use_bomb.wav")
bomb_sound.set_volume(0.2)
supply_sound = pygame.mixer.Sound("sound/supply.wav")
supply_sound.set_volume(0.2)
get_bomb_sound = pygame.mixer.Sound("sound/get_bomb.wav")
get_bomb_sound.set_volume(0.2)
get_bullet_sound = pygame.mixer.Sound("sound/get_bullet.wav")
get_bullet_sound.set_volume(0.2)
upgrade_sound = pygame.mixer.Sound("sound/upgrade.wav")
upgrade_sound.set_volume(0.2)
enemy3_fly_sound = pygame.mixer.Sound("sound/enemy3_flying.wav")
enemy3_fly_sound.set_volume(0.2)
enemy1_down_sound = pygame.mixer.Sound("sound/enemy1_down.wav")
enemy1_down_sound.set_volume(0.2)
enemy2_down_sound = pygame.mixer.Sound("sound/enemy2_down.wav")
enemy2_down_sound.set_volume(0.2)
enemy3_down_sound = pygame.mixer.Sound("sound/enemy3_down.wav")
enemy3_down_sound.set_volume(0.2)
me_down_sound = pygame.mixer.Sound("sound/me_down.wav")
me_down_sound.set_volume(0.2)

class Myplane(pygame.sprite.Sprite):
    def __init__(self,bg_size):
      pygame.sprite.Sprite.__init__(self)#继承类

      self.image1 = pygame.image.load("image/me1.png").convert_alpha()
      self.image2 = pygame.image.load("image/me2.png").convert_alpha()
      self.destroy_image1 = pygame.image.load("image/me_destroy_1.png").convert_alpha()
      self.destroy_image2 = pygame.image.load("image/me_destroy_2.png").convert_alpha()
      self.destroy_image3 = pygame.image.load("image/me_destroy_3.png").convert_alpha()
      self.rect = self.image1.get_rect()
      self.width,self.height =bg_size, bg_size
      self.rect.left,self.rect.top = \
                                     (self.width - self.rect.width)//2,\
                                    self.height - self.rect.height -60
      self.speed = 10
      
      
    def moveUp(self):
      if self.rect.top >0:
            self.rect.top -= self.speed
      else:
            self.rect.top = 0

    def moveDown(self):
      if self.rect.bottom < self.height-60:
            self.rect.bottom += self.speed
      else:
            self.rect.bottom = self.height -60

    def moveLeft(self):
      if self.rect.left >0:
            self.rect.left -= self.speed
      else:
            self.rect.left = 0

    def moveRight(self):
      if self.rect.right < self.width:
            self.rect.right += self.speed
      else:
            self.rect.right = self.width




class Enemyplane(pygame.sprite.Sprite):
    def __init__(self,bg_size,position):
      pygame.sprite.Sprite.__init__(self)#继承类

      self.image = pygame.image.load("image/enemy1.png").convert_alpha()
      self.breakdown_image1 = pygame.image.load("image/enemy1_down1.png").convert_alpha()
      self.breakdown_image2 = pygame.image.load("image/enemy2_down1.png").convert_alpha()

      self.rect = self.image.get_rect()
      self.width,self.height =bg_size, bg_size
      self.rect.left,self.rect.top = position
      self.speed = 5
                  
    def enemy_move(self):
      if self.rect.top < self.height:
            self.rect.top += self.speed
      else:
            self.reset()

    def reset(self):
      self.rect.left,self.rect.top = \
                                     randint(0,self.width-self.rect.width),\
                                     randint(-2*self.height,0)


      
def main():
    pygame.mixer.music.play(-1)   #无限循环背景
    clock = pygame.time.Clock()

    #生成我方飞机
    me = Myplane(bg_size)


    group = pygame.sprite.Group()
    #在屏幕最上方随机产生小飞机
    position = randint(0,width-60),0
    you = Enemyplane(bg_size,position)
    group.add(you)

    #延时
    delay = 100
      
    #用于切换图片
    switch_image = True
   
    running = True
    while running:
      for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()

      #获取用户的键盘操作,当键盘操作频繁时,建议使用这种方式获取键盘
      key_pressed = pygame.key.get_pressed()
      
      if key_pressed or key_pressed:#按下w或者方向键
            me.moveUp()
      if key_pressed or key_pressed:#按下w或者方向键
            me.moveDown()
      if key_pressed or key_pressed:#按下w或者方向键
            me.moveLeft()
      if key_pressed or key_pressed:#按下w或者方向键
            me.moveRight()

      screen.blit(background,(0,0))
      #绘制我方飞机
      switch_image = not switch_image#取反,实现不断切换
      if switch_image:
            screen.blit(me.image1,me.rect)
      else:
            screen.blit(me.image2,me.rect)


      #绘制敌方飞机
      for each in group:
            each.enemy_move()
            screen.blit(you.image,you.rect)

      if pygame.sprite.spritecollide(me, group, False):
            screen.blit(you.breakdown_image1,you.rect)
            screen.blit(me.destroy_image1,me.rect)
            screen.blit(me.destroy_image2,me.rect)
            screen.blit(me.destroy_image3,me.rect)
            me_down_sound.play()
            you.speed = 0
            me.speed = 0
      
      
      pygame.display.flip()
      clock.tick(60)

      

if __name__ =="__main__":
    #在双击打开文件时,如果出现异常,将异常显示出来
    try:
      main()
    except SystemExit:
      pass
    except:
      traceback.print_exc()
      pygame.quit()
      input()

猪猪虾 发表于 2020-5-8 13:06:39

txxcat 发表于 2020-5-8 12:58
56行加一句:

{:5_95:}

qiuyouzhi 发表于 2020-5-8 13:07:00

猪猪虾 发表于 2020-5-8 13:03
不需要吧,我写的另一个参考教学视频的版本,和小甲鱼的不全一样,不用属性也可以直接运行,那个是对的 ...

那个里面定义了rect,你的没有
把myplane_1_rect改成rect试试

猪猪虾 发表于 2020-5-8 13:11:33

qiuyouzhi 发表于 2020-5-8 13:07
那个里面定义了rect,你的没有
把myplane_1_rect改成rect试试

好啦,谢啦
页: [1]
查看完整版本: 打飞机 ,182行报错,'Myplane' object has no attribute 'rect'