pygame旋转一条线

时间:2013-08-12 05:08:20

标签: python pygame

我正试图在Pygame中创建一个“雷达”。我无法旋转雷达的针头。如何旋转它?

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)

这里解决了:

import pygame
import math
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:
    screen.fill(0)
    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)    
    radar = (400,400)
    radar_len = 400
    x = radar[0] + math.cos(math.radians(degree)) * radar_len
    y = radar[1] + math.sin(math.radians(degree)) * radar_len

    # then render the line radar->(x,y)
    pygame.draw.line(screen, Color("red"), radar, (x,y), 1)
    pygame.display.flip()   
    degree+=5
    FPSCLOCK.tick(40)

使用以下行:

x = radar[0] + math.cos(math.radians(degree)) * radar_len
y = radar[1] + math.sin(math.radians(degree)) * radar_len

取一个angle更改每一帧,从屏幕中心创建一条长度为radar_len

的行

2 个答案:

答案 0 :(得分:5)

您需要计算开始和结束位置。

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)

答案 1 :(得分:2)

您也可以使用向量,一个用于“起点”,一个用于端点(即从起点开始的偏移量),然后旋转端点向量并将其添加到起始点。

SELECT u.productID, 
    u.attributeValue,
    NEW_attributeValue = (SELECT attributeValue FROM table1 WHERE productID = u.productID and attributeID = 'a02')

    FROM table1 AS u
    WHERE attributeID = 'a01'
相关问题