gpt4 book ai didi

python - 如何在 Pygame 中进行两个单独的对象匹配旋转

转载 作者:行者123 更新时间:2023-12-01 08:25:49 27 4
gpt4 key购买 nike

我正在制作某种小行星重制版,小行星中最重要的一点是子弹会旋转以匹配物体的方向。我设法让子弹从船上射出,但它保持相同的直立旋转。我想让子弹与船的旋转相匹配,所以看起来子弹是从船里出来的。不幸的是,我的代码很长,但我不知道该删除什么以使其更紧凑。我只是想添加评论并希望我能猜到。任何帮助是极大的赞赏。绝大多数操作发生在“MOUSEBUTTONDOWN”代码中。我已经为飞船提供了“旋转”功能,我想也许我们必须使用它,但我无法实现它。

船的图像

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

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()

最佳答案

I would Like to make the bullet match rotation with the ship, so it looks like the bullets are coming out of the ship.

您必须知道每个单独子弹的旋转角度。将旋转角度添加到子弹数据中:

data = (arrowbox, move[0], move[1], shipdir)
arrows.append(data)

通过 pygame.transform.rotate 创建旋转项目符号和 blit它:

for bullet in arrows:
rotBullet = pygame.transform.rotate(arrow, bullet[3])
screen.blit(rotBullet, (bullet[0].x, bullet[0].y))

关于python - 如何在 Pygame 中进行两个单独的对象匹配旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54257041/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com