python - 如何在任何位置用鼠标射击弹丸?
问题描述
我用按钮左右射击了我的弹丸,但是点击很多按钮会很无聊如何用我的鼠标射击?在任意位置 x,y
# projectile class
if keys[pygame.K_f]:
for bullet in bullets:
if bullet.x < 500 and bullet.x > 0:
bullet.x += bullet.speed
else:
bullets.pop(bullets.index(bullet))
if len(bullets) < 2:
bullets.append(projectile(round(playerman.x+playerman.width//2),round(playerman.y + playerman.height-54),(0,0,0)))
if keys[pygame.K_g]:
for bullet in bullets:
if bullet.x < 500 and bullet.x > 0:
bullet.x -= bullet.speed
else:
bullets.pop(bullets.index(bullet))
if len(bullets) < 2:
bullets.append(projectile(round(playerman.x+playerman.width//2),round(playerman.y + playerman.height-54),(0,0,0)))
# Jump and Collisions
这是我的弹丸课
class projectile(object):
def __init__(self, x, y,color):
self.x = x
self.y = y
self.slash = pygame.image.load("heart.png")
self.rect = self.slash.get_rect()
self.rect.topleft = ( self.x, self.y )
self.speed = 10
self.color = color
def draw(self, window):
self.rect.topleft = ( self.x,self.y )
window.blit(slash, self.rect)
解决方案
您必须将移动方向 ( dirx
, diry
) 添加到类射弹中。进一步添加一个移动子弹的方法:
class projectile(object):
def __init__(self, x, y, dirx, diry, color):
self.x = x
self.y = y
self.dirx = dirx
self.diry = diry
self.slash = pygame.image.load("heart.png")
self.rect = self.slash.get_rect()
self.rect.topleft = ( self.x, self.y )
self.speed = 10
self.color = color
def move(self):
self.x += self.dirx * self.speed
self.y += self.diry * self.speed
def draw(self, window):
self.rect.topleft = (round(self.x), round(self.y))
window.blit(slash, self.rect)
计算按下鼠标按钮时从玩家到鼠标的方向并生成一个新子弹。方向由玩家到缪斯位置 ( mouse_x - start_x
, mouse_y - start_y
) 的向量给出。必须通过将向量分量除以欧几里得距离来对向量进行归一化(单位向量) :
for event in pygame.event.get():
# [...]
if event.type == pygame.MOUSEBUTTONDOWN:
if len(bullets) < 2:
start_x, start_y = playerman.x+playerman.width//2, playerman.y + playerman.height-54
mouse_x, mouse_y = event.pos
dir_x, dir_y = mouse_x - start_x, mouse_y - start_y
distance = math.sqrt(dir_x**2 + dir_y**2)
if distance > 0:
new_bullet = projectile(start_x, start_y, dir_x/distance, dir_y/distance, (0,0,0))
bullets.append(new_bullet)
在主应用程序循环中循环移动项目符号,如果项目符号超出窗口,则将其移除
run = True
while run:
# [...]
for event in pygame.event.get():
# [...]
for bullet in bullets[:]:
bullet.move()
if bullet.x < 0 or bullet.x > 500 or bullet.y < 0 or bullet.y > 500:
bullets.pop(bullets.index(bullet))
# [...]
示例代码
runninggame = True
while runninggame:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
runninggame = False
if event.type == pygame.MOUSEBUTTONDOWN:
if len(bullets) < 2:
start_x, start_y = playerman.x+playerman.width//2, playerman.y + playerman.height-54
mouse_x, mouse_y = event.pos
dir_x, dir_y = mouse_x - start_x, mouse_y - start_y
distance = math.sqrt(dir_x**2 + dir_y**2)
if distance > 0:
new_bullet = projectile(start_x, start_y, dir_x/distance, dir_y/distance, (0,0,0))
bullets.append(new_bullet)
for bullet in bullets[:]:
bullet.move()
if bullet.x < 0 or bullet.x > 800 or bullet.y < 0 or bullet.y > 800:
bullets.pop(bullets.index(bullet))
# [...]
推荐阅读
- javascript - Javascript,返回主函数时出错
- c# - HttpDelete 带多个参数批量删除
- javascript - 如何使用 react-router-dom 和 BrowserRouter 在组件外部导航
- algorithm - 从不断更新的列表中获取最大数量
- python - 使用 keras/tf 在 CNN 中获取梯度值
- c# - 从 Nitra 语法迁移到 Antlr4
- regex - 在 awk 的正则表达式中删除除逗号之外的所有标点符号
- c# - 本地化 TextBlock 格式的文本
- html - 浏览 html 表格中输入的单元格
- javascript - 在网站上推进图像滑块时页面滚动