Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/data
diff options
context:
space:
mode:
authorChris Ball <cjb@pullcord.laptop.org>2007-12-08 22:26:38 (GMT)
committer Chris Ball <cjb@pullcord.laptop.org>2007-12-08 22:26:38 (GMT)
commita692bb65183234a97aba9a572632c8096302ec5b (patch)
tree8ba67ef78fb5d8688b489956e74f59505a792d0b /data
parentd293c5fabf7d4c22faad1728459e8f59c694d7df (diff)
Add examples from Henry Holtzman: bounce, jump, lines, pong.
Diffstat (limited to 'data')
-rw-r--r--data/graphics/bounce89
-rw-r--r--data/graphics/image52
-rw-r--r--data/graphics/jump9
-rw-r--r--data/graphics/lines105
-rw-r--r--data/graphics/pong151
5 files changed, 399 insertions, 7 deletions
diff --git a/data/graphics/bounce b/data/graphics/bounce
new file mode 100644
index 0000000..94e5c8b
--- /dev/null
+++ b/data/graphics/bounce
@@ -0,0 +1,89 @@
+# bounce: move some text around the screen
+# use the escape key to exit
+#
+# on the XO, the escape key is the top lefthand key,
+# circle with an x in it.
+
+import sys,pygame
+from pygame.locals import *
+from random import *
+
+# always need to init first thing
+pygame.init()
+
+# turn off cursor
+pygame.mouse.set_visible(False)
+
+# XO screen is 1200x900
+size = width, height = 1200, 900
+
+# we'll use 36 pixel high text
+fsize = 36
+
+# vector for motion, will control speed and angle
+mvect = [3,2]
+
+# create the window and keep track of the surface
+# for drawing into
+screen = pygame.display.set_mode(size)
+
+msg = "Hello!"
+
+# create a Font object from a file, or use the default
+# font if the file name is None. size param is height
+# in pixels
+
+# usage: pygame.font.Font(filename|object, size)
+font = pygame.font.Font(None, fsize)
+
+# Font.render draws text onto a new surface.
+#
+# usage: Font.render(text, antialias, color, bg=None)
+text = font.render(msg, True, (10,10,10))
+
+# the Rect object is used for positioning
+textRect = text.get_rect()
+
+# start at the top left
+textRect.left = 0;
+textRect.top = 0;
+
+while 1:
+
+ for event in pygame.event.get():
+ if event.type == QUIT:
+ sys.exit()
+
+ elif event.type == KEYDOWN:
+ if event.key == K_ESCAPE:
+ sys.exit()
+
+ screen.fill((250,250,250))
+
+ # draw the text
+ screen.blit(text, textRect)
+
+ # update the display
+ pygame.display.flip()
+
+ # move the text
+ #
+ # Rect.move returns a new Rect while
+ # Rect.move_ip moves in place, so we'll use
+ # the latter
+ textRect.move_ip(mvect)
+
+ # bounce off edges
+ if textRect.left < 0 :
+ textRect.left = 0
+ mvect[0] = -1 * mvect[0]
+ elif textRect.right >= size[0] :
+ textRect.right = size[0] - 1
+ mvect[0] = -1 * mvect[0]
+
+ if textRect.top < 0 :
+ textRect.top = 0
+ mvect[1] = -1 * mvect[1]
+ elif textRect.bottom >= size[1] :
+ textRect.bottom = size[1] - 1
+ mvect[1] = -1 * mvect[1]
diff --git a/data/graphics/image b/data/graphics/image
new file mode 100644
index 0000000..8ee1796
--- /dev/null
+++ b/data/graphics/image
@@ -0,0 +1,52 @@
+# image: take a picture
+#
+# any keypress to exit
+
+import sys, pygame, gst, time
+from random import *
+
+# XO screen is 1200 by 900
+size = width, height = 1200, 900
+
+# grey background
+bgcolor = (128,128,128)
+
+# grab a frame from camera to file
+pipeline = gst.parse_launch('v4l2src ! ffmpegcolorspace ! jpegenc ! filesink location=/tmp/pippypic.jpg')
+pipeline.set_state(gst.STATE_PLAYING)
+
+# pygame always needs to be initialized as the first call
+pygame.init()
+
+# turn off cursor
+pygame.mouse.set_visible(False)
+
+# create the pygame window at the desired size and return a Surface object for
+# drawing in that window.
+screen = pygame.display.set_mode(size)
+
+time.sleep(1)
+pipeline.set_state(gst.STATE_NULL)
+
+# load in previously grabbed frame
+image = pygame.image.load("/tmp/pippypic.jpg")
+
+angle = 0.0
+scale = 2.0
+
+while 1:
+ for event in pygame.event.get():
+ if event.type == pygame.QUIT: sys.exit()
+ elif event.type == pygame.KEYDOWN: sys.exit()
+
+ newImage = pygame.transform.rotozoom(image, angle,scale)
+ newImageRect = newImage.get_rect()
+ newImageRect.centerx = screen.get_rect().centerx
+ newImageRect.centery = screen.get_rect().centery
+
+ screen.fill(bgcolor)
+ screen.blit(newImage, newImageRect)
+ pygame.display.flip()
+
+ angle = angle + 5.0
+ scale = scale * 0.95
diff --git a/data/graphics/jump b/data/graphics/jump
index db7c347..ed08950 100644
--- a/data/graphics/jump
+++ b/data/graphics/jump
@@ -1,14 +1,10 @@
-# From C. Scott Ananian
-# both of these functions should be in the 'basic' package or some such
def clear_scr():
- print '\x1B[H\x1B[J' # clear screen, the hard way.
+ print '\x1B[H\x1B[J' # clear screen
+
def wait():
import time
time.sleep(0.1)
-# jumping man!
-# having to escape the backslash is rather unfortunate
-# i didn't have to do that in C64 BASIC
for i in xrange(0,50):
clear_scr()
print "\\o/"
@@ -33,4 +29,3 @@ for i in xrange(0,50):
print " | "
print "/ \\"
wait()
-
diff --git a/data/graphics/lines b/data/graphics/lines
new file mode 100644
index 0000000..1885d61
--- /dev/null
+++ b/data/graphics/lines
@@ -0,0 +1,105 @@
+# lines: make lots of lines on the screen, just for fun!
+# use the escape key to exit
+#
+# on the XO, the escape key is the top lefthand key,
+# circle with an x in it.
+
+import sys,pygame
+from pygame.locals import *
+from random import *
+
+# XO screen is 1200x900
+size = width, height = 1200, 900
+
+# always need to init first thing
+pygame.init()
+
+# turn off the cursor
+pygame.mouse.set_visible(False)
+
+# create the window and keep track of the surface
+# for drawing into
+screen = pygame.display.set_mode(size)
+# start the screen all black
+screen.fill((0,0,0))
+
+# we need starting endpoints for the line and seed motion vectors
+start=[randint(0,size[0]), randint(0,size[1])]
+end =[randint(0,size[0]), randint(0,size[1])]
+
+# randomize the motion, 1..3 in each direction, positive or negative, but
+# never 0
+mvect_start = [choice((-1,1)) * randint(1,3), choice((-1,1)) * randint(1,3)]
+mvect_end = [choice((-1,1)) * randint(1,3), choice((-1,1)) * randint(1,3)]
+
+# start with a random color and color direction
+color = [randint(0,255), randint(0,255), randint(0,255)]
+direction = [choice((-1,1)), choice((-1,1)), choice((-1,1))]
+
+while 1:
+
+ for event in pygame.event.get():
+ if event.type == QUIT:
+ sys.exit()
+
+ elif event.type == KEYDOWN:
+ if event.key == K_ESCAPE:
+ sys.exit()
+
+ # draw the line using the current values and width=3
+ pygame.draw.line(screen, color, start, end, 3)
+
+ # update the display
+ pygame.display.flip()
+
+ # update the end points and the color
+ for i in range(2):
+ start[i] = start[i] + mvect_start[i]
+ end[i] = end[i] + mvect_end[i]
+
+ for i in range(3):
+ color[i] = color[i] + direction[i]
+
+ # check if anything has gone out of range and
+ # if so, bring back to edge and reverse the
+ # corresponding motion vector
+ if start[0] < 0 :
+ start[0] = 0
+ mvect_start[0] = -1 * mvect_start[0]
+ elif start[0] >= size[0] :
+ start[0] = size[0] - 1
+ mvect_start[0] = -1 * mvect_start[0]
+
+ if start[1] < 0 :
+ start[1] = 0
+ mvect_start[1] = -1 * mvect_start[1]
+ elif start[1] >= size[1] :
+ start[1] = size[1] - 1
+ mvect_start[1] = -1 * mvect_start[1]
+
+ if end[0] < 0 :
+ end[0] = 0
+ mvect_end[0] = -1 * mvect_end[0]
+ elif end[0] >= size[0] :
+ end[0] = size[0] - 1
+ mvect_end[0] = -1 * mvect_end[0]
+
+ if end[1] < 0 :
+ end[1] = 0
+ mvect_end[1] = -1 * mvect_end[1]
+ elif end[1] >= size[1] :
+ end[1] = size[1] - 1
+ mvect_end[1] = -1 * mvect_end[1]
+
+ for i in range(3) :
+ if color[i] < 0:
+ color[i] = 0
+ direction[i] = direction[i] * -1
+ elif color[i] >= 255 :
+ color[i] = 255
+ direction[i] = direction[i] * -1
+
+ # randomly change the color directon on occasion
+ if randint(0,511) == 128:
+ for i in range(3):
+ direction[i] = choice((-1,1))
diff --git a/data/graphics/pong b/data/graphics/pong
new file mode 100644
index 0000000..351f7ce
--- /dev/null
+++ b/data/graphics/pong
@@ -0,0 +1,151 @@
+# pong: hit the ball with the paddle
+#
+# use the escape key to exit
+#
+# on the XO, the escape key is the top lefthand key,
+# circle with an x in it.
+
+import sys,pygame
+from pygame.locals import *
+from random import *
+
+# always need to init first thing
+pygame.init()
+
+# XO screen is 1200x900
+size = width, height = 1200, 900
+
+# create the window and keep track of the surface
+# for drawing into
+screen = pygame.display.set_mode(size)
+
+# turn off the cursor
+pygame.mouse.set_visible(False)
+
+# turn on key repeating (repeat 40 times per second)
+pygame.key.set_repeat(25,25)
+
+# start the screen all black
+bgcolor = (0,0,0)
+screen.fill(bgcolor)
+
+# paddle constants
+paddle_width = 20
+paddle_length = 100
+paddle_radius = paddle_length / 2
+paddle_color = (250,250,250)
+step = 6 # paddle moves 3 pixels at a go
+
+# ball constants
+ball_color = (250,250,250)
+ball_radius = 25
+
+# game constants
+fsize = 48
+msg = "Press 'g' to start game"
+
+font=pygame.font.Font(None, fsize)
+text = font.render(msg, True, (250,250,250))
+textRect = text.get_rect()
+textRect.centerx = screen.get_rect().centerx
+textRect.centery = screen.get_rect().centery
+
+while 1:
+
+ # display msg
+ screen.fill(bgcolor)
+ screen.blit(text, textRect)
+ pygame.display.flip()
+
+ # chill until a key is pressed
+ for idle_event in pygame.event.get():
+ if idle_event.type == QUIT:
+ sys.exit()
+
+ if idle_event.type == KEYDOWN:
+ if idle_event.key == K_ESCAPE:
+ sys.exit()
+
+ if idle_event.key == 103: # g key
+
+ # play a game!
+
+ # start the paddle in the center
+ paddle_location = height / 2
+
+ # number of balls to a game
+ balls = 4
+
+ while balls > 0:
+
+ ball_position = [ball_radius, ball_radius]
+ ball_mvect = [randint(3,5), randint(3,5)]
+ ball_limit = size
+ balls = balls - 1
+
+ while ball_position[0] + ball_radius < ball_limit[0]: # in play
+
+ for event in pygame.event.get():
+ if event.type == QUIT:
+ sys.exit()
+
+ elif event.type == KEYDOWN:
+ if event.key == K_ESCAPE:
+ sys.exit()
+ elif event.key == 273 \
+ or event.key == 265 \
+ or event.key == 264: # up
+ paddle_location = paddle_location - step
+ elif event.key == 274 \
+ or event.key == 259 \
+ or event.key == 258: # down
+ paddle_location = paddle_location + step
+
+ # make sure the paddle is in-bounds
+ if paddle_location - paddle_radius < 0:
+ paddle_location = paddle_radius
+ elif paddle_location + paddle_radius >= height:
+ paddle_location = height - 1 - paddle_radius
+
+ # clear the screen
+ screen.fill(bgcolor)
+
+ # draw the paddle on the right side of the screen
+ pygame.draw.line(screen,
+ paddle_color,
+ (width - paddle_width, paddle_location -
+ paddle_radius),
+ (width - paddle_width,
+ paddle_location+paddle_radius),
+ paddle_width)
+
+ # draw the ball
+ pygame.draw.circle(screen, ball_color, ball_position, ball_radius)
+
+ # draw the unused balls
+ for i in range(balls):
+ pygame.draw.circle(screen, ball_color, (30+i*ball_radius*2.4, 30),
+ ball_radius)
+
+ # update the display
+ pygame.display.flip()
+
+ # update the ball
+ for i in range(2):
+ ball_position[i] = ball_position[i] + ball_mvect[i]
+
+ # bounce on top and left
+ if ball_position[i] < ball_radius:
+ ball_position[i] = ball_radius
+ ball_mvect[i] = -1 * ball_mvect[i]
+ # bounce on bottom
+ elif i == 1 \
+ and ball_position[i] >= ball_limit[i] - ball_radius:
+ ball_position[i] = ball_limit[i] - ball_radius - 1
+ ball_mvect[i] = -1 * ball_mvect[i]
+ elif i == 0 \
+ and ball_position[i] >= ball_limit[i] - ball_radius - paddle_width \
+ and ball_position[1] > paddle_location - paddle_radius \
+ and ball_position[1] < paddle_location + paddle_radius :
+ ball_position[i] = ball_limit[i] - ball_radius - paddle_width - 1
+ ball_mvect[i] = -1 * ball_mvect[i]