Friday, May 17, 2013

My first Python game HOORAY!!

Below is a video of me playing against myself -lol- I suck at playing pong....

Loved making my first little game in python, though.
Most fun part was creating the simple reflection and collision we needed for the ball. I use some vector math and an if statement. There was also the fact that the paddles cant go off screen, and they needed to only move when you hit the correct keys on the keyboard.

The math looked something like this.....

For movement:

Math:
p(t+1) = p(t) +(1)(v(t))

p = position
v = vector
t = time

Python:
New pos      Init pos
p[0] =     p[0] + v[0]
p[1] =     p[1] + v[1]

ball_pos[0] += vel[0]
ball_pos[1] += vel[1]

*add a negative value on the horizontal vector to make the reflection off of paddle.*

ball_pos[0] += vel[0]
ball_pos[1] -= vel[1]

and then to get distance between two points:

Python:
dist(p, q)
return math.sqrt((p[0]-q[0])**2+(p[1]-q[1])**2)

then you pretty much just say if point 0 is in the same range as point 1, point 0 either reflect off of the pad or hits the wall.

I used a local acceleration variable when the player hits a key(up or down)