亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在pygame中向鼠標方向射擊子彈

在pygame中向鼠標方向射擊子彈

大話西游666 2022-07-12 15:29:08
我只是想不通為什么我的子彈不起作用。我做了一個子彈課,這里是:class Bullet:    def __init__(self):        self.x = player.x        self.y = player.y        self.height = 7        self.width = 2        self.bullet = pygame.Surface((self.width, self.height))        self.bullet.fill((255, 255, 255))現在我在我的游戲類中添加了幾個函數,這是新代碼:class Game:    def __init__(self):        self.bullets = []        def shoot_bullet(self):         if self.bullets:            for bullet in self.bullets:                rise = mouse.y - player.y                run = mouse.x - player.x                angle = math.atan2(rise, run)                bullet.x += math.cos(angle)                bullet.y += math.sin(angle)                pygame.transform.rotate(bullet.bullet, -math.degrees(angle))                D.blit(bullet.bullet, (bullet.x, bullet.y))    def generate_bullet(self):        if  mouse.is_pressed():            self.bullets.append(Bullet())我期望代碼做的是每次按下鼠標按鈕時Bullet()都會添加一個,然后計算玩家和鼠標之間的角度并相應地朝鼠標的方向射擊子彈。然而,結果是一團糟,子彈實際上不旋轉也不移動。它們被生成并奇怪地移動到屏幕的左側。我不確定我是否搞砸了,或者我使用的方法完全錯誤。game.bulletsgame.shoot_bullet
查看完整描述

1 回答

?
弒天下

TA貢獻1818條經驗 獲得超8個贊

首先pygame.transform.rotate不變換對象本身,而是創建一個新的旋轉表面并將其返回。


如果你想向某個方向發射子彈,方向是在子彈發射的那一刻定義的,但它不會連續改變。當子彈發射時,設置子彈的起始位置并計算到鼠標位置的方向向量:


self.pos = (x, y)

mx, my = pygame.mouse.get_pos()

self.dir = (mx - x, my - y)

方向向量不應該取決于到鼠標的距離,但它必須是單位向量。通過除以歐幾里得距離來歸一化向量


length = math.hypot(*self.dir)

if length == 0.0:

    self.dir = (0, -1)

else:

    self.dir = (self.dir[0]/length, self.dir[1]/length)

計算向量的角度并旋轉子彈。一般來說,向量的角度可以通過 計算atan2(y, x)。atan2(-y, x)y軸需要反轉(


angle = math.degrees(math.atan2(-self.dir[1], self.dir[0]))


self.bullet = pygame.Surface((7, 2)).convert_alpha()

self.bullet.fill((255, 255, 255))

self.bullet = pygame.transform.rotate(self.bullet, angle)

要更新子彈的位置,只需縮放方向(按速度)并將其添加到子彈的位置:


self.pos = (self.pos[0]+self.dir[0]*self.speed, 

            self.pos[1]+self.dir[1]*self.speed)

要將旋轉的子彈繪制在正確的位置,請選取旋轉子彈的邊界矩形并設置中心點self.pos(請參閱如何使用 PyGame 圍繞其中心旋轉圖像?):


bullet_rect = self.bullet.get_rect(center = self.pos)

surf.blit(self.bullet, bullet_rect)  

另請參閱向目標或鼠標射擊子彈


最小的例子: repl.it/@Rabbid76/PyGame-FireBulletInDirectionOfMouse

https://i.stack.imgur.com/oyzor.gif

import pygame

import math


pygame.init()

window = pygame.display.set_mode((500, 500))

clock = pygame.time.Clock()


class Bullet:

    def __init__(self, x, y):

        self.pos = (x, y)

        mx, my = pygame.mouse.get_pos()

        self.dir = (mx - x, my - y)

        length = math.hypot(*self.dir)

        if length == 0.0:

            self.dir = (0, -1)

        else:

            self.dir = (self.dir[0]/length, self.dir[1]/length)

        angle = math.degrees(math.atan2(-self.dir[1], self.dir[0]))


        self.bullet = pygame.Surface((7, 2)).convert_alpha()

        self.bullet.fill((255, 255, 255))

        self.bullet = pygame.transform.rotate(self.bullet, angle)

        self.speed = 2


    def update(self):  

        self.pos = (self.pos[0]+self.dir[0]*self.speed, 

                    self.pos[1]+self.dir[1]*self.speed)


    def draw(self, surf):

        bullet_rect = self.bullet.get_rect(center = self.pos)

        surf.blit(self.bullet, bullet_rect)  


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.MOUSEBUTTONDOWN:

            bullets.append(Bullet(*pos))


    for bullet in bullets[:]:

        bullet.update()

        if not window.get_rect().collidepoint(bullet.pos):

            bullets.remove(bullet)


    window.fill(0)

    pygame.draw.circle(window, (0, 255, 0), pos, 10)

    for bullet in bullets:

        bullet.draw(window)

    pygame.display.flip()


查看完整回答
反對 回復 2022-07-12
  • 1 回答
  • 0 關注
  • 240 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號