当我有x和y坐标时使物体沿轨道运动

时间:2018-12-05 19:46:24

标签: python python-3.x pygame

此代码主要只是pygame窗口的常规启动,但我正在尝试使其运行,以便使该对象(为我制作的行星对象)在为该对象制作的太阳对象周围的轨道中移动。我给出的坐标。我知道x和y值正在更新,但我不明白为什么对象不移动。

#import the library
import pygame
import math


#classes

class button:
    def _init_ (self,screen, colour, x, y, width,height, letter):
        self.screen = screen
        self.colour = colour
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.letter = letter
        self.radius = radius
    def draw(self):
        pygame.draw.rect(self.screen, self.colour,(self.x,self.y, self.width, self.height))
        if self.letter!= '+' and self.letter!= '-':
            font = pygame.font.SysFont('agencyfb',15,True,False)
        else:
            font = pygame.font.SysFont('agencyfb',25,True,False)

        text = font.render(self.letter, True, black)
        text_rect = text.get_rect(center=(self.x+self.width/2,self.y+self.height/2))
        screen.blit(text, text_rect)


class orbit:
    def __init__(self,screen,colour,x,y,radius,width):
        self.screen = screen
        self.colour = colour
        self.x = x
        self.y = y
        self.width = width
        self.radius = radius
    def draw_circle(self):
        pygame.draw.circle(self.screen,self.colour,(self.x,self.y),self.radius,self.width)




#define colours
##Sun = pygame.draw.circle(screen,Sun,[1000,450],100,0)
Black = (0,0,0)
White = (255,255,255)
Green = (0,255,0)
Red = (255,0,0)
Blue = (0,0,255)
Sun = (255,69,0)
Sun = []
Planet = []


#initialise the engine

pygame.init()

#Opening a window

size = (1920,1080)

screen = pygame.display.set_mode(size)

#set window title

pygame.display.set_caption("Orbit Simulator")

#loop unti the user clicks the close button

done = False
#
x=1000
y=450
Sun.append(orbit(screen,Red,1000,450,100,0))
Planet.append(orbit(screen,White,x,y,50,0))


#

#used to manage how fast the screen updates

clock = pygame.time.Clock()
#------ Main program Loop ------

while not done:

    #--- Main event loop

    for event in pygame.event.get(): #user did something

        if event.type == pygame.QUIT: #if user clicked close

            done = True #flag that we are done and exit the loop



    #------ Game logic should go here ------



    #------ Drawing code should go here -------


    #first, clear the screen to white. Don't put other drawing commands above this or they will be erased with this command.

    screen.fill(Black)


    for i in Sun:
        i.draw_circle()
    for i in Planet:
            r=150
            angle=0
            count = 0
            while angle <= 360:

                angle_radians = math.radians(angle)

                x = int(math.cos(angle_radians)*r)
                y = int(math.sin(angle_radians)*r)


                angle +=1
                count +=1
                print(count)
                x+=1000
                y+=450

                pygame.draw.circle(screen,White,[x,y],10,0)

                print("Position [",x,",",y,"]")



    #update the screen

    pygame.display.flip()

    #------ Limit to 60 frames per second ------

    clock.tick(60)

#------ When the loop ends, quit ------

pygame.quit()

1 个答案:

答案 0 :(得分:1)

您可以使用三角法或vectors使对象围绕另一个对象旋转。使用矢量,您只需要旋转一个矢量,该矢量定义每帧相对于旋转中心的偏移量,并将其添加到位置矢量(self.pos,它是旋转中心)中即可获得所需的self.rect.center坐标。轨道物体。

import pygame as pg
from pygame.math import Vector2


class Planet(pg.sprite.Sprite):

    def __init__(self, pos, *groups):
        super().__init__(*groups)
        self.image = pg.Surface((40, 40), pg.SRCALPHA)
        pg.draw.circle(self.image, pg.Color('dodgerblue'), (20, 20), 20)
        self.rect = self.image.get_rect(center=pos)
        self.pos = Vector2(pos)
        self.offset = Vector2(200, 0)
        self.angle = 0

    def update(self):
        self.angle -= 2
        # Add the rotated offset vector to the pos vector to get the rect.center.
        self.rect.center = self.pos + self.offset.rotate(self.angle)


def main():
    pg.init()
    screen = pg.display.set_mode((640, 480))
    screen_rect = screen.get_rect()
    clock = pg.time.Clock()
    all_sprites = pg.sprite.Group()
    planet = Planet(screen_rect.center, all_sprites)
    yellow = pg.Color('yellow')

    while True:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                return

        all_sprites.update()
        screen.fill((30, 30, 30))
        pg.draw.circle(screen, yellow, screen_rect.center, 60)
        all_sprites.draw(screen)

        pg.display.flip()
        clock.tick(60)


if __name__ == '__main__':
    main()
    pg.quit()

enter image description here