已编辑,解决方案位于问题底部
在一些好人的帮助下,我在这个问题上有了一些改进:Python - Shoot a bullet in the direction (angle in degrees) my spaceship is facing .
现在的问题是,虽然让飞船朝方向加速的原理是对着的,但是对于弹丸来说似乎不起作用。当子弹从船上以某些角度射出时,似乎会出现奇怪的偏移。
我将把代码放在下面,方法 UPDATE() 和 BOOST() 负责使船移动并且它可以工作。
射弹使用几乎相同的原理,但没有加速。
这里有一个视频,您可以直观地看到游戏运行情况并看看出了什么问题 https://youtu.be/-s7LGuLhePI
这是我的 Ship 和向量类,它们与问题有关。 (我将省略并删除不需要在此处显示的方法)
船舶类包含船舶元素以及射弹类
import pygame
import colors
import math
from vectors import Vector2D
from polygon import Polygon
from helpers import *
class Ship(Polygon) :
def __init__(self, x, y, screen) :
self.screen = screen
self.pos = Vector2D(x, y)
self.size = 18
self.color = colors.green
self.rotation = 0
self.points = [
(-self.size, self.size),
(0, self.size / 3),
(self.size, self.size),
(0, -self.size)
]
self.translate((self.pos.x, self.pos.y))
self.velocity = Vector2D(0, 0)
self.projectiles = []
def shoot(self) :
p = Projectile(self.pos.x, self.pos.y, self.rotation, self.screen)
self.projectiles.append(p)
def turn(self, dir) :
turn_rate = 4
if dir == 'left' :
deg = -turn_rate
elif dir == 'right' :
deg = turn_rate
else :
deg = 0
self.rotate((self.pos.x, self.pos.y), deg)
if self.rotation > 360 :
self.rotation -= 360
elif self.rotation < 0 :
self.rotation += 360
else :
self.rotation += deg
#print('HDG: ' + str(self.rotation))
def boost(self) :
#print(self.velocity.x, ',', self.velocity.y)
force = Vector2D().create_from_angle(self.rotation, 0.1, True)
#Limits the speed
if (((self.velocity.x <= 4) and (self.velocity.x >= -4))
or
((self.velocity.y <= 4) and (self.velocity.y >= -4))) :
self.velocity.add(force)
#print('Velocity: ' + str(self.velocity.x) + ',' + str(self.velocity.y))
def update(self) :
#Adds friction
f = 0.98
self.velocity.mult((f, f))
# Resets ship possition when it's out of the screen
if self.pos.x > (self.screen.get_width() + self.size) :
#print('COLLIDED RIGHT')
self.pos.x -= self.screen.get_width() + self.size
self.translate((-(self.screen.get_width() + self.size), 0))
elif self.pos.x < -self.size :
#print('COLLIDED LEFT')
self.pos.x += self.screen.get_width() + self.size
self.translate(((self.screen.get_width() + self.size), 0))
if self.pos.y > (self.screen.get_height() + self.size) :
#print('COLLIDED BOTTOM')
self.pos.y -= self.screen.get_height() + self.size
self.translate((0, -(self.screen.get_height() + self.size)))
elif self.pos.y < -self.size :
#print('COLLIDED TOP')
self.pos.y += self.screen.get_height() + self.size
self.translate((0, (self.screen.get_height() + self.size)))
self.pos.x += self.velocity.x #TODO: simplify using V2D add function
self.pos.y += self.velocity.y
self.translate(self.velocity.tuple())
#Update projectiles that have been shot
for p in self.projectiles :
p.update()
def draw(self) :
stroke = 3
pygame.draw.polygon(self.screen, self.color, self.points, stroke)
#Draws projectiles that have been shot
for p in self.projectiles :
p.draw()
class Projectile(object) :
def __init__(self, x, y, ship_angle, screen) :
self.screen = screen
self.speed = 3 #Slow at the moment while we test it
self.direction = ship_angle;
self.pos = Vector2D(x, y)
self.color = colors.green
def update(self) :
self.pos.add(Vector2D().create_from_angle(self.direction, self.speed, return_instance = True))
def draw(self) :
pygame.draw.circle(self.screen, self.color, self.pos.int().tuple(), 2, 0)
CLASS VECTOR(用于根据元素“方向”计算矢量并应用速度)
import math
class Vector2D() :
def __init__(self, x = None, y = None) :
self.x = x
self.y = y
def create_from_angle(self, angle, magnitude, return_instance = False) :
angle = math.radians(angle) - math.pi / 2
x = math.cos(angle) * magnitude
y = math.sin(angle) * magnitude
self.x = x
self.y = y
if return_instance :
return self
def tuple(self) :
return (self.x, self.y)
def int(self) :
self.x = int(self.x)
self.y = int(self.y)
return self
def add(self, vector) :
if isinstance(vector, self.__class__) : # vector is an instance of this class
self.x += vector.x
self.y += vector.y
else : # vector is a tuple
self.x += vector[0]
self.y += vector[1]
def mult(self, vector) :
if isinstance(vector, self.__class__) : # vector is an instance of this class
self.x *= vector.x
self.y *= vector.y
else : # vector is a tuple
self.x *= vector[0]
self.y *= vector[1]
基于标记答案的解决方案
Pygame.draw.circle() 只接受 INTEGER 值元组作为位置参数,由于我们正在进行的计算,这是不可能的,因为角度计算的结果始终是 float 。
解决方案是在 Projectile 类中更改绘制方法以使用 Ellipse 而不是 Circle:
class Projectile(object) :
def __init__(self, x, y, ship_angle, screen) :
self.screen = screen
self.speed = 3 #Slow at the moment while we test it
self.direction = ship_angle;
self.velocity = Vector2D().create_from_angle(self.direction, self.speed, return_instance = True)
self.pos = Vector2D(x, y)
self.color = colors.green
# Properties neccesary to draw the ellipse in the projectile position
self.size = 4
self.box = (0,0,0,0)
def update(self) :
self.pos.add(self.velocity)
self.box = (self.pos.x, self.pos.y, self.size, self.size)
def draw(self) :
stroke = 2
pygame.draw.ellipse(self.screen, self.color, self.box, stroke)
最佳答案
事实证明,错误出人意料地出现在您的 Vector2D
类中,而不是出现在您期望的函数中!您的 Vector2D().create_from_angle()
函数很好,假设向上为 0 并且角度值顺时针增加,则可以创建正确的角度,即使它违反了 0 向右的“标准数学约定”逆时针方向是增加角度值。
真正的问题在于您的 Vector2D().int()
函数,该函数被用作 draw()
的一部分Projectile
类中的函数。将 float
数字转换为 int
只是截断小数值,因此您可以将 int
转换与 python 的 round
一起使用> 功能如下:
def int(self) :
self.x = int(round(self.x))
self.y = int(round(self.y))
return self
但是,即使您执行此修复,您也会注意到由于圆角与船尖相比,角度仍然略有偏差。这是因为 Projectile.draw()
函数下的 self.pos.int()
使用不准确的值覆盖当前位置,不断添加到 Projectile.update()
函数中的四舍五入版本。最好的解决方案可能是避免使用 Vector2D.int()
函数,并将 Projectile.draw()
函数替换为:
def draw(self) :
pos_x = int(round(self.pos.x))
pos_y = int(round(self.pos.y))
pygame.draw.circle(self.screen, self.color, (pos_x, pos_y), 2, 0)
上述修改将使您的射弹从船尖完美射出,但会导致抖动,因为位置在 x 或 y 坐标中有时向上舍入,有时向下舍入。使用修改后的 Vector2D.int()
函数可以实现不抖动但不准确的射弹射击。哪种错误最好由您决定。如果清楚的话请告诉我!
关于Python - 让我的宇宙飞船朝其面向的方向射击第 2 部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41214465/