首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在游戏中,你如何将枪管指向鼠标?

在游戏中,你如何将枪管指向鼠标?
EN

Stack Overflow用户
提问于 2021-12-09 00:26:48
回答 1查看 195关注 0票数 1

我已经修复了this帖子中指出的问题,但现在我对如何将灰色矩形(桶)指向我的鼠标感到困惑。有人能帮忙吗?谢谢!

这是我的代码:

代码语言:javascript
运行
复制
import pygame
import math

pygame.init()

screen = pygame.display.set_mode((400, 400))

pygame.display.set_caption("diep.io")

screen.fill((255,255,255))

auto_shoot = False

class Bullet:
  def __init__(self, x_move, y_move, x_loc, y_loc):
    self.image = pygame.image.load("C:\\Users\\ender\\OneDrive\\Documents\\repos\\Bullet.png")
    self.x_move = x_move
    self.y_move = y_move
    self.x_loc = x_loc
    self.y_loc = y_loc
    self.bullet_rect = self.image.get_rect()
  
  def update(self):
    self.x_loc += self.x_move
    self.y_loc += self.y_move   
    self.bullet_rect.center = round(self.x_loc), round(self.y_loc)

    rect = screen.blit(self.image, self.bullet_rect)
    if not screen.get_rect().contains(rect):
      bullets.remove(self)

    if self.x_loc > 400 or self.y_loc > 400:
      bullets.remove(self)

bullet = None
bullets = []

while True:
  screen.fill((255, 255, 255))
  pygame.draw.rect(screen, (100, 100, 100), (205, 193, 25, 15)) # This is the rectangle I want to point at my mouse
  pygame.draw.circle(screen, (82, 219, 255), (200, 200), 15)
  for event in pygame.event.get():
    if event.type == pygame.MOUSEBUTTONUP:
      x = pygame.mouse.get_pos()[0] - 200
      y = pygame.mouse.get_pos()[1] - 200
      pythag = float(math.sqrt(x**2 + y**2))
      bullets.append(Bullet(x/pythag, y/pythag, 200, 200))
  for bullet in bullets:
    bullet.update()
  pygame.display.update()
  pygame.time.delay(10)

注意:这与其他问题不同,因为我试图在鼠标上指向一个矩形,而其他帖子则将图像指向鼠标。这不是我想做的。其次,游戏矩形是由其左下角定义的,因此这进一步使事情复杂化。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-11 19:25:54

关于子弹的旋转,请参见How to move a sprite according to an angle in Pygamecalculating direction of the player to shoot PyGmae

计算方向矢量grees(math.atan2(-y_move, x_move))的角度并旋转子弹图像:

代码语言:javascript
运行
复制
class Bullet:
    def __init__(self, x_move, y_move, x_loc, y_loc):
        self.image = pygame.image.load("C:\\Users\\ender\\OneDrive\\Documents\\repos\\Bullet.png")
        self.image = pygame.transform.rotate(self.image, math.degrees(math.atan2(-y_move, x_move)))

旋转长方形要复杂一些。在pygame.Surface上绘制大炮

代码语言:javascript
运行
复制
cannon = pygame.Surface((50, 50), pygame.SRCALPHA)
pygame.draw.rect(cannon, (100, 100, 100), (30, 17, 25, 15))
pygame.draw.circle(cannon, (82, 219, 255), (25, 25), 15)

How do I rotate an image around its center using PyGame?。编写一个旋转和blit曲面的函数

代码语言:javascript
运行
复制
def blitRotateCenter(surf, image, center, angle):
    rotated_image = pygame.transform.rotate(image, angle)
    new_rect = rotated_image.get_rect(center = image.get_rect(center = center).center)
    surf.blit(rotated_image, new_rect)

使用应用程序循环中的函数绘制加农炮:

代码语言:javascript
运行
复制
while True:
    # [...]
    
    x = pygame.mouse.get_pos()[0] - 200
    y = pygame.mouse.get_pos()[1] - 200
    angle = math.degrees(math.atan2(-y, x)) 
    blitRotateCenter(screen, cannon, (200, 200), angle)
    
    # [...]

另见How to rotate an image(player) to the mouse direction?How can you rotate the sprite and shoot the bullets towards the mouse position?

完整的例子:

代码语言:javascript
运行
复制
import pygame
import math

pygame.init()
screen = pygame.display.set_mode((400, 400))
pygame.display.set_caption("diep.io")
screen.fill((255,255,255))
clock = pygame.time.Clock()

class Bullet:
    def __init__(self, x_move, y_move, x_loc, y_loc):
        #self.image = pygame.image.load("C:\\Users\\ender\\OneDrive\\Documents\\repos\\Bullet.png")
        self.image = pygame.Surface((20, 5), pygame.SRCALPHA)
        self.image.fill((255, 0, 0))
        self.image = pygame.transform.rotate(self.image, math.degrees(math.atan2(-y_move, x_move)))
        self.x_move = x_move
        self.y_move = y_move
        self.x_loc = x_loc
        self.y_loc = y_loc
        self.bullet_rect = self.image.get_rect()
    
    def update(self):
        self.x_loc += self.x_move
        self.y_loc += self.y_move   
        self.bullet_rect.center = round(self.x_loc), round(self.y_loc)
        rect = screen.blit(self.image, self.bullet_rect)
        if not screen.get_rect().contains(rect):
            bullets.remove(self)
        if self.x_loc > 400 or self.y_loc > 400:
            bullets.remove(self)

cannon = pygame.Surface((50, 50), pygame.SRCALPHA)
pygame.draw.rect(cannon, (100, 100, 100), (30, 17, 25, 15))
pygame.draw.circle(cannon, (82, 219, 255), (25, 25), 15)

def blitRotateCenter(surf, image, center, angle):
    rotated_image = pygame.transform.rotate(image, angle)
    new_rect = rotated_image.get_rect(center = image.get_rect(center = center).center)
    surf.blit(rotated_image, new_rect)

bullet = None
bullets = []
auto_shoot = False
run = True
while run:
    x = pygame.mouse.get_pos()[0] - 200
    y = pygame.mouse.get_pos()[1] - 200
    angle = math.degrees(math.atan2(-y, x))  
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.MOUSEBUTTONUP:
            pythag = float(math.sqrt(x**2 + y**2))
            bullets.append(Bullet(x/pythag, y/pythag, 200, 200))
    
    screen.fill((255, 255, 255))
    for bullet in bullets:
        bullet.update()
    blitRotateCenter(screen, cannon, (200, 200), angle)
    pygame.display.update()
    clock.tick(100)

pygame.quit()
exit()
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70283340

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档