Python 如何在Pygame中使两个单独的对象匹配旋转

Python 如何在Pygame中使两个单独的对象匹配旋转,python,python-3.x,image,rotation,pygame,Python,Python 3.x,Image,Rotation,Pygame,我正在做一个类似小行星的翻拍,在小行星中最重要的事情之一是子弹旋转以匹配物体的方向。我已经设法让子弹从船上出来了,但它仍然保持垂直旋转。我想让子弹和飞船的旋转相匹配,所以看起来子弹是从飞船里出来的。不幸的是,我的代码很长,但我不知道应该拿出什么使它更紧凑。我只是想添加一些评论,希望我能猜到。非常感谢您的帮助。绝大多数操作都发生在“MOUSEBUTTONDOWN”代码中。我已经为飞船提供了一个“旋转”功能,我想也许我们必须使用它,但我无法实现它 船的图像 我要旋转的项目符号的图像 将pygam

我正在做一个类似小行星的翻拍,在小行星中最重要的事情之一是子弹旋转以匹配物体的方向。我已经设法让子弹从船上出来了,但它仍然保持垂直旋转。我想让子弹和飞船的旋转相匹配,所以看起来子弹是从飞船里出来的。不幸的是,我的代码很长,但我不知道应该拿出什么使它更紧凑。我只是想添加一些评论,希望我能猜到。非常感谢您的帮助。绝大多数操作都发生在“MOUSEBUTTONDOWN”代码中。我已经为飞船提供了一个“旋转”功能,我想也许我们必须使用它,但我无法实现它

船的图像

我要旋转的项目符号的图像

