Browse Source

There, I commented it. :)

main
Jo Jerrica Decker 6 years ago
parent
commit
6ce9628c38
  1. 6
      scroller/char_sheet.py
  2. 4
      scroller/character.py
  3. 6
      scroller/scroller.py
  4. 37
      test.py

6
scroller/char_sheet.py

@ -1,3 +1,7 @@
# Character Sheet object for holding the sprite sheet image file and additional
# data such as character width and height.
# This, of course, only takes into account fixed-width bitmap fonts.
from pygame import image
@ -8,6 +12,8 @@ class CharSheet:
self.char_width = char_width
self.char_height = char_height
# Retrieves the top-left x and y co-ordinates of a character on the sprite
# sheet.
def get_sheet(self, character):
index = self.characters.index(character.char)

4
scroller/character.py

@ -1,3 +1,7 @@
# Character object for managing an individual character and its attributes,
# such as its location, speed and angle.
class Character:
def __init__(self,
x=0,

6
scroller/scroller.py

@ -1,3 +1,6 @@
# Scroller object within which the base text string is stored, and the current
# list of visible characters is maintained and updated.
from . import character
@ -7,6 +10,7 @@ class Scroller:
self.next_char = 0
self.sheet_chars = []
# obtains the next character adds it to the list of characters to render.
def get_next_char(self,
x=0,
y=0,
@ -27,5 +31,7 @@ class Scroller:
else:
self.next_char = 0
# Removes a character from the render list, usually as a result of leaving
# the screen.
def delete_first_char(self):
self.sheet_chars.pop(0)

37
test.py

@ -5,10 +5,18 @@ from scroller import char_sheet
pygame.init()
# For this example, I just wanted to work with a small 320x240 space.
# Most of the old scrollers didn't have a lot of pixels to play with, after
# all.
size = width, height = 320, 240
black = 0, 0, 0
red = 255, 0, 0
# A character list to match up the available ASCII characters with the
# spritesheet. You could probably use ints instead. I used chars because I
# forgot Python treat chars like strings. x_X
characters = [' ', '!', '\"', '#', '$', '%', '&', '\'', '(', ')', '*', '+',
',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8',
'9', ':', ';', '<', '=', '>', '?', '@', 'A', 'B', 'C', 'D', 'E',
@ -18,56 +26,83 @@ characters = [' ', '!', '\"', '#', '$', '%', '&', '\'', '(', ')', '*', '+',
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
'z', '{', '|', '}', '~', '©']
# A CharSheet object holds the spritesheet and the character list, as well as
# the dimensions of each sprite.
char_sheet = char_sheet.CharSheet("jeromBD-metal-CCBY3.png",
characters, 16, 16)
# Placeholder co-ordinates just to centre the scroller while I built this.
x = 320
y = 120
# We need a clock to restrain the framerate and not simply have the scroller
# zipping across before the view even realises the program has started.
clock = pygame.time.Clock()
# Creates a new Scroller object, with the text we want to put into it.
# Some space added to create a gap between repetitions.
scroller = scroller.Scroller("IT'S BEEN... one week since you looked at me, " +
"cocked your head to the side and said \"I'm " +
"angry\" ")
# Opens a pygame window to the previously-specified dimentions.
screen = pygame.display.set_mode(size)
while 1:
# Obtain a time delta that will be used to determine the full extent of the
# scroller's movement.
dt = clock.tick(60)
# Blank the screen first. Don't want any trails (At least not in this case.
screen.fill(black)
# Because for loops can't count themselves.
i = 0
# If a character is not present on the scroller, create one.
# Else an exception will happen.
if not scroller.sheet_chars:
scroller.get_next_char(320, 120, 0.1, 2, 0, 20)
for sheet_char in scroller.sheet_chars:
# Move each character to the left.
sheet_char.x -= sheet_char.x_speed * dt
# Move the character up and down according to a sine wave.
sheet_char.angle += sheet_char.y_speed
sheet_char.y = ((sheet_char.radius *
math.sin(sheet_char.angle * math.pi / 180)) +
120)
# Delete the character from memory if has left the visible screen.
# i needs to be decreased too, as it has effectively stepped back one.
if (sheet_char.x < -(char_sheet.char_width)):
scroller.delete_first_char()
i -= 1
else:
# If the newest character has fully entered the visible screen,
# a new one can be introduced.
if (i == (len(scroller.sheet_chars) - 1) and
sheet_char.x <= (width - char_sheet.char_width)):
scroller.get_next_char(320, 120, 0.1, 2, 0, 20)
# This was just some debug stuff to figure out an issue.
print(str(len(scroller.sheet_chars) - 1) + " | " + str(i) +
" | " + str(scroller.next_char))
# All done, onto the next character, if there is one.
i += 1
# Create a rect that will serve as a bounding box into which the
# character sprite will be drawn.
sect = char_sheet.get_sheet(sheet_char)
sprite = pygame.Rect((sect[0] * 16),
(sect[1] * 16), 16, 16)
# Draw the character to the screen.
screen.blit(char_sheet.sheet, (sheet_char.x,
sheet_char.y), sprite)
# Bring the finished frame to the foreground.
pygame.display.flip()

Loading…
Cancel
Save