前提・実現したいこと
python pygameでゲームを作成しています。グループクラス.draw(screen)で画像を表示させたいのですが実行するとエラーを図れてしまいます。エラーにはmachineにimageがないといわれているのですが、まずimageという変数を用いていないのでよくわかりません。ご協力お願い致します。
発生している問題・エラーメッセージ
AttributeError: 'machine' object has no attribute 'image'
該当のソースコード
python3
from pygame.locals import * import pygame FPS = 60 # Frame per Second 毎秒のフレーム数 LOOP=True jump=False Right=False Left=False machine_img="pilot.png" machine_img2="pilot2.png" machine_img3="pilot3.png" machine_img4="pilot4.png" target_img="monster1.png" mouse_point="mouse_point.png" class machine(pygame.sprite.Sprite): def __init__(self, x, y, vx, vy, w, h): super().__init__() self.x, self.y=(x, y) self.vx, self.vy=(vx, vy) self.w, self.h=(w, h) self.grav = 16 self.rect = pygame.Rect(self.x, self.y, self.w, self.h) self.image1 = pygame.image.load(machine_img).convert() self.image1=pygame.transform.scale(self.image1, (70, 70)) self.jump=False def LEFT(self): if 0 <= self.rect.left-10 <= 700: self.rect.move_ip(-self.vx, 0) self.image1 = pygame.image.load(machine_img3).convert() self.image1=pygame.transform.scale(self.image1, (70, 70)) def RIGHT(self): if 0 <= self.rect.right+10 <= 700: self.rect.move_ip(self.vx, 0) self.image1 = pygame.image.load(machine_img).convert() self.image1=pygame.transform.scale(self.image1, (70, 70)) def DOWN(self): if self.rect.y<=self.y+self.h/2: self.rect.move_ip(0,self.vy) class target1(pygame.sprite.Sprite): def __init__(self, x, y, vx, w, h): self.x, self.y = (x, y) self.vx = vx self.w, self.h = (w, h) self.rect=pygame.Rect(self.x, self.y, self.w, self.h) self.image2 = pygame.image.load(target_img).convert() self.image2=pygame.transform.scale(self.image2, (80, 80)) self.time=0 def draw_target1(self): self.rect=pygame.draw.rect(screen, (255, 255, 255), (self.x, self.y, 20, 30)) def move_target1(self): self.time+=1 if self.time<=30: self.rect.move_ip(self.vx, 0) elif 30<self.time<=60: self.rect.move_ip(-self.vx, 0) else: self.time=0 screen = pygame.display.set_mode((800, 450)) clock = pygame.time.Clock() own_machine=pygame.sprite.Group() own_machine.add(machine(350,300,10,10,30,40)) tar1=target1(200, 100, 1, 40, 30) mouse_x, mouse_y=(20,20) mouse_point = pygame.image.load(mouse_point).convert() mouse_point=pygame.transform.scale(mouse_point, (70, 70)) while LOOP: for event in pygame.event.get(): if event.type == pygame.QUIT: LOOP = False clock.tick(FPS) pressed_keys = pygame.key.get_pressed() for event in pygame.event.get(): if event.type == MOUSEMOTION: mouse_x, mouse_y=event.pos mouse_x -= int(mouse_point.get_width() / 2) mouse_y -= int(mouse_point.get_height() / 2) if pressed_keys[pygame.K_UP]: jump=True if pressed_keys[pygame.K_LEFT]: own_machine.LEFT() Right=False Left=True if pressed_keys[pygame.K_RIGHT]: own_machine.RIGHT() Right=True Left=False if event.type == KEYDOWN: if pressed_keys[pygame.K_DOWN]: own_machine.DOWN() own_machine.image1 = pygame.image.load(machine_img2).convert() own_machine.image1=pygame.transform.scale(own_machine.image1, (70, 70)) if Left: own_machine.image1 = pygame.image.load(machine_img4).convert() own_machine.image1=pygame.transform.scale(own_machine.image1, (70, 70)) if event.type == KEYUP: if own_machine.rect.y>=own_machine.y+10: own_machine.rect.move_ip(0,-own_machine.vy) own_machine.image1 = pygame.image.load(machine_img).convert() own_machine.image1=pygame.transform.scale(own_machine.image1, (70, 70)) if Left: own_machine.image1 = pygame.image.load(machine_img3).convert() own_machine.image1=pygame.transform.scale(own_machine.image1, (70, 70)) if jump: own_machine.rect.move_ip(0, -own_machine.grav) own_machine.grav-=1 if own_machine.grav<-16: own_machine.grav=16 jump=False screen.blit(mouse_point, (mouse_x, mouse_y)) screen.blit(tar1.image2, tar1.rect) tar1.move_target1() own_machine.draw(screen) pygame.display.flip() screen.fill((0, 0, 0)) pygame.quit()
補足情報(FW/ツールのバージョンなど)
グループクラス.draw(screen)は下から4行目で使っています。これを用いたい理由は、ゲームに表示されるオブジェクトは全てrectに画像を張り付けているのですが、画像だけでなくそのrect部分も表示されてしまうのでそれを消したいためです。
まだ回答がついていません
会員登録して回答してみよう