2014-11-08 76 views
1

我在python中创建了经典的街机游戏“aestroids”,并且我在使用子弹射击的子弹上遇到了一些问题。我一直在试图追踪问题从哪里演变而来。当我在一张纸上单独进行数学计算时,我得到了正确的答案,但是当它运行子弹时,似乎只能以0度角和45度的角度拍摄。我觉得原点可能来自于此功能:Python数学sin/cos编码问题

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) 

下一页末和newY设置为X,当按下空格键和bullangle是拿角度船是当空间被击中以可变我的船Y的线。

这里是全码:

"""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

观察:

$ 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似乎是以度为单位,永远不会转换为弧度。

+0

非常感谢你解决了我的问题 – Ryan 2014-11-08 18:46:16