将pygame导入为游戏
将pygame.math导入为math
将随机导入为r
'''
在这里我定义了事物
'''
game.init()
游戏。显示。设置标题(“小行星”)
屏幕=游戏.显示.设置模式([800600])
gameon=True
bgcolor=game.color.color(“#FFFFFF”)
ship=game.image.load(“ship.png”)
船=游戏。变换。比例(船,(50,50))
rect=ship.get_rect()
矩形x=400
矩形y=400
shipdir=0
新闻船
自动=错误
arrow=game.image.load(“bullet.png”)
shiphitbox=ship.get_rect()
shiphitbox.x=100
shiphitbox.y=100
箭头=[]
#旋转船的代码
def rotateship(shipdir):
newship=game.transform.rotate(ship,shipdir)
newrect=rect.copy()
newrect.center=newship.get_rect().center
return newship.subground(newrect.copy())
def getmove():
#返回基于方向的x-y移动
全球shipdir
d=shipdir
如果d>=349或d<11:
返回[0,-2]
如果d>=11且d<34:
返回[-1,-2]
如果d>=34且d<56:
返回[-2,-2]#
如果d>=56且d<79:
返回[-2,-1]
如果d>=79且d<102:
返回[-2,0]
如果d>=102且d<125:
返回[-2,1]
如果d>=125且d<147:
返回[-2,2]#
如果d>=147且d<170:
返回[-1,2]
如果d>=170且d<191:
返回[0,2]
如果d>=191且d<214:
返回[1,2]
如果d>=214且d<237:
返回[2,2]#
如果d>=237且d<260:
返回[2,1]
如果d>=260且d<282:
返回[2,0]
如果d>=282且d<305:
返回[2,-1]
如果d>=305且d<328:
返回[2,-2]#
如果d>=328且d<349:
返回[1,-2]
##游戏的主循环
而gameon:
屏幕填充(bgcolor)
screen.blit(newship,(rect.x,rect.y))
key=game.key.get_pressed()
event=game.event.poll()
#控制
如果键[game.K_a]:
如果shipdir==0:shipdir=360
shipdir-=1
新闻职位=轮换职位(shipdir)
如果键[game.K_d]:
如果shipdir==360:shipdir=0
shipdir+=1
新闻职位=轮换职位(shipdir)
如果auto==True或key[game.K_SPACE]或key[game.K_w]:
move=getmove()
rect.x+=移动[0]
矩形y+=移动[1]
游戏。时间。延迟(5)
#射击
如果event.type==game.MOUSEBUTTONDOWN:
'''
这就是枪击发生的地方
'''
arrowbox=arrow.get_rect()
arrowbox.x=rect.x;
箭头框y=矩形y;
move=getmove()
数据=[箭头框,移动[0],移动[1]]
箭头。追加(数据)
如果event.type==game.QUIT:
gameon=False;
#产卵射弹
对于箭头中的项目符号:
项目符号[0]。x+=项目符号[1]
项目符号[0]。y+=项目符号[2]
如果项目符号[0].x>700或项目符号[0].x
我想让子弹和飞船的旋转相匹配,所以看起来子弹是从飞船里出来的

你必须知道每个单独子弹的旋转角度。将旋转角度添加到项目符号数据:

data=(箭头框,移动[0],移动[1],shipdir)
箭头。追加(数据)
创建旋转的项目符号,并将其:

对于箭头中的项目符号:
rotbill=pygame.transform.rotate(箭头,bullet[3])
screen.blit(rotbill,(bullet[0].x,bullet[0].y))

您已经在跟踪ship dir,只需在子弹上使用“game.transform.rotate”
import pygame as game
import pygame.math as math
import random as r

'''
Here I defined things

'''
game.init()


game.display.set_caption("Asteroids")
screen = game.display.set_mode([800,600])
gameon=True
bgcolor = game.color.Color("#FFFFFF")
ship = game.image.load("ship.png")
ship = game.transform.scale(ship, (50, 50))
rect = ship.get_rect()
rect.x=400
rect.y=400
shipdir = 0
newship = ship

auto = False

arrow = game.image.load("bullet.png")
shiphitbox = ship.get_rect()
shiphitbox.x = 100
shiphitbox.y = 100

arrows=[]




#code to rotate the ship
def rotateship(shipdir):
    newship = game.transform.rotate(ship, shipdir)
    newrect = rect.copy()
    newrect.center  = newship.get_rect().center
    return newship.subsurface(newrect).copy()


def getmove():
    #returns x-y movement based on directrion facing
    global shipdir
    d=shipdir
    if d >= 349 or d < 11:

        return [0, -2]
    elif d >=11 and d < 34:
        return [-1,-2]
    elif d >=34 and d < 56:
        return [-2,-2] #
    elif d >=56 and d < 79:
        return [-2,-1]
    elif d >=79 and d < 102:
        return [-2,0]
    elif d >=102 and d < 125:
        return [-2,1]
    elif d >=125 and d < 147:
        return [-2,2] #
    elif d >=147 and d < 170:
        return [-1,2]
    elif d >=170 and d < 191:
        return [0,2]
    elif d >=191 and d < 214:
        return [1,2]
    elif d >=214 and d < 237:
        return [2,2] #
    elif d >=237 and d < 260:
        return [2,1]
    elif d >=260 and d < 282:
        return [2,0]
    elif d >=282 and d < 305:
        return [2,-1]
    elif d >=305 and d < 328:
        return [2,-2] #
    elif d >=328 and d < 349:
        return [1,-2]



##main loop of the game
while gameon:
    screen.fill(bgcolor)
    screen.blit(newship,(rect.x,rect.y))


    key = game.key.get_pressed()
    event=game.event.poll()
    #controls

    if key[game.K_a]:
        if shipdir==0: shipdir=360
        shipdir -= 1
        newship = rotateship(shipdir)

    if key[game.K_d]:
        if shipdir==360: shipdir=0
        shipdir += 1
        newship = rotateship(shipdir)



    if auto==True or key[game.K_SPACE] or key[game.K_w] :
        move = getmove()
        rect.x +=  move[0]
        rect.y +=  move[1]

    game.time.delay(5)

    #shooting
    if event.type==game.MOUSEBUTTONDOWN:
        '''
        This is where the shooting happens
        '''
        arrowbox = arrow.get_rect()
        arrowbox.x = rect.x;
        arrowbox.y = rect.y;
        move = getmove()
        data = [arrowbox, move[0], move[1]]
        arrows.append(data)


    if event.type==game.QUIT:
        gameon=False;

    #spawning projectiles 
    for bullet in arrows:
        bullet[0].x += bullet[1]
        bullet[0].y += bullet[2]

        if bullet[0].x > 700 or bullet[0].x<0:
            arrows.remove(bullet)



    for bullet in arrows:
        screen.blit(arrow,(bullet[0].x,bullet[0].y))




    game.display.flip() #redraws / refreshes screen



##comes here when game is over
while True:
    screen.fill(bgcolor)
    game.display.flip()
    event=game.event.poll()
    if event.type == game.QUIT:
        break

game.quit()