pygameで敵キャラがぐるぐる回りながら攻めてくる、みたいな動きをやるにはどうしたらいいか、考えてみました
螺旋階段を降りてくるみたいな動きをすればいいわけですね
円の中心を少しずつずらしながら円を描いていきます。
import pygame
from pygame.locals import *
import sys
import math
def main():
pygame.init() # Pygameの初期化
screen = pygame.display.set_mode((800, 600)) # 800*600の画面
cx = 400
cy = 300
cr= 100
cdig = 0
crad = cdig*3.14/180
px= cx+ cr*(math.cos(crad))
py= cy+ cr*(math.sin(crad))
while True:
screen.fill((255,255,255)) # 背景を白
cdig += 1
cx += 0.1
cy += 0.1
crad = cdig*3.14/180
px= cx+ cr*(math.cos(crad))
py= cy+ cr*(math.sin(crad))
pygame.draw.circle(screen,(10,210,10),(int(px),int(py)),10) # ●
pygame.display.update() # 画面更新
# イベント処理
for event in pygame.event.get(): # イベントを取得
if event.type == QUIT: # 閉じるボタンが押されたら終了
pygame.quit() # Pygameの終了(ないと終われない)
sys.exit() # 終了(ないとエラーで終了することになる)
if __name__ == "__main__":
main()
ゲーム化してみる
上のおかしな動きをゲーム化してみました
マウスの位置に玉が飛んでいって、当たれば緑の敵キャラが消えます
import pygame
from pygame.locals import *
import sys
import math
class Ball():
def __init__(self,vx,vy):
self.cx = 400 #中心
self.cy = 300
self.cr = 100 #回る半径
self.cdig = 0 #角度
self.crad = self.cdig*3.14/180 #rad
self.x = self.cx+ self.cr*(math.cos(self.crad))#位置
self.y = self.cy+ self.cr*(math.sin(self.crad))
self.vx = vx
self.vy = vy
self.hp = 100
def update(self,T):
self.cdig += 1#角度を1度追加
self.cx += self.vx
self.cy += self.vy
#枠から外れたら戻る
if self.cy > 600 or self.cy < 0:
self.vy*=-1
if self.cx > 800 or self.cx < 0:
self.vx*=-1
#回転半径が10以上なら半径を小さくする(らせんの動き)
if self.cr >= 10:
self.cr -= 0.05
#当たり判定 タマ
if T.x-10<=self.x<=T.x+10 and T.y-10<=self.y<=T.y+10:
self.hp=0
self.crad = self.cdig*3.14/180
self.x= self.cx+ self.cr*(math.cos(self.crad))
self.y= self.cy+ self.cr*(math.sin(self.crad))
def draw(self, screen):
if self.hp>0:
pygame.draw.circle(screen,(10,210,10),(int(self.x),int(self.y)),10) # ●
# タマ描画
class Tama():
def __init__(self):
#タマ関連
self.isShoot = False
self.x = 0
self.y = 0
self.vx = 0
self.vy = 0
def update(self):
self.x+=self.vx/10
self.y+=self.vy/10
def draw(self, screen):
if self.isShoot:
pygame.draw.circle(screen,(10,10,10),(int(self.x),int(self.y)),10) # ●
if self.x > 800 or self.x <0 or self.y>600 or self.y < 0:
self.isShoot=False
class Player():
def __init__(self):
self.x = 400
self.y = 500
#マウス関連
self.mx = 0
self.my = 0
def update(self, T):
# イベント処理
for event in pygame.event.get(): # イベントを取得
if event.type == QUIT: # 閉じるボタンが押されたら終了
pygame.quit() # Pygameの終了(ないと終われない)
sys.exit() # 終了(ないとエラーで終了することになる)
elif event.type == KEYDOWN:
if event.key == K_SPACE:
if T.isShoot == False:
T.isShoot = True
T.x = self.x
T.y = self.y
dx = self.mx - self.x
dy = self.my - self.y
dg = math.atan2(dy, dx)
T.vx = math.cos(dg)*10
T.vy = math.sin(dg)*10
elif event.type == MOUSEMOTION:
self.mx, self.my = event.pos
def draw(self, screen):
#プレイヤ描画
pygame.draw.rect(screen, (255,0,0), Rect(self.x,self.y,50,50), 1) # ■
def main():
pygame.init() # Pygameの初期化
screen = pygame.display.set_mode((800, 600)) # 800*600の画面
#Bs = [[Ball(i/20,j/20) for i in range(-4,5)]for j in range(-4,5)]
Bs = [[Ball(i/10,j/10) for i in range(-1,2)]for j in range(-1,1)]
P1 = Player()
T1 = Tama()
while True:
screen.fill((255,255,255)) # 背景を白
for B1s in Bs:
for B in B1s:
B.update(T1)
B.draw(screen)
P1.update(T1)
P1.draw(screen)
T1.update()
T1.draw(screen)
pygame.display.update() # 画面更新
if __name__ == "__main__":
main()