Sign in to follow this  
  • entries
    108
  • comments
    319
  • views
    137709

Pong!

Sign in to follow this  

370 views

One of the first graphical video games was Pong, so I decided to implement it using Boo and BooGame:


BooPlayer Pong


The source is quite easy to understand if you take it one line at a time:


// Pong.boo
// A Pong implementation using BooGame
// To run, drag ontop of BooPlayer, or run BooPlayer BooPlayerPong.boo
// By: Rob Loach (http://www.robloach.net)

import BooGame
import System.Drawing

public class Pong:
static font = BooGame.Font(FontFamily.GenericMonospace, 30, FontStyle.Bold)
static player1score = 0
static player2score = 0

static player1 = RectangleF(20, Display.Height / 2f - 25,10,100)
static player2 = RectangleF(Display.Width - 30, Display.Height / 2f - 25, 10, 100)
static ball = Circle(Display.Width / 2f, Display.Height / 2f, 15)

static ballSpeed = Vector(250, -350)
static originalBallSpeed = ballSpeed.Length
static playerSpeed = 300

static def Draw():
// Draw the score and players and the ball
font.Print(player1score + ' : ' + player2score, Color.White, Display.Width / 2f, 0f, ContentAlignment.TopCenter)
Paint.FilledRectangle(player1, Color.White)
Paint.FilledRectangle(player2, Color.White)
Paint.FilledCircle(ball, Color.White)

static def Update():
// Move the ball
ball.X += ballSpeed.X * Timer.DeltaSeconds
ball.Y += ballSpeed.Y * Timer.DeltaSeconds

// Move the players
if Keyboard.IsKeyDown(Keys.UpArrow):
player1.Y -= playerSpeed * Timer.DeltaSeconds
elif Keyboard.IsKeyDown(Keys.DownArrow):
player1.Y += playerSpeed * Timer.DeltaSeconds

if ballSpeed.X > 0:
if ball.Y < player2.Y + player2.Height / 2f:
player2.Y -= playerSpeed * Timer.DeltaSeconds
elif ball.Y > player2.Y + player2.Height / 2f:
player2.Y += playerSpeed * Timer.DeltaSeconds

// Bound players and ball on screen
if player1.Y < 0:
player1.Y = 0
player1Speed = 0
if player1.Y + player1.Height > Display.Height:
player1.Y = Display.Height - player1.Height
player1Speed = 0
if player2.Y < 0:
player2.Y = 0
player2Speed = 0
if player2.Y + player2.Height > Display.Height:
player2.Y = Display.Height - player2.Height
player2Speed = 0

if ball.Top <= 0 and ballSpeed.Y < 0:
ballSpeed = ballSpeed.Reflection(90)
if ball.Bottom >= Display.Height and ballSpeed.Y > 0:
ballSpeed = ballSpeed.Reflection(90)
if ball.Right < 0:
player1score++
ball.Location = PointF(Display.Width / 2f, Display.Height / 2f)
ballSpeed.X *= -1
ballSpeed.Length = originalBallSpeed
elif ball.Left > Display.Width:
player2score++
ball.Location = PointF(Display.Width / 2f, Display.Height / 2f)
ballSpeed.X *= -1
ballSpeed.Length = originalBallSpeed

// Check collisions
if ballSpeed.X > 0:
if ball.Rectangle.IntersectsWith(player2):
ballSpeed = ballSpeed.Reflection(0)
ballSpeed.Length += 15f
elif ballSpeed.X < 0:
if ball.Rectangle.IntersectsWith(player1):
ballSpeed = ballSpeed.Reflection(0)
ballSpeed.Length += 15f

static def KeyDown(obj, e as KeyboardEventArgs):
if e.Key == Keys.Escape:
Core.QuitApplication()

static def Main():
Display.WindowCaption = 'BooGame - Pong!'
Display.ClearColor = Color.Black
Core.Draw += Draw
Core.Update += Update
Keyboard.KeyDown += KeyDown



Wow, the Python syntax highlighting doesn't work with Boo at all. Anyway, all you have to do to run it is to put it into a pong.boo file, and then drag the file onto BooPlayer.exe. It would be neat to have an online database of these BooPlayer scripts of which users could connect to and run the scripts remotely. That's for later though, after 4E5 [wink].

The hard thing about 4E5 is getting the graphics. I wish I had programmer art skillz, yo....

Random Interest


Dark Materia Star Trek Music
Sign in to follow this  


6 Comments


Recommended Comments

After 4E5 I'm going to have to look into Boo, it looks really good.

What kind of game are you making for 4E5?

Share this comment


Link to comment
I don't think "programmer art skillz" really exist, anyone can draw programmer art. I think it's more of an acceptance thing. I for one could not release something if I didn't enjoy the graphics, they don't have to look like the latest crap, but they should be decent. Stompy however seems not to really care, which looks like a good trait considering his progress.

Share this comment


Link to comment
Heh, my ego is too big 'n bloated to release screenshots with programmer art, like Stompy. [grin]

Share this comment


Link to comment
Quote:
It would be neat to have an online database of these BooPlayer scripts of which users could connect to and run the scripts remotely. That's for later though, after 4E5 .


Check out http://balloon.hobix.com/ Same idea for ruby scripts up and running.

Share this comment


Link to comment
Quote:
Original post by MustEatYemen
Quote:
It would be neat to have an online database of these BooPlayer scripts of which users could connect to and run the scripts remotely. That's for later though, after 4E5 .
Check out http://balloon.hobix.com/ Same idea for ruby scripts up and running.
Very cool, I'll have to find out some server architechure for a Boo/PHP system.

Quote:
Original post by Programmer16
What kind of game are you making for 4E5?
A 2D platformer where you star as a Fabio-esque character, having "adventures" in Venice. [wink]

.... Well, that's what I hope to be working on.

Quote:
Original post by Programmer16
After 4E5 I'm going to have to look into Boo, it looks really good.
I like it a lot as it interfaces nicely with the CLI and adds some functionality which really saves you typing time, especially when it comes to arrays and collections. It also has something called "duck" typing, which is somewhat like the notion of generics, except it works with .NET 1.1, which is neat.

Share this comment


Link to comment
Your Pong game using Boo brings back many fond memories of my learning of the XBox hardware and XDK. First game I made for the XBox was Pong. Or as I liked to call it: PongX: Revenge of the Pong.

There's nothing like playing Pong on a 60" widescreen plasma display with surround sound. Truly.

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now