質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Pygame

Pygameは、ビデオゲームの製作用に設計されたクロスプラットフォームのPythonモジュールセットです。Pythonでコンピューターグラフィックスと音声を扱うためのライブラリが含まれています。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

1851閲覧

python pygameのエラー解決

ain_kun

総合スコア1

Pygame

Pygameは、ビデオゲームの製作用に設計されたクロスプラットフォームのPythonモジュールセットです。Pythonでコンピューターグラフィックスと音声を扱うためのライブラリが含まれています。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2021/12/28 13:16

前提・実現したいこと

python pygameでゲームを作成しています。グループクラス.draw(screen)で画像を表示させたいのですが実行するとエラーを図れてしまいます。エラーにはmachineにimageがないといわれているのですが、まずimageという変数を用いていないのでよくわかりません。ご協力お願い致します。

発生している問題・エラーメッセージ

AttributeError: 'machine' object has no attribute 'image'

該当のソースコード

python3

1from pygame.locals import * 2import pygame 3 4 5FPS = 60 # Frame per Second 毎秒のフレーム数 6LOOP=True 7jump=False 8Right=False 9Left=False 10 11machine_img="pilot.png" 12machine_img2="pilot2.png" 13machine_img3="pilot3.png" 14machine_img4="pilot4.png" 15target_img="monster1.png" 16mouse_point="mouse_point.png" 17 18 19 20class machine(pygame.sprite.Sprite): 21 def __init__(self, x, y, vx, vy, w, h): 22 super().__init__() 23 self.x, self.y=(x, y) 24 self.vx, self.vy=(vx, vy) 25 self.w, self.h=(w, h) 26 self.grav = 16 27 self.rect = pygame.Rect(self.x, self.y, self.w, self.h) 28 self.image1 = pygame.image.load(machine_img).convert() 29 self.image1=pygame.transform.scale(self.image1, (70, 70)) 30 self.jump=False 31 32 def LEFT(self): 33 if 0 <= self.rect.left-10 <= 700: 34 self.rect.move_ip(-self.vx, 0) 35 self.image1 = pygame.image.load(machine_img3).convert() 36 self.image1=pygame.transform.scale(self.image1, (70, 70)) 37 38 def RIGHT(self): 39 if 0 <= self.rect.right+10 <= 700: 40 self.rect.move_ip(self.vx, 0) 41 self.image1 = pygame.image.load(machine_img).convert() 42 self.image1=pygame.transform.scale(self.image1, (70, 70)) 43 44 def DOWN(self): 45 if self.rect.y<=self.y+self.h/2: 46 self.rect.move_ip(0,self.vy) 47 48class target1(pygame.sprite.Sprite): 49 def __init__(self, x, y, vx, w, h): 50 self.x, self.y = (x, y) 51 self.vx = vx 52 self.w, self.h = (w, h) 53 self.rect=pygame.Rect(self.x, self.y, self.w, self.h) 54 self.image2 = pygame.image.load(target_img).convert() 55 self.image2=pygame.transform.scale(self.image2, (80, 80)) 56 self.time=0 57 58 def draw_target1(self): 59 self.rect=pygame.draw.rect(screen, (255, 255, 255), (self.x, self.y, 20, 30)) 60 61 def move_target1(self): 62 self.time+=1 63 if self.time<=30: 64 self.rect.move_ip(self.vx, 0) 65 elif 30<self.time<=60: 66 self.rect.move_ip(-self.vx, 0) 67 else: 68 self.time=0 69 70screen = pygame.display.set_mode((800, 450)) 71clock = pygame.time.Clock() 72 73own_machine=pygame.sprite.Group() 74own_machine.add(machine(350,300,10,10,30,40)) 75tar1=target1(200, 100, 1, 40, 30) 76 77mouse_x, mouse_y=(20,20) 78mouse_point = pygame.image.load(mouse_point).convert() 79mouse_point=pygame.transform.scale(mouse_point, (70, 70)) 80 81while LOOP: 82 for event in pygame.event.get(): 83 if event.type == pygame.QUIT: LOOP = False 84 clock.tick(FPS) 85 pressed_keys = pygame.key.get_pressed() 86 for event in pygame.event.get(): 87 if event.type == MOUSEMOTION: 88 mouse_x, mouse_y=event.pos 89 mouse_x -= int(mouse_point.get_width() / 2) 90 mouse_y -= int(mouse_point.get_height() / 2) 91 if pressed_keys[pygame.K_UP]: 92 jump=True 93 if pressed_keys[pygame.K_LEFT]: 94 own_machine.LEFT() 95 Right=False 96 Left=True 97 if pressed_keys[pygame.K_RIGHT]: 98 own_machine.RIGHT() 99 Right=True 100 Left=False 101 if event.type == KEYDOWN: 102 if pressed_keys[pygame.K_DOWN]: 103 own_machine.DOWN() 104 own_machine.image1 = pygame.image.load(machine_img2).convert() 105 own_machine.image1=pygame.transform.scale(own_machine.image1, (70, 70)) 106 if Left: 107 own_machine.image1 = pygame.image.load(machine_img4).convert() 108 own_machine.image1=pygame.transform.scale(own_machine.image1, (70, 70)) 109 110 if event.type == KEYUP: 111 if own_machine.rect.y>=own_machine.y+10: 112 own_machine.rect.move_ip(0,-own_machine.vy) 113 own_machine.image1 = pygame.image.load(machine_img).convert() 114 own_machine.image1=pygame.transform.scale(own_machine.image1, (70, 70)) 115 if Left: 116 own_machine.image1 = pygame.image.load(machine_img3).convert() 117 own_machine.image1=pygame.transform.scale(own_machine.image1, (70, 70)) 118 119 if jump: 120 own_machine.rect.move_ip(0, -own_machine.grav) 121 own_machine.grav-=1 122 if own_machine.grav<-16: 123 own_machine.grav=16 124 jump=False 125 126 screen.blit(mouse_point, (mouse_x, mouse_y)) 127 screen.blit(tar1.image2, tar1.rect) 128 tar1.move_target1() 129 own_machine.draw(screen) 130 pygame.display.flip() 131 screen.fill((0, 0, 0)) 132pygame.quit()

