#!/usr/bin/python
import pygame
from pygame.locals import KEYDOWN
width = 320
height = 240
size = [width, height]
pygame.init()
screen = pygame.display.set_mode(size)
background = pygame.Surface(screen.get_size())
b = pygame.sprite.Sprite() # create sprite
b.image = pygame.image.load("ball.png").convert() # load ball image
b.rect = b.image.get_rect() # use image extent values
b.rect.topleft = [0, 0] # put the ball in the top left corner
screen.blit(b.image, b.rect)
pygame.display.update()
while pygame.event.poll().type != KEYDOWN:
pygame.time.delay(100)
#!/usr/bin/python
import pygame
from pygame.locals import KEYDOWN
class BallSprite(pygame.sprite.Sprite):
image = None
def __init__(self, location):
pygame.sprite.Sprite.__init__(self)
if BallSprite.image is None:
# This is the first time this class has been
# instantiated. So, load the image for this and
# all subsequence instances.
BallSprite.image = pygame.image.load("ball.png")
self.image = BallSprite.image
# Make our top-left corner the passed-in location.
self.rect = self.image.get_rect()
self.rect.topleft = location
pygame.init()
screen = pygame.display.set_mode([320, 320])
b = BallSprite([0, 0]) # put the ball in the top left corner
screen.blit(b.image, b.rect)
pygame.display.update()
while pygame.event.poll().type != KEYDOWN:
pygame.time.delay(10)
#!/usr/bin/python
import pygame
from pygame.locals import KEYDOWN
width = 320
height = 240
size = [width, height]
ydir = 1
xdir = 1
xpos = 0
ypos = 0
pygame.init()
screen = pygame.display.set_mode(size)
background = pygame.Surface(screen.get_size())
b = pygame.sprite.Sprite() # create sprite
b.image = pygame.image.load("ball.png").convert() # load image
b.rect = b.image.get_rect() # use image extent values
b.rect.topleft = [xpos, ypos] # put the ball in the top left corner
screen.blit(b.image, b.rect)
slow = 0
def gravity(y):
global height
return 0
return (((height+height/20) * 3) / y)
pygame.display.update()
while pygame.event.poll().type != KEYDOWN:
pygame.time.delay(gravity(ypos))
# If we’re at the top or bottom of the screen,
# switch directions.
if b.rect.bottom>=height:
ydir = -1
elif ypos == 0:
ydir = 1
if xpos == 0:
xdir = 1
elif b.rect.right>=width:
xdir = -1
if slow:
screen.fill([0, 0, 0]) # blank the screen
else:
rectlist = [screen.blit(background, b.rect)]
# Move our position up or down by one pixel
xpos += xdir
ypos += ydir
b.rect.topleft = [xpos, ypos]
if slow:
screen.blit(b.image, b.rect)
pygame.display.update()
else:
rectlist += [screen.blit(b.image, b.rect)]
pygame.display.update(rectlist)
#!/usr/bin/python
import pygame
from pygame.locals import KEYDOWN
class BallSprite(pygame.sprite.Sprite):
image = None
def __init__(self, initial_position):
pygame.sprite.Sprite.__init__(self)
if BallSprite.image is None:
BallSprite.image = pygame.image.load("ball.png")
self.image = BallSprite.image
self.rect = self.image.get_rect()
self.rect.topleft = initial_position
self.going_down = True # Start going downwards
self.next_update_time = 0 # update() hasn’t been called yet.
def update(self, current_time, bottom):
# Update every 10 milliseconds = 1/100th of a second.
if self.next_update_time < current_time:
# If we’re at the top or bottom of the screen, switch directions.
if self.rect.bottom == bottom - 1: self.going_down = False
elif self.rect.top == 0: self.going_down = True
# Move our position up or down by one pixel
if self.going_down: self.rect.top += 1
else: self.rect.top -= 1
self.next_update_time = current_time + 10
pygame.init()
boxes = []
for location in [[0, 0],
[60, 60],
[120, 120]]:
boxes.append(BallSprite(location))
screen = pygame.display.set_mode([150, 150])
while pygame.event.poll().type != KEYDOWN:
screen.fill([0, 0, 0]) # blank the screen.
time = pygame.time.get_ticks()
for b in boxes:
b.update(time, 150)
screen.blit(b.image, b.rect)
pygame.display.update()
This document was produced using groff-1.19.