Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 旋转直线的游戏_Python_Pygame - Fatal编程技术网

Python 旋转直线的游戏

Python 旋转直线的游戏,python,pygame,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

我试图在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 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)

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

import sys
import pygame

pygame.init()

screen = pygame.display.set_mode((640, 480))
FPSCLOCK = pygame.time.Clock()
RED = pygame.Color("red")
startpoint = pygame.math.Vector2(320, 240)
endpoint = pygame.math.Vector2(170, 0)
angle = 0
done = False

while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    # % 360 to keep the angle between 0 and 360.
    angle = (angle+5) % 360
    # The current endpoint is the startpoint vector + the
    # rotated original endpoint vector.
    current_endpoint = startpoint + endpoint.rotate(angle)

    screen.fill((0, 0, 0))
    pygame.draw.line(screen, RED, startpoint, current_endpoint, 2)

    pygame.display.flip()
    FPSCLOCK.tick(30)

pygame.quit()
sys.exit()

上面的代码有什么问题?还有,您是否有缩进问题(最后三行)?在上面的代码中,该行没有旋转..pygame.transform仅适用于rect对象