Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import simplegui
- import math
- Width = 900
- Height = 900
- boost_message = "Hold spacebar to boost"
- boost_message_color = "Yellow"
- player_sprite = simplegui.load_image("https://i.imgrpost.com/imgr/2018/03/01/fhN53R.png")
- sun_sprite_image = simplegui.load_image("https://i.imgrpost.com/imgr/2018/03/01/Vsd87P.png")
- sun_atmosphere_image = simplegui.load_image("https://i.imgrpost.com/imgr/2018/03/01/bf7wqA.png")
- class player_1:
- def __init__(self, circle1_center, circle1_radius, velocity_circle1, acceleration1, bounce_distance1, circle1_mass):
- self.circle1_center = circle1_center
- self.circle1_radius = circle1_radius
- self.velocity_circle1 = velocity_circle1
- self.acceleration1 = acceleration1
- self.bounce_distance1 = bounce_distance1
- self.circle1_mass = circle1_mass
- def keydown1(self, key):
- global boost_message, boost_message_color
- if key == simplegui.KEY_MAP["left"]:
- self.velocity_circle1[0] -= self.acceleration1
- elif key == simplegui.KEY_MAP["up"]:
- self.velocity_circle1[1] -= self.acceleration1
- elif key == simplegui.KEY_MAP["right"]:
- self.velocity_circle1[0] += self.acceleration1
- elif key == simplegui.KEY_MAP["down"]:
- self.velocity_circle1[1] += self.acceleration1
- elif key == simplegui.KEY_MAP["space"]:
- self.acceleration1 *= 3
- boost_message = "BOOST ACTIVATED"
- boost_message_color = "Red"
- def keyup1(self, key):
- global boost_message, boost_message_color
- if key == simplegui.KEY_MAP["left"]:
- self.velocity_circle1[0] = 0
- elif key == simplegui.KEY_MAP["up"]:
- self.velocity_circle1[1] = 0
- elif key == simplegui.KEY_MAP["right"]:
- self.velocity_circle1[0] = 0
- elif key == simplegui.KEY_MAP["down"]:
- self.velocity_circle1[1] = 0
- elif key == simplegui.KEY_MAP["space"]:
- self.acceleration1 /= 3
- boost_message = "Hold spacebar to boost"
- boost_message_color = "Yellow"
- def draw_player_1_sprite(self, canvas):
- global Width, Height
- canvas.draw_image(player_sprite, [1060/2, 1060/2], [1060, 1060], self.circle1_center, [40, 40])
- def Movement(self):
- self.circle1_center[0] = self.circle1_center[0] + self.velocity_circle1[0]
- self.circle1_center[1] = self.circle1_center[1] + self.velocity_circle1[1]
- def Border_collisions(self):
- global Width, Height
- if self.circle1_center[0] <= self.circle1_radius:
- self.circle1_center[0] += self.bounce_distance1
- elif self.circle1_center[0] >= Width-self.circle1_radius:
- self.circle1_center[0] -= self.bounce_distance1
- elif self.circle1_center[1] <= self.circle1_radius:
- self.circle1_center[1] += self.bounce_distance1
- elif self.circle1_center[1] >= Height-self.circle1_radius:
- self.circle1_center[1] -= self.bounce_distance1
- class sun_sprite:
- def __init__(self, sun_hitbox_center, sun_hitbox_radius):
- self.sun_hitbox_center = sun_hitbox_center
- self.sun_hitbox_radius = sun_hitbox_radius
- def draw_sun(self, canvas):
- global Width, Height
- canvas.draw_image(sun_sprite_image, [3002/2, 3005/2], [3002, 3005], self.sun_hitbox_center, [100, 100])
- def sun_orbit_collisions(self):
- global Call_player_1
- x = math.pow((self.sun_hitbox_center[0]-Call_player_1.circle1_center[0]), 2)
- y = math.pow((self.sun_hitbox_center[1]-Call_player_1.circle1_center[1]), 2)
- distance = math.sqrt(x + y)
- #Collisions:
- if Call_player_1.circle1_radius+self.sun_hitbox_radius >= distance:
- if Call_player_1.circle1_center[0] > self.sun_hitbox_center[0]:
- Call_player_1.circle1_center[0] += Call_player_1.bounce_distance1
- elif Call_player_1.circle1_center[1] > self.sun_hitbox_center[1]:
- Call_player_1.circle1_center[1] += Call_player_1.bounce_distance1
- elif Call_player_1.circle1_center[0] < self.sun_hitbox_center[0]:
- Call_player_1.circle1_center[0] -= Call_player_1.bounce_distance1
- elif Call_player_1.circle1_center[1] < self.sun_hitbox_center[1]:
- Call_player_1.circle1_center[1] -= Call_player_1.bounce_distance1
- class sun_atmosphere:
- def __init__(self, sun_atmosp_center, sun_atmosp_radius, sun_atmosp_mass, sun_atmosp_time):
- self.sun_atmosp_center = sun_atmosp_center
- self.sun_atmosp_radius = sun_atmosp_radius
- self.sun_atmosp_mass = sun_atmosp_mass
- self.sun_atmosp_time = sun_atmosp_time
- def draw_atmosphere(self, canvas):
- global Width, Height
- canvas.draw_image(sun_atmosphere_image, [2136/2, 2136/2], [2136, 2136], self.sun_atmosp_center, [550, 550])
- def influence_speed(self):
- global Call_player_1, timer
- #Distance
- dx = math.pow((self.sun_atmosp_center[0]-Call_player_1.circle1_center[0]), 2)
- dy = math.pow((self.sun_atmosp_center[1]-Call_player_1.circle1_center[1]), 2)
- distance = math.sqrt(dx + dy)
- if Call_player_1.circle1_radius+self.sun_atmosp_radius >= distance:
- timer.start()
- self.sun_atmosp_time += 1
- else:
- timer.stop()
- self.sun_atmosp_time = 0
- if Call_player_1.circle1_radius+self.sun_atmosp_radius >= distance:
- #Force and Mass
- Force_Scalar = Call_player_1.circle1_mass * self.sun_atmosp_mass/(distance**2)
- theta = math.atan2 (dy, dx)
- Fx = math.cos(theta) * Force_Scalar
- Fy = math.sin(theta) * Force_Scalar
- #Acceleration
- ax = Fx/Call_player_1.circle1_mass
- ay = Fy/Call_player_1.circle1_mass
- #Velocity
- Call_player_1.velocity_circle1[0] = Call_player_1.velocity_circle1[0] + ax*self.sun_atmosp_time
- Call_player_1.velocity_circle1[1] = Call_player_1.velocity_circle1[1] + ay*self.sun_atmosp_time
- #Final Speed Add
- if Call_player_1.circle1_center[0] > self.sun_atmosp_center[0]:
- Fx = +Fx
- elif Call_player_1.circle1_center[1] > self.sun_atmosp_center[1]:
- Fy = +Fy
- elif Call_player_1.circle1_center[0] < self.sun_atmosp_center[0]:
- Fx = -Fx
- elif Call_player_1.circle1_center[1] < self.sun_atmosp_center[1]:
- Fy = -Fy
- class reset_position:
- def reset_button():
- global Call_player_1
- Call_player_1.circle1_center = [100, 800]
- Call_player_1.velocity_circle1 = [0, 0]
- Call_player_1 = player_1([100, 800], 17, [0,0], 4, 10, 6)
- Call_sun_sprite = sun_sprite([Width/2,Height/2], 57)
- Call_sun_atmosphere = sun_atmosphere([Width/2,Height/2], 240, 15, 0)
- Call_reset_position = reset_position
- def draw_sprites(canvas):
- global Call_player_1, Call_sun_sprite, boost_message_color, boost_message
- Call_player_1.Movement()
- Call_player_1.Border_collisions()
- #Draw Layer 1
- Call_sun_atmosphere.draw_atmosphere(canvas)
- Call_sun_atmosphere.influence_speed()
- #Draw Layer 2
- Call_sun_sprite.draw_sun(canvas)
- Call_sun_sprite.sun_orbit_collisions()
- #Draw Layer 3
- Call_player_1.draw_player_1_sprite(canvas)
- #Draw Layer 4
- canvas.draw_text("Time spent in atmosphere:" + " " + str(Call_sun_atmosphere.sun_atmosp_time), [10,25], 18, "White")
- canvas.draw_text(boost_message, [10,70], 14, boost_message_color)
- def keydown(key):
- global Call_player_1
- Call_player_1.keydown1(key)
- def keyup(key):
- global Call_player_1
- Call_player_1.keyup1(key)
- frame = simplegui.create_frame("Circle Orbital Simulation", Width, Height)
- frame.set_draw_handler(draw_sprites)
- frame.set_keydown_handler(keydown)
- frame.set_keyup_handler(keyup)
- frame.add_button("Reset Position of Player", Call_reset_position.reset_button, 200)
- timer = simplegui.create_timer(100, Call_sun_atmosphere.influence_speed)
- frame.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement