blob: 5b444b60121c710c056be6dc274e6ce17efa708d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
import pygame
class UpDownBox(pygame.sprite.Sprite):
def __init__(self, imagesList, initial_position):
pygame.sprite.Sprite.__init__(self)
self.images = imagesList
self.listLen = len(imagesList)
self.listPos = 0
self.image = imagesList[self.listPos]
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
if self.listPos < self.listLen - 1:
self.listPos += 1
else:
self.listPos = 0
self.image = self.images[self.listPos]
self.next_update_time = current_time
|