Tanks课程的一部分:
(在下面的代码中:如果没有单词:'turret'包含在下面的任何变量中,那么变量是关于坦克的)
class Tanks(pygame.sprite.Sprite):
def __init__(self,image,location,angle,speed,x_change,y_change,turret_image,turret_angle):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(image)
self.turret_image = pygame.image.load(turret_image)
#Trect is turret rect
self.Trect = self.turret_image.get_rect()
self.rect = self.image.get_rect()
self.rect.left,self.rect.top=location
self.Trect.centerx,self.Trect.bottom = self.rect.centerx,self.rect.centery
经过一些轮换代码......
RealTank.image,rect,turret_image,Trect已更改为旋转图像
screen.blit(RealTank.image,RealTank.rect)
screen.blit(RealTank.turret_image,RealTank.Trect)
炮塔的角度由键'a'和键'd'
改变if event.key == pygame.K_a:
turret_angle_change = 5
elif event.key == pygame.K_d:
turret_angle_change = -5
对于测试我画了一个红色圆圈以找到我的子弹的起点
pygame.draw.circle(screen,red,[RealTank.Trect.centerx,RealTank.Trect.top],5,0)
但奇怪的是,圆圈以一种奇怪的方式旋转
有谁知道我做错了什么?
=============================================== ============================
我的所有代码都在下面(不必要)
图片:
import pygame,math
pygame.init()
red = (155,0,0)
class Tanks(pygame.sprite.Sprite):
def __init__(self,image,location,angle,speed,x_change,y_change,turret_image,turret_angle):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(image)
self.turret_image = pygame.image.load(turret_image)
#Trect is turret rect
self.Trect = self.turret_image.get_rect()
self.rect = self.image.get_rect()
self.rect.left,self.rect.top=location
self.Trect.centerx,self.Trect.bottom = self.rect.centerx,self.rect.centery
self.angle = angle
self.turret_angle = turret_angle
self.speed = speed
self.x_change = x_change
self.y_change = y_change
if self.turret_angle == 360:
self.turret_angle = 0
if self.angle == 360:
self.angle = 0
def rotate(self):
rot_image = pygame.transform.rotate(self.image,self.angle)
rot_rect = rot_image.get_rect(center = self.rect.center)
return rot_image,rot_rect
def turret_rotate(self):
turret_rot_image = pygame.transform.rotate(self.turret_image,self.turret_angle)
turret_rot_rect = turret_rot_image.get_rect(center = self.Trect.midbottom)
return turret_rot_image,turret_rot_rect
def moving_after_angle_change(self):
x=round(math.cos(math.radians(self.angle+90)),1)*self.speed
y=round(math.sin(math.radians(self.angle-90)),1)*self.speed
return x,y
def shoot(self):
#print(self.turret_angle)
print(self.Trect.x,self.Trect.y)
print(pygame.mouse.get_pos())
#initial
player_image_str = 'Tank.png'
player_turret_str = 'turret - Copy.png'
clock = pygame.time.Clock()
FPS = 30
display_width,display_height = 900,600
screen = pygame.display.set_mode([display_width,display_height])
player_location = [600,300]
player_angle = 0
player_angle_change = 0
player_speed = 0
player_x_change=0
player_y_change=0
RealTank_x_change_store=0
RealTank_y_change_store=0
turret_angle = 0
turret_angle_change = 0
#main
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
turret_angle_change = 5
elif event.key == pygame.K_d:
turret_angle_change = -5
elif event.key == pygame.K_UP:
player_speed=2
elif event.key == pygame.K_DOWN:
player_speed=-2
elif event.key == pygame.K_LEFT:
player_angle_change = 2
elif event.key == pygame.K_RIGHT:
player_angle_change = -2
if event.type == pygame.KEYUP:
if event.key == pygame.K_a or event.key == pygame.K_d:
turret_angle_change = 0
elif event.key == pygame.K_UP:
player_speed = 0
elif event.key == pygame.K_DOWN:
player_speed = 0
elif event.key == pygame.K_LEFT:
player_angle_change = 0
elif event.key == pygame.K_RIGHT:
player_angle_change = 0
player_angle+=player_angle_change
turret_angle+=turret_angle_change
RealTank = Tanks(player_image_str,player_location,player_angle,player_speed,
player_x_change,player_y_change,player_turret_str,turret_angle)
RealTank.image,RealTank.rect=RealTank.rotate()
RealTank.turret_image,RealTank.Trect = RealTank.turret_rotate()
RealTank.x_change,RealTank.y_change=RealTank.moving_after_angle_change()
RealTank_x_change_store += RealTank.x_change
RealTank_y_change_store += RealTank.y_change
RealTank.Trect.centerx +=RealTank_x_change_store
RealTank.Trect.centery +=RealTank_y_change_store
RealTank.rect.centerx += RealTank_x_change_store
RealTank.rect.centery += RealTank_y_change_store
RealTank.shoot()
screen.fill([0,0,0])
screen.blit(RealTank.image,RealTank.rect)
screen.blit(RealTank.turret_image,RealTank.Trect)
######### for test only
pygame.draw.circle(screen,red,[RealTank.Trect.centerx,RealTank.Trect.top],5,0)
######
pygame.display.update()
clock.tick(FPS)
pygame.quit()
quit()
答案 0 :(得分:1)
你需要这样的东西
a = RealTank.Trect.centerx + math.cos(math.radians(RealTank.turret_angle+90))*55
b = RealTank.Trect.centery + math.sin(math.radians(RealTank.turret_angle-90))*55
pygame.draw.circle(screen,red,[int(a), int(b)],5,0)
你必须使用炮塔长度而不是100
也许它需要一些计算,因为它不能顺利进行。
编辑:我删除了round()
,现在它移动得更顺畅了。 (我将100改为55)。