Python数学sin / cos编码问题

时间:2014-11-08 18:23:44

标签: python pygame sin cos

我创造了经典的街机游戏" aestroids"在python中我和你在小行星上射击的子弹有些问题。我一直试图追踪问题的演变。当我在一张纸上分别进行数学计算时,我得到了正在寻找的正确答案,但是当它运行时子弹似乎只是以0度角和45度成直角拍摄。我觉得起源可能来自于此功能:

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被设置为我船的x,y绳索,而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()
谢谢你们花时间帮我解决这个问题。非常感谢。

1 个答案:

答案 0 :(得分:1)

观察:

$ grep bullangle code.py 
       global shoot,newX,newY,num_of_bullets,bullangle,degrees
           bullangle = degrees
   global num_of_bullets,newX,newY,bullangle 
   newX = newX - (math.sin( bullangle ) * 5)
   newY = newY - (math.cos( bullangle ) * 5) 

如您所知,math.sinmath.cos期望他们的参数是弧度,而不是度数,并且,如此行所示:

           bullangle = degrees

bullangle似乎是度数,永远不会转换为弧度。