计算玩家射击的方向 pygame
- 2025-02-20 09:23:00
- admin 原创
- 24
问题描述:
这是我的玩家班级
class Player:
def __init__(self, image):
self.rotation_angle = 0
def rotate(self, keys, left, right):
if keys[right]:
self.rotation_angle -= 0.5
if keys[left]:
self.rotation_angle += 0.5
self.rotated_player = pygame.transform.rotate(self.player, (self.rotation_angle))
现在self.rotation_angle
,我需要根据 来发射子弹。因此我做了以下操作。
class Bullet:
def __init__(self):
self.pos = [player1.pos[0], player1.pos[1]]
self.direction = math.radians(player1.rotation_angle)
self.bullet = pygame.Surface((5, 20))
self.rotated_bullet = pygame.transform.rotate(self.bullet, (self.direction))
self.bullet.fill((100, 200, 120))
self.time = 0
def shoot(self):
self.pos[0] += math.cos(self.direction) * self.time
self.pos[1] += math.sin(self.direction) * self.time
self.time += 0.5
但这不起作用,结果子弹只是朝某个随机方向移动。我尝试不将角度转换为弧度,并将self.direction
y 轴的值更改为负值,但就是不起作用。我该如何准确计算子弹的方向?谢谢您的帮助。
解决方案 1:
数学运算中角度的单位math.sin
是math.cos
弧度,但角度的单位pygame.transform.rotate()
是度。此外,在旋转之前,您必须创建一个pygame.Surface
带有标志的窗口SRCALPHA
,并用子弹颜色填充表面:
class Bullet:
def __init__(self):
self.pos = [player1.pos[0], player1.pos[1]]
self.direction = math.radians(player1.rotation_angle)
self.bullet = pygame.Surface((10, 5), pygame.SRCALPHA)
self.bullet.fill((100, 200, 120))
self.rotated_bullet = pygame.transform.rotate(self.bullet, player1.rotation_angle)
self.time = 0
在 pygame 中,y 轴指向从顶部到底部。这与通常的笛卡尔坐标系的方向相反。因此,必须反转方向的 y 坐标:
class Bullet:
# [...]
def shoot(self):
self.pos[0] += math.cos(self.direction) * self.time
self.pos[1] -= math.sin(self.direction) * self.time
self.time += 0.5
请看示例:
import pygame
import math
pygame.init()
WIDTH, HEIGHT = 500, 500
window = pygame.display.set_mode((500, 500))
clock = pygame.time.Clock()
class Player():
def __init__(self):
self.rotation_angle = 0
self.player = pygame.Surface((20, 20), pygame.SRCALPHA)
self.player.fill((0, 255, 0))
self.rotated_player = self.player
self.pos = (WIDTH//2, HEIGHT//2)
def rotate(self, keys, left, right):
if keys[right]:
self.rotation_angle -= 0.5
if keys[left]:
self.rotation_angle += 0.5
self.rotated_player = pygame.transform.rotate(self.player, (self.rotation_angle))
class Bullet:
def __init__(self):
self.pos = [player1.pos[0], player1.pos[1]]
self.direction = math.radians(player1.rotation_angle)
self.bullet = pygame.Surface((10, 5), pygame.SRCALPHA)
self.bullet.fill((100, 200, 120))
self.rotated_bullet = pygame.transform.rotate(self.bullet, player1.rotation_angle)
self.time = 0
def shoot(self):
self.pos[0] += math.cos(self.direction) * self.time
self.pos[1] -= math.sin(self.direction) * self.time
self.time += 0.5
player1 = Player()
bullets = []
pos = (250, 250)
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
bullets.append(Bullet())
keys = pygame.key.get_pressed()
player1.rotate(keys, pygame.K_LEFT, pygame.K_RIGHT)
for bullet in bullets[:]:
bullet.shoot()
if not window.get_rect().collidepoint(bullet.pos):
bullets.remove(bullet)
window.fill(0)
window.blit(player1.rotated_player, player1.rotated_player.get_rect(center=player1.pos))
for bullet in bullets:
window.blit(bullet.rotated_bullet, bullet.rotated_bullet.get_rect(center=bullet.pos))
pygame.display.flip()
参见
为什么屏幕上没有出现任何子弹? - pygame
在 pygame 中朝鼠标方向射击子弹
相关推荐
热门文章
项目管理软件有哪些?
- 2025年20款好用的项目管理软件推荐,项目管理提效的20个工具和技巧
- 2024年开源项目管理软件有哪些?推荐5款好用的项目管理工具
- 2024年常用的项目管理软件有哪些?推荐这10款国内外好用的项目管理工具
- 项目管理软件有哪些?推荐7款超好用的项目管理工具
- 项目管理软件有哪些最好用?推荐6款好用的项目管理工具
- 项目管理软件哪个最好用?盘点推荐5款好用的项目管理工具
- 项目管理软件排行榜:2024年项目经理必备5款开源项目管理软件汇总
- 项目管理必备:盘点2024年13款好用的项目管理软件
- 项目管理软件有哪些,盘点推荐国内外超好用的7款项目管理工具
- 2024项目管理软件排行榜(10类常用的项目管理工具全推荐)
热门标签
云禅道AD