Я создаю классическую аркадную игру «эстроиды» на питоне, и я упал на некоторые проблемы с пулей, которую вы снимаете на астероидах. Я пытаюсь отслеживать, откуда эта проблема развивается. когда я делаю математику отдельно на листе бумаги, я получаю правильный ответ, который я ищу, но когда он запускает пулю, кажется, что она стреляет под прямым углом под углом 0 градусов и 45. Я чувствую, что происхождение может быть от этого функция:Python math sin/cos encoding issue
def bullet():
global newX,newY,bullangle
pygame.draw.rect(screen,white,(newX,newY,5,5))
print newX, newY
newX = newX - (math.sin(bullangle) * 5)
newY = newY - (math.cos(bullangle) * 5)
newX и Newy устанавливаются в х, у корды моего корабля, когда кнопка нажата пространство и bullangle является переменной держать угол корабль был в том, когда пространство было хитом.
вот полный код:
"""Basic imports"""
import pygame
import math
from pygame.locals import*
pygame.init()
screen = pygame.display.set_mode((640,480))
pygame.display.set_caption("Aestroids")
#colors definitions
black = (0,0,0)
white = (255,255,255)
#variables
degrees = 0
angle = 0
acceleration = 0
x = 320
y = 200
num_of_bullets = 0
#booleans and lists
running = True
shoot = False
clock = pygame.time.Clock()
#PNGS
ship = pygame.image.load("ship.png")
#ship movement
def movement():
global acceleration,angle,degrees
#forward motion
if key[K_LEFT]:
degrees +=5
if key[K_RIGHT]:
degrees -= 5
if key[K_UP]:
if acceleration < 2.5:
acceleration += .2
elif key[K_DOWN]:
if acceleration > -2.3:
acceleration -= .2
#key released motion
if not key[K_UP]:
if acceleration >.5:
acceleration -=.1
if not key[K_DOWN]:
if acceleration <-.5:
acceleration +=.1
if key[K_SPACE]:
global shoot,newX,newY,num_of_bullets,bullangle,degrees
if num_of_bullets == 0:
newX = x
newY = y
bullangle = degrees
num_of_bullets +=1
shoot = True
#rotating the sprite
def rot_center(image, angle):
global rot_image,degrees
if degrees>360:
degrees -=360
elif degrees<-360:
degrees+=360
orig_rect = image.get_rect()
rot_image = pygame.transform.rotate(image, degrees)
rot_rect = orig_rect.copy()
rot_rect.center = rot_image.get_rect().center
rot_image = rot_image.subsurface(rot_rect).copy()
print degrees
#moving the ship forward
def forward():
global x,y
angle = degrees * math.pi/180
x = x- (math.sin(angle) * acceleration)
y = y- (math.cos(angle) * acceleration)
#define bullet
def bullet():
global num_of_bullets,newX,newY,bullangle
pygame.draw.rect(screen,white,(newX,newY,5,5))
print newX, newY
newX = newX - (math.sin(bullangle) * 5)
newY = newY - (math.cos(bullangle) * 5)
num_of_bullets = num_of_bullets - num_of_bullets
#main
while running == True:
clock.tick(30)
key = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill(black)
if shoot == True:
bullet()
movement()
forward()
rot_center(ship, angle)
screen.blit(rot_image,(x,y))
pygame.display.update()
спасибо, кто когда-либо тратит свое время, чтобы помочь мне с этой проблемой. это очень ценится.
Спасибо, что исправил мою проблему – Ryan