補足情報(FW/ツールのバージョンなど)

グループクラス.draw(screen)は下から4行目で使っています。これを用いたい理由は、ゲームに表示されるオブジェクトは全てrectに画像を張り付けているのですが、画像だけでなくそのrect部分も表示されてしまうのでそれを消したいためです。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

pygame.sprite - Pygameドキュメント 日本語訳

この基底Spriteクラスは、内部に格納した情報をSurfaceへ描写する機能を持っています。Group.draw 命令を実行するには、格納している各Spriteオブジェクトに Surface.image 属性と Surface.rect 属性が設定されている必要があります。

なので、machine クラスの self.image1self.image にリネームします。

python

1class machine(pygame.sprite.Sprite): 2 def __init__(self, x, y, vx, vy, w, h): 3 super().__init__() 4 self.x, self.y=(x, y) 5 self.vx, self.vy=(vx, vy) 6 self.w, self.h=(w, h) 7 self.grav = 16 8 self.rect = pygame.Rect(self.x, self.y, self.w, self.h) 9 self.image = pygame.image.load(machine_img).convert() 10 self.image=pygame.transform.scale(self.image, (70, 70)) 11 self.jump=False 12 13 def LEFT(self): 14 if 0 <= self.rect.left-10 <= 700: 15 self.rect.move_ip(-self.vx, 0) 16 self.image = pygame.image.load(machine_img3).convert() 17 self.image=pygame.transform.scale(self.image, (70, 70)) 18 19 def RIGHT(self): 20 if 0 <= self.rect.right+10 <= 700: 21 self.rect.move_ip(self.vx, 0) 22 self.image = pygame.image.load(machine_img).convert() 23 self.image=pygame.transform.scale(self.image, (70, 70)) 24 25 def DOWN(self): 26 if self.rect.y<=self.y+self.h/2: 27 self.rect.move_ip(0,self.vy)

これで描画される様になりますが、キー入力を行うと別のエラーが発生します。

投稿2021/12/28 19:47

melian

総合スコア19761

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問