我正在对小行星进行某种形式的重制,其中一个大的事实是子弹旋转以匹配物体方向。我设法使子弹从船上出来了,但它仍然保持直立旋转。我想使子弹与飞船匹配旋转,因此子弹看起来像是从船中出来的。不幸的是,我的代码很长,但是我不知道该删除什么以使其更紧凑。我只是想添加评论,希望我猜得到。任何帮助是极大的赞赏。绝大多数动作都发生在“ 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()
答案 0 :(得分:1)
我想让子弹与飞船匹配旋转,所以看起来子弹正从船上出来。
您必须知道每个单独的子弹的旋转角度。将旋转角度添加到项目符号数据中:
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))