前提・実現したいこと
pythonで簡単なゲームを作っています。
pygame zeroを使用しコードを書いています。
playerから物(ほね)を投げるコードを書きたいと思っています。(シューティングのようなもの)
発生している問題・エラーメッセージ
骨をplayerから投げることを実現したいのですが座標が更新されず描画されません。
一度playerの座標に表示され、消えてしまいます。
該当のソースコード
以下が該当コード部分です。
python
class Obj(Actor): def __init__(self, x, y, angle, num): Actor.__init__(self, dogs[num].imagename, (x, y)) self.hp = dogs[num].hp self.num = num self.count = 0 self.angle = angle class Bone(Obj): def update(self): self.x, self.y = shootBone(self.pos, self.angle, 8) hitbox = Rect((self.x-15, self.y-15), (30, 30)) for sp in objects: if sp.hp == 99: continue if sp.colliderect(hitbox): self.x = OUTSIDE sp.hp -= 1 break def shootBone(pos, angle, speed): # 角度と速さから次の座標を返す x, y = pos rad = math.radians(angle) x += speed * (math.cos(rad)) y += speed * (math.sin(rad)) return int(x), int(y) def draw(): # 描画部分 screen.clear() if titlemode == True: screen.draw.text("game start", left=150, top=240, fontsize=64, color="YELLOW") else: for sp in objects: sp.draw() if gameover: screen.draw.text("game over", left=150, top=240, fontsize=64, color="YELLOW")
コード全体
python
import pgzrun import random import time import math WIDTH = 500 HEIGHT = 500 score = 0 OUTSIDE = 1000 gameover = False titlemode = True start = 0 class Charclass: def __init__(self, filename, hp): self.imagename = filename self.hp = hp char = [] # キャラの追加 char.append(Charclass("human.png", 999)) char.append(Charclass("dog.png", 1)) char.append(Charclass("bone.png", 1)) class Obj(Actor): # objectの基本クラス def __init__(self, x, y, angle, num): Actor.__init__(self, char[num].imagename, (x, y)) self.hp = char[num].hp self.num = num self.count = 0 self.angle = angle class Dog(Obj): #犬クラス def update(self): # 円形に動く rad = math.radians(self.count - 100) self.y = (HEIGHT / 2) + (math.sin(rad) * 200) self.x = (HEIGHT / 2) + (math.cos(rad) * 200) class Bone(Obj): # 骨クラス def update(self): self.x, self.y = shootBone(self.pos, self.angle, 8) hitbox = Rect((self.x-15, self.y-15), (30, 30)) for sp in objects: if sp.hp == 999: # playerなら無視 continue if sp.colliderect(hitbox):# player以外で当たっていれば self.x = OUTSIDE sp.hp -= 1 break class Player(Obj): def update(self): # キーボード操作 if keyboard.up: self.y -= 4 if keyboard.down: self.y += 4 if keyboard.left: self.x -= 4 if keyboard.right: self.x += 4 if self.x < 35: self.x = 35 if self.y < 35: self.y = 35 if self.x > (WIDTH-35): self.x = WIDTH-35 if self.y > (HEIGHT-35): self.y = HEIGHT-35 if keyboard.space and (self.count % 16) == 0: # 骨を投げる objects.append(Bone(self.x, self.y, 180, 2)) objects.append(Bone(self.x, self.y, 0, 2)) objects.append(Bone(self.x, self.y, -90, 2)) def shootBone(pos, angle, speed): x, y = pos rad = math.radians(angle) x += speed * (math.cos(rad)) y += speed * (math.sin(rad)) return int(x), int(y) objects = [Player(400, 400, 0, 0), Dog(20, 20, 0, 1)] def draw(): screen.clear() # screen.draw.text("Time:" + str(start), (10, 10), color="white") if titlemode == True: screen.draw.text("dog game", left=150, top=240, fontsize=64, color="YELLOW") else: for sp in objects: sp.draw() if gameover: screen.draw.text("game over", left=150, top=240, fontsize=64, color="YELLOW") def update(): global titlemode, start, objects, gameover start += 1 if titlemode: if keyboard.space: titlemode = False if gameover: return for sp in objects: if start == 1000: gameover = True sp.update() sp.count += 1 if sp.hp <= 0: sp.x = OUTSIDE if sp.x < -35 or sp.x > (HEIGHT+35) or sp.y < -35 or sp.y > (WIDTH+35): objects.remove(sp) pgzrun.go()
試したこと
骨を投げた後の座標をprintしてみたのですが1回しか表示されませんでした。骨はobjectを入れるリストには入っていました。
pygame zero については触って2日ほどでわからないことが多いので助けていただくと幸いです。
まだ回答がついていません
会員登録して回答してみよう