Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Change everything, Part 3

  • Loading branch information...
commit 21cf04fae93432cb8cfa38661ae1fa58bf3961eb 1 parent 916edcf
@kalgynirae kalgynirae authored
View
28 experimenting.py
@@ -23,6 +23,12 @@ class Boost(BoostTile):
texture_active = pygame.image.load('sprites/boost_active.png')
texture_inactive = pygame.image.load('sprites/boost_inactive.png')
+# setup pygame window
+pygame.init()
+screen = pygame.display.set_mode((level.width, level.height))
+pygame.display.set_caption("Test stuff")
+
+# Continuously generate test levels and shoot the ball across them
while True:
# Create a level of 6x6 random tiles
N = 8
@@ -33,17 +39,9 @@ class RandomLevel(Level):
height = 64 * N
level = RandomLevel()
- # Load the ball texture
+ # Create a ball
ball = Ball(sprite=pygame.image.load('sprites/ball-12x12.png'))
- # setup pygame window
- pygame.init()
- screen = pygame.display.set_mode((level.width, level.height))
- pygame.display.set_caption("Test stuff")
- background = pygame.Surface(screen.get_size()).convert()
-
- level.draw(background)
-
# Make physics
god = God(level)
god.watch(ball)
@@ -53,6 +51,10 @@ class RandomLevel(Level):
clock = pygame.time.Clock()
while True:
+ # Draw
+ level.draw(screen)
+ god.draw(screen)
+ pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
@@ -67,14 +69,8 @@ class RandomLevel(Level):
except IndexError:
print("Ball out of bounds!")
break
- #print ball.position
# Stop if ball stops
if ball.velocity.magnitude < 0.05:
print("Ball stopped moving!")
break
- # Update screen
- level.draw(background)
- screen.blit(background, dest=(0, 0))
- god.draw(screen)
- pygame.display.flip()
-
+ del level, ball, god, clock
View
21 golfram/ball.py
@@ -1,23 +1,20 @@
import pygame
-from golfram.geometry import Vector
+from golfram.geometry import Circle, Rectangle, Vector
-class Ball:
+class GolfBall:
- DEFAULT_MASS = 0.0459 # kg
- DEFAULT_DIAMETER = 0.0427 # m
- DEFAULT_SIZE = 8 # px
+ diameter = 0.0427
+ mass = 0.0459
+ shape = Circle(diameter=diameter)
+ sprite = pygame.image.load('sprites/ball-12x12.png')
- def __init__(self, sprite=None, position=None, mass=DEFAULT_MASS,
- diameter=DEFAULT_DIAMETER):
- if not sprite:
- sprite = pygame.Surface((DEFAULT_SIZE, DEFAULT_SIZE))
- self.sprite = sprite
+ def __init__(self, position=None, velocity=None):
if not position:
position = Vector(0, 0)
+ if not velocity:
+ velocity = Vector(0, 0)
self.position = position
- self.mass = float(mass)
- self.diameter = float(diameter)
self.velocity = Vector(0, 0)
View
10 golfram/geometry.py
@@ -5,7 +5,13 @@ class Rectangle:
__slots__ = ('height', 'nw', 'se', 'width')
- def __init__(self, width=None, height=None, nw=None, se=None):
+ def __init__(self, width=None, height=None, offset=None)
+ """Create a Rectangle
+
+ offset is a Vector describing the position of the rectangle's top left
+ corner relative to the origin.
+
+ """
self.nw = nw
self.se = se
if width is not None and height is not None:
@@ -97,7 +103,7 @@ def normalize(self):
"""Return a unit vector in the direction of self"""
return self / self.magnitude
- def projection(self, other):
+ def project(self, other):
"""Calculates the vector projection onto other"""
joseph = other.normalize()
return self * joseph * joseph
View
2  golfram/level.py
@@ -138,7 +138,7 @@ def acceleration_on_object(self, object):
self.active += 1 # A hack, for now
# This calculation is still wrong... the velocity should ramp toward
# the target velocity
- velocity_projection = object.velocity.projection(self.boost_velocity)
+ velocity_projection = object.velocity.project(self.boost_velocity)
dv = self.boost_velocity - velocity_projection
object.velocity += dv / 60
return friction
Please sign in to comment.
Something went wrong with that request. Please try again.