1. NodeBox 1
    1. Homepage
    2. NodeBox 3Node-based app for generative design and data visualization
    3. NodeBox OpenGLHardware-accelerated cross-platform graphics library
    4. NodeBox 1Generate 2D visuals using Python code (Mac OS X only)
  2. Gallery
  3. Documentation
  4. Forum
  5. Blog

Extremely Simple Solitaire Pong

Posted by Bill Mill on Mar 18, 2008

from math import cos, sin, radians
 
size(400,400)
speed(40)
 
ball_diameter = 20
paddle_size = 75
 
def setup():
    global velocity, pos, gravity, accel, angle, points, bounce
    velocity = (4, 0)
    accel = (0, 0)
    pos = (10, 10)
    gravity = 3
    angle = 0
    bounce = .8
    points = []
 
def draw():
    global velocity, pos, gravity, accel, angle, points, bounce
    a_x, a_y = accel
    v_x, v_y = velocity
    
    a_x = a_x * cos(angle)
    a_y = (a_y * sin(angle)) + gravity
    accel = (a_x, a_y)
    
    v_x += a_x
    v_y += a_y
    velocity = (v_x, v_y)
    
    if pos[1] + v_y + ball_diameter > HEIGHT \
    and MOUSEX < pos[0] < MOUSEX + paddle_size:
        v_y = -v_y * bounce
        velocity = (v_x, v_y)
    if pos[1] + v_y > HEIGHT + 20:
        text("Game Over", HEIGHT / 2, WIDTH / 2)
    elif pos[0] + v_x + ball_diameter > WIDTH:
        v_x = -v_x
        velocity = (v_x, v_y)
    elif pos[0] + v_x < 0:
        v_x = -v_x
        velocity = (v_x, v_y)
        
    if MOUSEX + paddle_size > WIDTH: x = WIDTH - paddle_size
    elif MOUSEX < 0:                 x = 0
    else:                            x = MOUSEX
    rect(x, HEIGHT-4, paddle_size, 4, roundness=2)
 
    pos = (pos[0] + v_x, pos[1] + v_y)
    oval(pos[0], pos[1], ball_diameter, ball_diameter)


 
Posted by Bill Mill on Mar 18, 2008

mostly just intended as a demo of simple drawing and interaction.



Posted by Bill Mill on Mar 18, 2008

sigh... and cos(angle) and sin(angle) should be cos(radians(angle)) and sin(radians(angle)), respectively.