Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Post merging of files

  • Loading branch information...
commit 8b02cbb77402b938b5a3d38017cf4a41163ebe82 2 parents a631b4a + cca842f
@Ropes Ropes authored
View
81 b2hello.py
@@ -0,0 +1,81 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+"""
+This is a simple example of building and running a simulation
+using Box2D. Here we create a large ground box and a small dynamic box.
+
+NOTE:
+There is no graphical output for this simple example, only text.
+"""
+
+from Box2D import *
+
+# Define the size of the world. Simulation will still work
+# if bodies reach the end of the world, but it will be slower.
+worldAABB=b2AABB()
+worldAABB.lowerBound = (-100, -100)
+worldAABB.upperBound = ( 100, 100)
+
+# Define the gravity vector.
+gravity = b2Vec2(0, -10)
+
+# Do we want to let bodies sleep?
+doSleep = True
+
+# Construct a world object, which will hold and simulate the rigid bodies.
+world = b2World(worldAABB, gravity, doSleep)
+
+# Define the ground body.
+groundBodyDef = b2BodyDef()
+groundBodyDef.position = [0, -10]
+
+# Call the body factory which allocates memory for the ground body
+# from a pool and creates the ground box shape (also from a pool).
+# The body is also added to the world.
+groundBody = world.CreateBody(groundBodyDef)
+
+# Define the ground box shape.
+groundShapeDef = b2PolygonDef()
+
+# The extents are the half-widths of the box.
+groundShapeDef.SetAsBox(50, 10)
+
+# Add the ground shape to the ground body.
+groundBody.CreateShape(groundShapeDef)
+
+# Define the dynamic body. We set its position and call the body factory.
+bodyDef = b2BodyDef()
+bodyDef.position = (0, 4)
+body = world.CreateBody(bodyDef)
+
+# Define another box shape for our dynamic body.
+shapeDef = b2PolygonDef()
+shapeDef.SetAsBox(1, 1)
+
+# Set the box density to be non-zero, so it will be dynamic.
+shapeDef.density = 1
+
+# Override the default friction.
+shapeDef.friction = 0.3
+
+# Add the shape to the body.
+shape=body.CreateShape(shapeDef)
+
+# Now tell the dynamic body to compute it's mass properties base on its shape.
+body.SetMassFromShapes()
+
+# Prepare for simulation. Typically we use a time step of 1/60 of a
+# second (60Hz) and 10 velocity/8 position iterations. This provides a
+# high quality simulation in most game scenarios.
+timeStep = 1.0 / 60
+vel_iters, pos_iters = 10, 8
+
+# This is our little game loop.
+for i in range(60):
+ # Instruct the world to perform a single step of simulation. It is
+ # generally best to keep the time step and iterations fixed.
+ world.Step(timeStep, vel_iters, pos_iters)
+
+ # Now print the position and angle of the body.
+ print body.position, body.angle
+
View
14 common.py
@@ -1,12 +1,15 @@
import pygame, Box2D
import math
-import entropy
-from game_state import *
from Box2D import *
-# class vec(b2Vec2, list):
-# def tup(self):
-# return (self.x, self.y)
+vec = b2Vec2
+
+import physics
+from physics import world
+
+def toScreen(v):
+ import game_state
+ return game_state.GameState.current.toScreen(v)
vec = b2Vec2
@@ -15,7 +18,6 @@ def __init__(self):
#Load all media needed(images, animations, sounds, etc)
self.test = load_img('test.png')
-
'''
Returns the image surface resource only
'''
View
8 debugdraw.py
@@ -1,5 +1,7 @@
import Box2D as box2d
import pygame
+from game_state import GameState
+from Box2D import b2Vec2
class DebugDraw(box2d.b2DebugDraw):
"""
@@ -100,7 +102,8 @@ def DrawSolidCircle(self, center_v, radius, axis, color):
if radius < 1: radius = 1
else: radius = int(radius)
- center = self.toScreen(center_v)
+ center = self.toScreen(b2Vec2(center_v))
+ print center
pygame.draw.circle(self.surface, (color[0]/2, color[1]/2, color[1]/2, 127), center, radius, 0)
pygame.draw.circle(self.surface, color, center, radius, 1)
@@ -131,7 +134,8 @@ def toScreen(self, pt):
Input: (x, y) - a tuple in world coordinates
Output: (x, y) - a tuple in screen coordinates
"""
- return ((pt[0] * self.viewZoom) - self.viewOffset.x, self.height - ((pt[1] * self.viewZoom) - self.viewOffset.y))
+ return GameState.current.toScreen(pt)
+ # return ((pt[0] * self.viewZoom) - self.viewOffset.x, self.height - ((pt[1] * self.viewZoom) - self.viewOffset.y))
def scaleValue(self, value):
"""
Input: value - unscaled value
View
28 entropy.py
@@ -1,11 +1,12 @@
import os, sys, pygame
from Box2D import *
from pygame.locals import *
-from debugdraw import DebugDraw
-import units
+from common import *
from game_state import *
+import units
class Game:
+
def __init__(self):
pygame.init()
@@ -18,28 +19,13 @@ def __init__(self):
self.backgd = pygame.Surface(self.screen.get_size())
back_color = [128,128,128]
+ physics.b2draw.surface = self.screen
+
self.frames = 0
self.show = pygame.sprite.RenderClear()
GameState.current = PlayState()
- ### BOX2D STUFF ###
- worldAABB=b2AABB()
- worldAABB.lowerBound = (-100, -100)
- worldAABB.upperBound = ( 100, 100)
- gravity = b2Vec2(0, 0)
- doSleep = False
- self.world = b2World(worldAABB, gravity, doSleep)
- self.timeStep = 1.0 / 60
- self.vel_iters, self.pos_iters = 10, 8
-
- self.b2draw = DebugDraw()
- self.b2draw.SetFlags(self.b2draw.e_shapeBit) # and whatever else you want it to draw
- self.b2draw.viewZoom = 1.
- self.b2draw.viewCenter = b2Vec2(0,0)
- self.b2draw.viewOffset = b2Vec2(-200,50)
- self.world.SetDebugDraw(self.b2draw)
-
def run_loop(self):
black = [0,0,0]
@@ -48,7 +34,7 @@ def run_loop(self):
# home_sprites = pygame.sprite.RenderPlain(home)
def update():
- self.world.Step(self.timeStep, self.vel_iters, self.pos_iters)
+ physics.worldStep()
def draw():
home.draw(self.screen)
@@ -66,6 +52,8 @@ def draw():
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
return
+ if event.key == K_LEFT:
+ GameState.current.scroll.x -= 1
update()
draw()
View
8 game_state.py
@@ -1,6 +1,5 @@
import pygame, Box2D
import math
-import entropy
from common import *
class GameState(object):
@@ -17,6 +16,11 @@ def update(self):
def draw(self):
pass
+ def toScreen(self, v):
+ p = v - self.scroll
+ return (int(p.x), int(p.y))
+
+
class PlayState(GameState):
def __init__(self):
@@ -31,5 +35,3 @@ def draw(self):
pass
-def state():
- return GameState.current
View
38 physics.py
@@ -0,0 +1,38 @@
+
+from Box2D import *
+from debugdraw import *
+### BOX2D STUFF ###
+worldAABB=b2AABB()
+worldAABB.lowerBound = (-100, -100)
+worldAABB.upperBound = ( 100, 100)
+gravity = b2Vec2(0, 0)
+doSleep = False
+
+world = b2World(worldAABB, gravity, doSleep)
+timeStep = 1.0 / 60
+vel_iters, pos_iters = 10, 8
+
+b2draw = DebugDraw()
+b2draw.SetFlags(b2draw.e_shapeBit)
+b2draw.viewZoom = 1.
+b2draw.viewCenter = b2Vec2(0,0)
+b2draw.viewOffset = b2Vec2(-200,50)
+world.SetDebugDraw(b2draw)
+
+def worldStep():
+ global world
+ world.Step(timeStep, vel_iters, pos_iters)
+
+def home_body(radius):
+ bodyDef = b2BodyDef()
+ bodyDef.position = (0, 0)
+
+ shapeDef = b2CircleDef()
+ shapeDef.radius = radius
+ shapeDef.density = 1
+ shapeDef.restitution = 0
+ shapeDef.friction = 1
+
+ body = world.CreateBody(bodyDef)
+ body.CreateShape(shapeDef)
+ return body
View
29 units.py
@@ -1,15 +1,13 @@
import pygame, Box2D
import math
-import entropy
-from game_state import GameState
from common import *
+from game_state import GameState
class Unit(object):
@property
def screenCoords(self):
- p = self.pos - GameState.current.scroll
- return (int(p.x), int(p.y))
+ return GameState.current.toScreen(self.pos)
class Home(pygame.sprite.Sprite, Unit):
@@ -45,6 +43,7 @@ def __init__(self):
screen = pygame.display.get_surface()
self.rect = None
self.pos = vec(0,0)
+ self.body = physics.home_body(self.radius)
@property
def radius(self):
@@ -56,10 +55,28 @@ def update(self):
def draw(self, screen):
ent.draw()
- pygame.draw.circle(screen, [255,255,0], self.screenCoords, int(self.radius), 0)
+ # pygame.draw.circle(screen, [255,255,0], self.screenCoords, int(self.radius), 0)
+ pass
+
+class Clod(pygame.sprite.Sprite, Unit):
+ def __init__(self, pos, vel, mass):
+ super(Clod, self).__init__()
+ self.mass = mass
+ self.radius = 1
+ self.body = Game.world.CreateDynamicBody(
+ position=pos,
+ fixtures=b2FixtureDef(
+ shape=b2CircleShape(radius=self.radius),
+ density=0.5,
+ restitution=0,
+ friction=0.5
+ ),
+ damping=0
+ )
+ print dir(self.body)
-class Dragon(pygame.sprite.Sprite):
+class Dragon(pygame.sprite.Sprite, Unit):
def __init__(self):
super(Dragon, self).__init__()
self.position = [0, 0]
Please sign in to comment.
Something went wrong with that request. Please try again.