gpt4 book ai didi

python - 当我有 x 和 y 坐标时,使物体在轨道上移动

转载 作者:行者123 更新时间:2023-12-01 08:37:51 25 4
gpt4 key购买 nike

这段代码主要只是 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()

最佳答案

您可以使用三角学或 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

关于python - 当我有 x 和 y 坐标时,使物体在轨道上移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53639671/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com