Skip to content
This repository was archived by the owner on Oct 22, 2022. It is now read-only.

Commit

Permalink
added object cycling
Browse files Browse the repository at this point in the history
  • Loading branch information
robertfw committed Nov 22, 2012
1 parent aa5f4ad commit 1a90c35
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 43 deletions.
6 changes: 4 additions & 2 deletions engine/controls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ def set_repeat(self, delay, interval):
pygame.key.set_repeat(delay, interval)

def handle(self, event):
callback = self.bindings.get(event.key).get(event.type)
if callback:
try:
callback = self.bindings[event.key][event.type]
callback()
except KeyError:
pass


class MouseController(object):
Expand Down
12 changes: 6 additions & 6 deletions engine/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Map2D(object):

def add_object(self, obj, pos):
key = id(obj)

#TODO: is adding object attributes like this this bad form?
#(it sure is nice to be able to just do it!)
obj.map = self
Expand Down Expand Up @@ -62,7 +62,7 @@ class Map2DWindow(Window):
_pan_vector = (0, 0) # describes movement of the center
delta_zoom = 0 # describes movement in zoom
_viewable_objects = {} # stores objects/interpolated positions in between updates

def __init__(self, *args, **kwargs):
super(Map2DWindow, self).__init__(*args, **kwargs)

Expand Down Expand Up @@ -167,7 +167,7 @@ def get_sprite_map(self, interpolation):

for pos in self._viewable_objects:
obj = self._viewable_objects[pos]

#ask for forgiveness, not for permission
try:
sprite = obj.get_sprite(display_scale)
Expand All @@ -179,12 +179,12 @@ def get_sprite_map(self, interpolation):
#we need to fill in any layers behind us
for i in range(len(layers), sprite.layer + 1):
layers.append({})

layers[sprite.layer][pos] = sprite
except AttributeError:
# thrown when the object doesn't have a sprite. don't draw it
pass

return layers

def get_objects(self):
Expand All @@ -197,7 +197,7 @@ def get_objects(self):
objects = {}
for pos in raw:
objects[self.convert_world_to_screen(pos)] = raw[pos]

return objects

@property
Expand Down
12 changes: 10 additions & 2 deletions game/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def set_player_heading_from_mouse(event, window, player):
player_screen = window.convert_world_to_screen(player.get_position())
x = event.pos[0] - player_screen[0]
y = event.pos[1] - player_screen[1]

theta_rad = math.atan2(y, x)
degrees = math.degrees(theta_rad)

Expand All @@ -63,7 +63,7 @@ def set_player_heading_from_mouse(event, window, player):

if heading == 360:
heading = 0

heading = round(heading)

player.order_heading(heading)
Expand All @@ -84,3 +84,11 @@ def set_map_center_from_mouse_click(event, window, unlock_if_locked=True):
window.unlock_center()

window.center = window.convert_screen_to_world(event.pos)


def cycle_galaxy_index(galaxy_window):
galaxy_window.index_pointer += 1
if galaxy_window.index_pointer >= len(galaxy_window.index_cycle):
galaxy_window.index_pointer = 0

galaxy_window.lock_center(galaxy_window.index_cycle[galaxy_window.index_pointer])
14 changes: 9 additions & 5 deletions game/galaxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,23 @@ def register(self, func):
def create(self):
galaxy = Galaxy()
for system_generator in self.systems:
system_generator(galaxy)
system_index = system_generator(galaxy)
if type(system_index) == dict:
galaxy.index.update(system_index)

return galaxy


@memoize
def generate_sphere_sprite(radius, color, scale, layer=None):
scaled_radius = int(round(radius * scale))
if scaled_radius <= 0:
scaled_radius = 1

sprite = Sprite()
if layer is not None:
sprite.layer = layer

width = height = scaled_radius * 2
surface = Surface((width, height))
surface.set_colorkey((0, 0, 0))
Expand Down Expand Up @@ -90,7 +92,8 @@ def __init__(self, parent, child, period, radius, angle):

# determine our angular velocity, in degrees per second
self._angular_velocity = 360 / (self.period * 24 * 60 * 60)

#self._angular_velocity = 360 / (self.period * 24)

def update_position(self, dt):
self._cur_angle += self._angular_velocity * dt

Expand All @@ -115,6 +118,7 @@ class Galaxy(object):

map = None
orbits = []
index = {}

def __init__(self):
self.map = Map2D()
Expand Down
7 changes: 4 additions & 3 deletions game/ship.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import math


