我正试图找到一种方法来获得船的旋转并将其应用到梁上,以便它将朝着射入的方向行进。我真的不知道我会怎么做但是这里是我的代码:请原谅它的混乱,我提出意见,所以你知道什么。
import sys, pygame, math, time;
from pygame.locals import *;
spaceship = ('spaceship.png')
mouse_c = ('crosshair.png')
backg = ('background.jpg')
fire_beam = ('beams.png')
pygame.init()
screen = pygame.display.set_mode((800, 600))
bk = pygame.image.load(backg).convert_alpha()
mousec = pygame.image.load(mouse_c).convert_alpha()
space_ship = pygame.image.load(spaceship).convert_alpha()
f_beam = pygame.image.load(fire_beam).convert_alpha()
f_beam = pygame.transform.scale(f_beam, (50, 50))
f_beam_rect = f_beam.get_rect()
clock = pygame.time.Clock()
pygame.mouse.set_visible(False)
space_ship_rect = space_ship.get_rect()
space_ship_rect.centerx = 375
space_ship_rect.centery = 300
speed = 3.5
pressed_down = 0
while True:
clock.tick(60)
screen.blit(bk, (0, 0))
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == MOUSEBUTTONDOWN and event.button == 3:
pressed_down = 1
elif event.type == MOUSEBUTTONUP:
pressed_down = 0
if pressed_down == 1:
x, y = pygame.mouse.get_pos()
x1, y1 = x - space_ship_rect.x, y - space_ship_rect.y
angle = math.atan2(y1, x1)
dx = speed*math.cos(angle)
dy = speed*math.sin(angle)
movex = space_ship_rect.centerx = space_ship_rect.centerx + dx#ship x
movey = space_ship_rect.centery = space_ship_rect.centery + dy#ship y
if event.type == MOUSEMOTION:
x1, y1 = pygame.mouse.get_pos()
x2, y2 = space_ship_rect.x, space_ship_rect.y
dx, dy = x2 - x1, y2 - y1
rads = math.atan2(dx, dy)
degs = math.degrees(rads)
display_s = pygame.transform.rotate(space_ship, (degs))#rotation of ship
if event.type == MOUSEBUTTONDOWN and event.button == 1:
#Is it possible for me to get the degree rotation of the space_ship and apply it to here so the beam will travel in the direction it was shot in?
screen.blit(display_s, (space_ship_rect.centerx, space_ship_rect.centery))
pos = pygame.mouse.get_pos()
screen.blit(mousec, (pos))
pygame.display.update()
答案 0 :(得分:2)
我发现你的船只旋转有问题。
如果您创建旋转太空飞船display_s
,则会获得尺寸不同于space_ship
的图像,因此您必须获得display_s
矩形,并将center
太空飞船分配给display_s
中心。
display_s_rect = display_s.get_rect( center=spaceship_rect.center)
现在您必须使用display_s_rect
来显示display_s
screen.blit(display_s, display_s_rect)
顺便说一下:
blit()
期望将blit图像的左上角放在屏幕上的位置。
使用
screen.blit(display_s, (space_ship_rect.centerx, space_ship_rect.centery))
display_s
的左上角将放入(space_ship_rect.centerx, space_ship_rect.centery)
,但我认为您希望将display_s
置于(space_ship_rect.centerx, space_ship_rect.centery)
将中心值(如前所述)分配给(space_ship_rect.centerx, space_ship_rect.centery)
,但在(space_ship_rect.x, space_ship_rect.y)
中使用blit()
。
您可以使用space_ship_rect
代替(space_ship_rect.x, space_ship_rect.y)
blit()
,但结果相同。
我认为mousec
中的blit()
位置存在同样的问题。
获取mousec
矩形,将鼠标位置指定为矩形中心,然后使用矩形x,y进行blit。
mousec_rect = mousec.get_rect( center = pygame.mouse.get_pos() )
screen.blit(mousec, mousec_rect)
编辑:
我的修改后你的主循环 - 现在船是按原样旋转
display_s = space_ship # default value at start when ship wasn't rotate
while True:
clock.tick(60)
screen.blit(bk, (0, 0))
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == MOUSEBUTTONDOWN and event.button == 3:
pressed_down = 1
elif event.type == MOUSEBUTTONUP:
pressed_down = 0
if pressed_down == 1:
x, y = pygame.mouse.get_pos()
x1, y1 = x - space_ship_rect.x, y - space_ship_rect.y
angle = math.atan2(y1, x1)
dx = speed*math.cos(angle)
dy = speed*math.sin(angle)
movex = space_ship_rect.centerx = space_ship_rect.centerx + dx#ship x
movey = space_ship_rect.centery = space_ship_rect.centery + dy#ship y
if event.type == MOUSEMOTION:
x1, y1 = pygame.mouse.get_pos()
x2, y2 = space_ship_rect.centerx, space_ship_rect.centery
dx, dy = x2 - x1, y2 - y1
rads = math.atan2(dx, dy)
degs = math.degrees(rads)
display_s = pygame.transform.rotate(space_ship, (degs))#rotation of ship
display_s_rect = display_s.get_rect(center = space_ship_rect.center)
if event.type == MOUSEBUTTONDOWN and event.button == 1:
#Is it possible for me to get the degree rotation of the space_ship and apply it to here so the beam will travel in the direction it was shot in?
pass
screen.blit(display_s, display_s_rect)
#screen.blit(display_s, space_ship_rect)
pos = pygame.mouse.get_pos()
mousec_rect = mousec.get_rect(centerx=pos[0], centery=pos[1])
screen.blit(mousec, mousec_rect )
pygame.display.update()
答案 1 :(得分:0)
在While循环之前:
beam_speed=<somevalue>
f_beam_rect=space_ship_rect #initialise beam position
fired=False #check if beam is fired or not
Inside While循环
##If beam is fired(right click) blit images through-out the path
if fired:
b_angle = math.atan2(by1, bx1)
bdx = beam_speed*math.cos(b_angle)
bdy = beam_speed*math.sin(b_angle)
f_beam_rect.centerx = f_beam_rect.centerx + bdx
f_beam_rect.centery = f_beam_rect.centery + bdy
screen.blit(f_beam,f_beam_rect)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
###If mouse is clicked then find the direction (mouse position and spaceship)
elif event.type == MOUSEBUTTONDOWN and event.button == 1:
bx, by = pygame.mouse.get_pos()
bx1, by1 = bx - space_ship_rect.x, by - space_ship_rect.y
fired=True
现在你可以制作一个检查光束与敌人物体碰撞的功能,如果是真的话,则停止对光束和那个物体进行制作。
同时重置变量fired=False
并重置光束位置以再次从太空飞船开始f_beam_rect=space_ship_rect