需要帮助将对象旋转到另一个移动对象 [英] Need help rotating an object towards another moving object

查看:49
本文介绍了需要帮助将对象旋转到另一个移动对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个程序,其中一个对象将在屏幕上朝向您的鼠标旋转.现在我需要让它朝着另一个移动的物体旋转.这是我的代码:

I made a program where an object will rotate towards your mouse on the screen. now I need to make it so that the object will rotate towards another moving object. here is my code:

import pygame
import math
import sys
from pygame.locals import *;
from sys import exit
pygame.init()


blk = pygame.Color(0,0,0)
BG = ('BG.png')
pygame.init()
screen = pygame.display.set_mode((800, 600))
B_G = pygame.image.load(BG).convert_alpha()
clock = pygame.time.Clock()
pygame.mouse.set_visible(False)
fpsclock = pygame.time.Clock()



class Shork(pygame.sprite.Sprite):


 def __init__(self):
  pygame.sprite.Sprite.__init__(self)
  self.image = pygame.image.load('SHORK.png')
  screen = pygame.display.get_surface()
  self.x = 62
  self.y = 50
  self.direction = "down"

 def Moving(self):

  if self.direction == "right":
   self.x += 2
  elif self.direction == "left":
   self.x -= 2
  elif self.direction == "down":
   self.y += 2
  elif self.direction == "up":
   self.y -= 2


 def Path(self):

  if self.x == 62 and self.y == 538:
   self.direction = "right"

  if self.x == 246 and self.y == 538:
   self.direction = "up"

  if self.x == 246 and self.y == 366:
   self.direction = "left"

  if self.x == 176 and self.y == 366:
   self.direction = "up"

  if self.x == 176 and self.y == 114:
   self.direction = "right"

  if self.x == 530 and self.y == 114:
   self.direction = "down"

  if self.x == 530 and self.y == 366:
   self.direction = "left"

  if self.x == 460 and self.y == 366:
   self.direction = "down"

  if self.x == 460 and self.y == 538:
   self.direction = "right"

  if self.x == 644 and self.y == 538:
   self.direction = "up"
  if self.y == 0:
   sys.exit()

Shork = Shork()

Run = True


while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == MOUSEBUTTONDOWN and event.button == 1:
            print("test1")
        elif event.type == MOUSEBUTTONDOWN and event.button == 3:
            print("test3")
    while Run:
      fpsclock.tick(60)

      for event in pygame.event.get():

       if event.type == pygame.QUIT:
          Run = False

    pos = pygame.mouse.get_pos()
    angle = 360-math.atan2(pos[1]-300,pos[0]-400)*180/math.pi
    rotimage = pygame.transform.rotate(B_G,angle)
    Shork.Moving() 
    Shork.Path()
    screen.blit(Shork.image, (Shork.x, Shork.y))
    pygame.display.update()
    rect = rotimage.get_rect(center=(400,300))
    screen.blit(rotimage,rect)
    pygame.display.update()
    screen.fill(blk)

BG 是我需要旋转的对象,SHORK 是 BG 需要旋转的对象.代码的中间部分只是为要遵循的对象路径.我正在努力的代码是这样的:

BG is the object that I need to rotate and SHORK is the object that BG needs to rotate towards. The middle portion of the code is just pathing for an object to follow. The code that I am struggling with is this:

pos = pygame.mouse.get_pos()
angle = 360-math.atan2(pos[1]-300,pos[0]-400)*180/math.pi
rotimage = pygame.transform.rotate(B_G,angle)
Shork.Moving() 
Shork.Path()
screen.blit(Shork.image, (Shork.x, Shork.y))
pygame.display.update()
rect = rotimage.get_rect(center=(400,300))
screen.blit(rotimage,rect)
pygame.display.update()

这目前适用于跟随鼠标,但我一生都无法弄清楚如何使 BG 向 SHORK 旋转.附注我刚刚开始学习python,所以请耐心等待.:)

This currently works for following the mouse but I cannot for the life of me figure out how to make BG rotate towards SHORK. P.S. I am just starting to learn python so please try to be patient. :)

推荐答案

您需要更改 angle 以使用 Shork 的 sx/y 值而不是 pos.您可能还应该在计算角度之前更新 Shork 的值,因此我将 Shork.MovingShork.Path 移动到阻止.

You need to change angle to use Shork's x/y value instead of pos. You should also probably update Shork's values before calculating the angle, so I moved Shork.Moving and Shork.Path to the beginning of the block.

Shork.Moving() 
Shork.Path()
pos = pygame.mouse.get_pos()
angle = 360-math.atan2(Shork.y-300,Shork.x-400)*180/math.pi
rotimage = pygame.transform.rotate(B_G,angle)
screen.blit(Shork.image, (Shork.x, Shork.y))
pygame.display.update()
rect = rotimage.get_rect(center=(400,300))
screen.blit(rotimage,rect)
pygame.display.update()

这篇关于需要帮助将对象旋转到另一个移动对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