#TODO: move this to engine
class RigidBody(object):
mass = 10 # in kilograms
vector = None
Expand All @@ -17,7 +18,7 @@ def __init__(self):
def update(self, dt):
self.vector += self.acceleration
cur_pos = self.get_position()

new_pos = (cur_pos[0] + self.vector[0], cur_pos[1] - self.vector[1])

self.map.move_object(self, new_pos)
Expand Down Expand Up @@ -54,15 +55,15 @@ def __init__(self, power=None):
self.power = power

on = False


class Ship(OrientedBody):
size = 100
max_turn_rate = 10
turning_left = False
turning_right = False
_ordered_heading = None

def __init__(self):
OrientedBody.__init__(self)
self.heading = Heading(0)
Expand Down
32 changes: 18 additions & 14 deletions game/systems.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,43 +30,45 @@ def alpha_centauri(galaxy):

@milkyway.register
def sol(galaxy):
sol = Star(radius=1 * SR, color=(255, 250, 230))
index = {}

index['sol'] = sol = Star(radius=1 * SR, color=(255, 250, 230))
galaxy.map.add_object(sol, (0, 0))

#Mercury
mercury = Planet(radius=2439 * KM, color=(200, 200, 200))
index['mercury'] = mercury = Planet(radius=2439 * KM, color=(200, 200, 200))
add_orbiting_object(galaxy, sol, mercury, .46 * AU, 87)

#Venus
venus = Planet(radius=6051 * KM, color=(250, 187, 85))
index['venus'] = venus = Planet(radius=6051 * KM, color=(250, 187, 85))
add_orbiting_object(galaxy, sol, venus, .7 * AU, 224)

#Earth
earth = Planet(radius=6371 * KM, color=(30, 30, 255))
index['earth'] = earth = Planet(radius=6371 * KM, color=(30, 30, 255))
add_orbiting_object(galaxy, sol, earth, 1 * AU, 365)

add_orbiting_object(galaxy, earth, Moon(radius=1700 * KM), 384405 * KM, 29)

#Mars
mars = Planet(radius=3390 * KM, color=(255, 20, 20))
index['mars'] = mars = Planet(radius=3390 * KM, color=(255, 20, 20))
add_orbiting_object(galaxy, sol, mars, 1.6 * AU, 686)

add_orbiting_object(galaxy, mars, Moon(radius=11 * KM), 9377 * KM, .3)
add_orbiting_object(galaxy, mars, Moon(radius=6 * KM), 23460 * KM, 1.2)

#Jupiter
jupiter = Planet(radius=69911 * KM, color=(227, 207, 175))
index['jupiter'] = jupiter = Planet(radius=69911 * KM, color=(227, 207, 175))
jupiter.radius = 69911 * KM
add_orbiting_object(galaxy, sol, jupiter, 5.2 * AU, 4332)
add_orbiting_object(galaxy, jupiter, Moon(radius=1800 * KM), 421700 * KM, 1.7)
add_orbiting_object(galaxy, jupiter, Moon(radius=1550 * KM), 671034 * KM, 3.55)
add_orbiting_object(galaxy, jupiter, Moon(radius=2600 * KM), 1070412 * KM, 7.15)
add_orbiting_object(galaxy, jupiter, Moon(radius=2400 * KM), 1882709 * KM, 16.69)

#Saturn
saturn = Planet(radius=58232 * KM, color=(255, 204, 51))
index['saturn'] = saturn = Planet(radius=58232 * KM, color=(255, 204, 51))
add_orbiting_object(galaxy, sol, saturn, 10 * AU, 10759)

add_orbiting_object(galaxy, saturn, Moon(radius=198 * KM), 185000 * KM, 0.9)
add_orbiting_object(galaxy, saturn, Moon(radius=252 * KM), 238000 * KM, 1.4)
add_orbiting_object(galaxy, saturn, Moon(radius=531 * KM), 295000 * KM, 1.9)
Expand All @@ -78,27 +80,29 @@ def sol(galaxy):
inner_period = 200
outer_period = 250

