Skip to main content
The moving objects widgets provide animated elements that move across the screen with automatic boundary collision detection.

movingRect

Creates a rectangular object that moves around the screen and bounces off edges.

Constructor

movingRect(surface, pos=vec2d(0,0), size=vec2d(20,20), 
           color=(255,0,0), speed=vec2d(1,0), gravity=0)

Parameters

surface
pygame.Surface
required
The pygame surface to which the rect will draw itself
pos
vec2d
default:"vec2d(0,0)"
The initial position of the rectangle
size
vec2d
default:"vec2d(20,20)"
The width and height of the rectangle
color
tuple
default:"(255,0,0)"
RGB color tuple for the rectangle
speed
vec2d
default:"vec2d(1,0)"
The velocity vector (x, y) determining movement per frame
gravity
float
default:"0"
Gravity parameter (currently unused in implementation)

Usage Example

game.py
self.floater = movingRect(self.screen,
    pos=vec2d(self.SCREEN_WIDTH/2, 0),
    speed=vec2d(0,5))

Collision Behavior

The rectangle automatically reverses direction when hitting screen boundaries:
widgets.py
if self.pos.x + self.size.x > self.surfaceSize.x or self.pos.x < 0:
    self.speed.x *= -1
if self.pos.y + self.size.y > self.surfaceSize.y or self.pos.y < 0:
    self.speed.y *= -1
The collision check considers the full size of the rectangle, so it bounces when any edge touches the boundary.

movingImg

Creates an image that moves around the screen with boundary collision detection.

Constructor

movingImg(surface, image, pos=vec2d(0,0), speed=vec2d(1,0), gravity=1)

Parameters

surface
pygame.Surface
required
The pygame surface to which the image will draw itself
image
str
required
Path to the image file to load
pos
vec2d
default:"vec2d(0,0)"
The initial position of the image
speed
vec2d
default:"vec2d(1,0)"
The velocity vector (x, y) determining movement per frame
gravity
float
default:"1"
Gravity parameter (currently unused in implementation)

Usage Example

game.py
self.moveImg = movingImg(self.screen,
    "images/toggle1.png",
    pos=vec2d(0,self.SCREEN_HEIGHT*3/4),
    speed=vec2d(5, 0))

Collision Behavior

Identical to movingRect - the image bounces off screen edges:
widgets.py
if self.pos.x + self.size.x > self.surfaceSize.x or self.pos.x < 0:
    self.speed.x *= -1
if self.pos.y + self.size.y > self.surfaceSize.y or self.pos.y < 0:
    self.speed.y *= -1
self.pos.x += self.speed.x
self.pos.y += self.speed.y

circles

Creates a simple circular shape that can be drawn on the screen.

Constructor

circles(surface, pos=vec2d(10,10), radius=5, bgcolor=(0,0,0))

Parameters

surface
pygame.Surface
required
The pygame surface to which the circle will draw itself
pos
vec2d
default:"vec2d(10,10)"
The center position of the circle
radius
int
default:"5"
The radius of the circle in pixels
bgcolor
tuple
default:"(0,0,0)"
RGB color tuple for the circle

Usage Example

game.py
self.ball = circles(self.screen,
    pos=vec2d(25,25),
    radius=25)

Drawing

The circle is drawn using pygame’s circle drawing function:
widgets.py
def draw(self):
    pygame.draw.circle(self.surface, self.bgcolor, self.pos, self.radius)
Unlike movingRect and movingImg, the circles widget does not have built-in movement or collision detection. It’s described in the source as a “simple useless circle” for static display.

Common Patterns

Adding to World List

Moving objects are typically added to a world list for batch rendering:
game.py
self.world = [self.button, self.togglebtn, self.clockImg, self.hand, 
              self.textTest, self.moveImg, self.floater, self.ball]

def draw(self):
    self.draw_background()
    
    for obj in self.world:
        obj.draw()

Movement Patterns

  • Horizontal movement: speed=vec2d(5, 0)
  • Vertical movement: speed=vec2d(0, 5)
  • Diagonal movement: speed=vec2d(3, 3)
  • Stationary: speed=vec2d(0, 0)

Automatic Updates

Both movingRect and movingImg update their position automatically in their draw() method:
widgets.py
self.pos.x += self.speed.x
self.pos.y += self.speed.y
self.rect = Rect(self.pos.x, self.pos.y, self.size.x, self.size.y)
pygame.draw.rect(self.surface, self.color, self.rect)
To create bouncing diagonal movement, use equal x and y speed values like vec2d(3, 3). The automatic collision detection will handle the bouncing at corners naturally.
The gravity parameter is defined in both movingRect and movingImg constructors but is not currently used in the implementation.
  • Images - For static images without movement
  • Box - For static rectangular containers

Build docs developers (and LLMs) love