问题描述
VIDEO我已经有一段时间遇到这个问题了,我试图让我的笔直跟随我的枪尖,这样我才能使子弹看起来像它们从我的枪尖中出来了,但我不确定如何我可以做到的是,我只能使枪旋转,而不能使子弹看起来像是从枪口VIDEO中冒出来的,因为您可以看到我已经弄污了枪的图像和一个小矩形,我将如何做到这一点用枪尖旋转吗?枪在什么位置
import pygame,random,math
pygame.init()
window = pygame.display.set_mode((500,500))
pygame.display.set_caption("um..")
# how the gun is blitted
def blitRotate(surf,image,pos,originPos,angle):
# calcaulate the axis aligned bounding Box of the rotated image
w,h = image.get_size()
sin_a,cos_a = math.sin(math.radians(angle)),math.cos(math.radians(angle))
min_x,min_y = min([0,sin_a*h,cos_a*w,sin_a*h + cos_a*w]),max([0,sin_a*w,-cos_a*h,sin_a*w - cos_a*h])
# calculate the translation of the pivot
pivot = pygame.math.Vector2(originPos[0],-originPos[1])
pivot_rotate = pivot.rotate(angle)
pivot_move = pivot_rotate - pivot
# calculate the upper left origin of the rotated image
origin = (pos[0] - originPos[0] + min_x - pivot_move[0],pos[1] - originPos[1] - min_y + pivot_move[1])
# get a rotated image
rotated_image = pygame.transform.rotate(image,angle)
# rotate and blit the image
surf.blit(rotated_image,origin)
# the gun
class handgun():
def __init__(self,x,y,height,width,color):
self.x = x
self.y = y
self.height = height
self.width = width
self.color = color
self.rect = pygame.Rect(x,width)
# LOL THESE IS THE HAND
self.shootsright = pygame.image.load("hands.png")
self.image = self.shootsright
self.rect = self.image.get_rect(center = (self.x,self.y))
self.look_at_pos = (self.x,self.y)
self.isLookingAtPlayer = False
self.look_at_pos = (x,y)
self.hitBox = (self.x + -18,self.y,46,60)
self.gunDirection = "right"
def draw(self,drawX,drawY):
self.rect.topleft = (drawX,drawY)
# the guns hitBox
# rotatiing the gun
dx = self.look_at_pos[0] - self.rect.centerx
dy = self.look_at_pos[1] - self.rect.centery
angle = (190/math.pi) * math.atan2(-dy,dx)
gun_size = self.image.get_size()
pivot = (8,gun_size[1]//2)
blitRotate(window,self.image,self.rect.center,pivot,angle)
if((angle > 90 or angle < -90) and self.gunDirection != "left"):
self.gunDirection = "left"
self.image = pygame.transform.flip(self.image,False,True)
if((angle < 90 and angle > -90) and self.gunDirection != "right"):
self.gunDirection = "right"
self.image = pygame.transform.flip(self.image,True)
def lookAt( self,coordinate ):
self.look_at_pos = coordinate
white = (255,255,255)
handgun1 = handgun(300,300,10,white)
# the square that will be rotating with the gun
class portal:
def __init__(self,width)
self.direction = "blobright"
self.angle = 0
def draw(self,dx,dy):
self.rect.topleft = (dx,dy)
pygame.draw.rect(window,self.color,self.rect)
white = (0,0)
port1 = portal(200,200,white)
# the main loop
runninggame = True
while runninggame:
for event in pygame.event.get():
if event.type == pygame.QUIT:
runninggame = False
# fill the window black
window.fill((255,255))
# draw the rect that will be rotating with the gun tips
port1.draw(handgun1.x,handgun1.y)
# draw the gun
handgun1.draw(port1.x,port1.y)
handgun1.direction = "right"
# gun rotation
mousex,mousey = pygame.mouse.get_pos()
if not handgun1.isLookingAtPlayer:
handgun1.lookAt((mousex,mousey))
pygame.display.update()
pygmae.quit()
解决方法
我不清楚您的要求,但是我更新了代码,因此矩形将始终位于喷枪的射击线上,并且始终在100像素远。
要更新代码:
-在手枪类中,将所有angle
的引用更改为self.angle
,以便可以在类外部访问角度。
-在门户类(绘制方法)中,使用枪角定位矩形:
def draw(self,dx,dy):
# keep same distance from gun,use gun angle
self.dist = 100
dx = handgun1.x + self.dist*math.cos(-handgun1.angle*(math.pi/180)) -65 # why offset needed ?
dy = handgun1.y + self.dist*math.sin(-handgun1.angle*(math.pi/180)) -50 # why offset needed ?
self.rect.topleft = (dx,dy)
pygame.draw.rect(window,self.color,self.rect)
为了使圆与枪的原点对齐,我必须偏移矩形的原点(-65,-50)。我不知道为什么要这么做。