'I kept getting -805306369 (0xCFFFFFFF) error in Python while creating a pygame and fixed it but now I'm not getting any errors but script auto closes
I am creating a pygame simulation for planets rotating around the sun and ran into an error code of -805306369 (0xCFFFFFFF). I think I had an application loop because the app wasn't running and the program was waiting. I am new to Python. I no longer have that error without fixing really fixing the problem but then it started instantly closing so I tried fixing it with "input()" now it stays open but back to the original problem of not responding. Here is the code below mind you, my IDE isn't picking up any errors.
import math
import pygame
pygame.init()
WIDTH, HEIGHT = 800, 800 # px
WIN = pygame.display.set_mode((WIDTH, HEIGHT)) # setting mode to dimensions
pygame.display.set_caption("Planet Sim") # name at the top of the window
# defining name of colors to rgb
WHITE = (255, 255, 255)
YELLOW = (255, 255, 0)
BLUE = (100, 149, 237)
RED = (220, 20, 60)
DARK_GREY = (80, 78, 81)
class Planet:
# define variables
AU = 149.6e6 * 1000 # defining astronomical units
G = 6.67428e-11 # gravitation
SCALE = 220 / AU # 1au = 100px
TIMESTEP = 3600 * 24 # 1DAY
# initiation values
def __init__(self, x, y, radius, color, mass):
# defining name to pera
self.distance_to_sun = None # This just has to be here
self.x = x
self.y = y
self.radius = radius
self.color = color
self.mass = mass
self.orbit = []
self.sun = False # making sure we don't get orbit data from sun
self.distance_to_sun = 0
self.x_vel = 0 # sun doesn't move in this demo
self.y_vel = 0
# starting the event
def draw(self, win):
x = self.x * self.SCALE + WIDTH / 2
y = self.y * self.SCALE + WIDTH / 2
pygame.draw.circle(win, self.color, (x, y), self.radius)
# Gravitation towards each other | how that has effect of orbit
def attraction(self, other):
other_x, other_y = other.x, other.y
distance_x = other_x - self.x # getting magnitude
distance_y = other_y - self.y
distance = math.sqrt(distance_x ** 2 + distance_y ** 2)
if other.sun:
self.distance_to_sun = distance
# physics of gravitation of x to y using force is equal to g(momentum/r^2)
force = self.G * self.mass * other.mass / distance ** 2
theta = math.atan2(distance_y, distance_x)
force_x = math.cos(theta) * force
force_y = math.sin(theta) * force
return force_x, force_y
def update_position(self, planets):
total_fx = total_fy = 0 # total forces exerted on each planet
for planet in planets:
if self == planet:
continue
fx, fy = self.attraction(planet)
total_fx += fx
total_fy += fy
# find x and y velocity
self.x_vel += total_fx / self.mass * self.TIMESTEP
self.y_vel += total_fy / self.mass * self.TIMESTEP
# use velocity to figure out the updated position of the planets
self.x += self.x_vel * self.TIMESTEP
self.y += self.y_vel * self.TIMESTEP
self.orbit.append((self.x, self.y))
def main():
run = True
clock = pygame.time.Clock()
sun = Planet(0, 0, 30, YELLOW, 1.98892 * 10 ** 30)
sun.sun = True
earth = Planet(-1 * Planet.AU, 0, 16, BLUE, 5.9742 * 10 ** 24)
earth.y_vel = 29.783 * 1000
mars = Planet(-1.524 * Planet.AU, 0, 12, RED, 6.39 * 10 ** 23)
mars.y_vel = 24.077 * 1000
mercury = Planet(0.387 * Planet.AU, 0, 8, DARK_GREY, 3.30 * 10 ** 23)
mercury.y_vel = -47.4 * 1000
venus = Planet(0.723 * Planet.AU, 0, 14, WHITE, 4.8685 * 10 ** 24)
venus.y_vel = -35.02 * 1000
planets = [sun, earth, mars, mercury, venus]
while run:
clock.tick(60)
WIN.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
for planet in planets:
planet.update_position(planets)
planet.draw(WIN)
pygame.display.update()
pygame.quit()
input()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|