add_asteroid_belt(galaxy, 200, saturn, 66900 * KM, 74510 * KM, inner_period, outer_period, 1, 1, (105, 102, 51))
add_asteroid_belt(galaxy, 500, saturn, 74658 * KM, 92000 * KM, inner_period, outer_period, 1, 1, (105, 102, 51))
add_asteroid_belt(galaxy, 200, saturn, 66900 * KM, 74510 * KM, inner_period, outer_period, 1, 1, (120, 102, 51))
add_asteroid_belt(galaxy, 500, saturn, 74658 * KM, 92000 * KM, inner_period, outer_period, 1, 1, (105, 102, 91))
add_asteroid_belt(galaxy, 700, saturn, 92000 * KM, 117580 * KM, inner_period, outer_period, 1, 1, (105, 102, 51))
add_asteroid_belt(galaxy, 1000, saturn, 122170 * KM, 136775 * KM, inner_period, outer_period, 1, 1, (105, 102, 51))
add_asteroid_belt(galaxy, 700, saturn, 122170 * KM, 136775 * KM, inner_period, outer_period, 1, 1, (105, 80, 51))

#Uranus
uranus = Planet()
index['uranus'] = uranus = Planet()
uranus.radius = 25362 * KM
add_orbiting_object(galaxy, sol, uranus, 19 * AU, 30799)
add_orbiting_object(galaxy, uranus, Moon(), .25 * AU, 30)
add_orbiting_object(galaxy, uranus, Moon(), .35 * AU, 65)

#Neptune
neptune = Planet()
index['neptune'] = neptune = Planet()
neptune.radius = 24622 * KM
add_orbiting_object(galaxy, sol, neptune, 30 * AU, 60190)
add_orbiting_object(galaxy, neptune, Moon(), .25 * AU, 30)

#the asteroid belt
add_asteroid_belt(galaxy, 1000, sol, 2.3 * AU, 3.2 * AU, 700, 2000, min_radius=10, max_radius=1000)

return index


def add_asteroid_belt(galaxy,
number,
Expand Down
26 changes: 15 additions & 11 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import division
from engine.core import GameCore
from engine.widgets import TextWidget
from pygame.locals import K_ESCAPE, K_w, K_a, K_s, K_d, MOUSEMOTION, MOUSEBUTTONDOWN, K_SPACE, KEYDOWN, KEYUP
from pygame.locals import K_ESCAPE, K_w, K_a, K_s, K_d, K_RETURN, MOUSEMOTION, MOUSEBUTTONDOWN, K_SPACE, KEYDOWN, KEYUP
from engine.map import Map2DWindow
from game import commands
from game.units import AU
Expand All @@ -16,21 +16,22 @@ def __init__(self, *args, **kwargs):
super(Game, self).__init__(*args, **kwargs)

#create a new window, make it the full size of our current display
#galaxy = milkyway.create()
from game.galaxy import Galaxy
galaxy = Galaxy()
galaxy = milkyway.create()
galaxy_window = Map2DWindow(map2d=galaxy.map, rect=((0, 0), self.display.resolution), game=self)
self.windows.add_window(galaxy_window)
self.register_update_callback(galaxy.update)
self.register_pre_render_callback(galaxy_window.draw_grid)
#self.register_pre_render_callback(galaxy_window.draw_grid)

#add a ship and center the map on it
player = Ship()
#galaxy.map.add_object(player, (1 * AU, 1 * AU))
galaxy.map.add_object(player, (0, 0))
galaxy.map.add_object(player, (1 * AU, 1 * AU))
self.register_update_callback(player.update)
galaxy_window.lock_center(player)
galaxy_window.scale = 2
#galaxy_window.lock_center(player)
galaxy_window.scale = 0.000001

galaxy_window.index_cycle = galaxy.index.values()
galaxy_window.index_pointer = 0
galaxy_window.lock_center(galaxy_window.index_cycle[galaxy_window.index_pointer])

#TODO: figure out a better way to pass context items like player or galaxy_window to commands
#so that they have the same method signature
Expand All @@ -56,15 +57,18 @@ def __init__(self, *args, **kwargs):
},
K_ESCAPE: {
KEYDOWN: commands.quit
},
K_RETURN: {
KEYDOWN: lambda: commands.cycle_galaxy_index(galaxy_window)
}
}

self.mouse.bindings = {
MOUSEMOTION: lambda event: commands.set_player_heading_from_mouse(event, galaxy_window, player),
MOUSEBUTTONDOWN: {
1: lambda event: commands.set_map_center_from_mouse_click(event, galaxy_window),
4: lambda event: commands.zoom_map_in(event, galaxy_window, 2),
5: lambda event: commands.zoom_map_out(event, galaxy_window, 2)
4: lambda event: commands.zoom_map_in(event, galaxy_window, 1.1),
5: lambda event: commands.zoom_map_out(event, galaxy_window, 1.1)
}
}

Expand Down

0 comments on commit 1a90c35

Please sign in to comment.