当我有 x 和 y 坐标时,使物体在轨道上移动 [英] Making an object move in an orbit when I have the x and y coordinates

查看:42
本文介绍了当我有 x 和 y 坐标时,使物体在轨道上移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这段代码主要只是一个 pygame 窗口的通用启动,但我正在努力使对象(我制作的行星物体)在围绕我制作的太阳物体的轨道上移动我给的坐标.我知道 x 和 y 值正在更新,但我不明白为什么对象不移动.

#导入库导入pygame导入数学#classes类按钮:def _init_(自我,屏幕,颜色,x,y,宽度,高度,字母):self.screen = 屏幕self.color = 颜色自我.x = x自我.y = yself.width = 宽度self.height = 高度self.letter = 字母self.radius = 半径定义绘制(自我):pygame.draw.rect(self.screen, self.colour,(self.x,self.y, self.width, self.height))如果 self.letter!='+' 和 self.letter!='-':font = pygame.font.SysFont('agencyfb',15,True,False)别的: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_rect)班级轨道:def __init__(self,screen,colour,x,y,radius,width):self.screen = 屏幕self.color = 颜色自我.x = x自我.y = yself.width = 宽度self.radius = 半径def draw_circle(self):pygame.draw.circle(self.screen,self.colour,(self.x,self.y),self.radius,self.width)#定义颜色##Sun = pygame.draw.circle(screen,Sun,[1000,450],100,0)黑色 = (0,0,0)白色 = (255,255,255)绿色 = (0,255,0)红色 = (255,0,0)蓝色 = (0,0,255)太阳 = (255,69,0)太阳 = []行星 = []#初始化引擎pygame.init()#打开一个窗口大小 = (1920,1080)屏幕 = pygame.display.set_mode(size)#设置窗口标题pygame.display.set_caption("轨道模拟器")#loop 直到用户点击关闭按钮完成 = 错误#x=1000y=450Sun.append(orbit(screen,Red,1000,450,100,0))Planet.append(orbit(screen,White,x,y,50,0))##用于管理屏幕更新的速度时钟 = pygame.time.Clock()#------ 主程序循环------虽然没有完成:#--- 主事件循环对于 pygame.event.get() 中的事件:#user 做了一些事情if event.type == pygame.QUIT: #if 用户点击关闭done = True #标志我们完成并退出循环#------ 游戏逻辑应该放在这里------#------ 绘图代码应该放在这里 -------#首先,将屏幕清除为白色.不要在此之上放置其他绘图命令,否则它们将被此命令擦除.屏幕填充(黑色)对于我在太阳:i.draw_circle()对于我在星球:r=150角度=0计数 = 0而角度 <= 360:角度弧度 = math.radians(角度)x = int(math.cos(angle_radians)*r)y = int(math.sin(angle_radians)*r)角度 +=1计数 +=1打印(计数)x+=1000y+=450pygame.draw.circle(屏幕,白色,[x,y],10,0)print("位置 [",x,",",y,"]")#更新屏幕pygame.display.flip()#------ 限制为每秒 60 帧------时钟滴答(60)#------ 当循环结束时,退出------pygame.quit()

解决方案

您可以使用三角学或

This code is mostly just the generic start up of a pygame window but I'm trying to make it so the object moves (the planet object I've made) in the orbit around the sun object I've made for the coordinates I've given it. I know the x and y values are updating but I don't understand why the object doesn't move.

#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()

解决方案

You can make an object rotate around another by using trigonometry or vectors. With vectors you just have to rotate a vector which defines the offset from the rotation center each frame and add it to the position vector (self.pos which is the rotation center) to get the desired self.rect.center coordinates of the orbiting object.

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()

这篇关于当我有 x 和 y 坐标时,使物体在轨道上移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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