pygame 旋转一条线 [英] pygame rotating a line

查看:90
本文介绍了pygame 旋转一条线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Pygame 中创建一个雷达".我在旋转雷达指针时遇到问题.我如何旋转它?

I am trying to create a "radar" in Pygame. I am having trouble rotating the needle of the radar. How do I rotate it?

import pygame
from pygame.locals import *
SIZE = 800, 800
pygame.init()
screen = pygame.display.set_mode(SIZE)
FPSCLOCK = pygame.time.Clock()
done = False
screen.fill((0, 0, 0))
degree=0
while not done:
    for e in pygame.event.get():
        if e.type == QUIT or (e.type == KEYDOWN and e.key == K_ESCAPE):
            done = True
            break
    for x in range(1,400,10):
        pygame.draw.circle(screen,(255,255,255),(400,400),x,1)
    line = pygame.draw.line(screen,(10,100,10),(400,400),(400,0),3)
    pygame.transform.rotate(line,degree)
        pygame.display.flip()   
        degree+=5
        FPSCLOCK.tick(40)

推荐答案

你需要计算你的开始和结束位置.

You need to calculate your start and end position.

import math
import pygame
from pygame.locals import *

radar = (100,100)
radar_len = 50
x = radar[0] + math.cos(math.radians(angle)) * radar_len
y = radar[1] + math.sin(math.radians(angle)) * radar_len

# then render the line radar->(x,y)
pygame.draw.line(screen, Color("black"), radar, (x,y), 1)

这篇关于pygame 旋转一条线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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