Я пытаюсь найти способ получить вращение корабля и применить его к лучу, чтобы он двигался в том направлении, в котором оно было снято. Я действительно не знаю, как бы я сделайте это, но вот мой код: Пожалуйста, извините беспорядок, я помещаю комментарии, чтобы вы знали, что.Пулевые движения Python - Pygame
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()
Пожалуйста, проверьте отступы перед публикацией коды (Python ГКА.) Я отформатированный код, но я хочу, чтобы обеспечить его. – adil
Вы имеете в виду, что «лазерный луч» уволен с космического корабля, и вы хотите стрелять лучом в направлении, в котором ориентирован космический корабль? – adil
@adil Да, отступ теперь прав, сожалею об этом, и да, это то, что я ищу adil :) – user2993584