Original Post
import os, sys
import pygame
from pygame.locals import *
SCREENRECT = Rect(0, 0, 590, 400)
def load_image(name):
fullname = name
try:
image = pygame.image.load(fullname)
except pygame.error:
print ('Cannot load image:', fullname)
raise SystemExit(str(geterror()))
image = image.convert()
return image
class AnimatedSprite(pygame.sprite.Sprite):
def __init__(self, images, fps = 10):
pygame.sprite.Sprite.__init__(self)
self._images = images
self.image = images[0]
self.rect = self.image.get_rect()
self.rect.midtop = (300,200)
# Track the time we started, and the time between updates.
# Then we can figure out when we have to switch the image.
self._start = pygame.time.get_ticks()
self._delay = 1000 / fps
self._last_update = 0
self._frame = 0
self.min = 0
self.max = 0
self.speed = 3
# Call update to set our first image.
self.animate(pygame.time.get_ticks())
def move(self, direction):
if direction == 1:
self.min = 0
self.max = 10
if direction == -1:
self.min = 11
self.max = 21
self.animate(pygame.time.get_ticks())
self.rect.move_ip(direction*self.speed, 0)
self.rect = self.rect.clamp(SCREENRECT)
def animate(self, t):
# Note that this doesn't work if it's been more that self._delay
# time between calls to update(); we only update the image once
# then, but it really should be updated twice.
if t - self._last_update > self._delay:
self._frame += 1
if (self._frame + self.min ) >= self.max: self._frame = 0
self.image = self._images[self._frame + self.min]
self._last_update = t
def main():
"""this function is called when the program starts.
it initializes everything it needs, then runs in
a loop until the function returns."""
#Initialize Everything
pygame.init()
screen = pygame.display.set_mode((600, 400))
pygame.display.set_caption('Game')
pygame.mouse.set_visible(0)
image1 = pygame.image.load('r1.png')
image2 = pygame.image.load('r2.png')
image3 = pygame.image.load('r3.png')
image4 = pygame.image.load('r4.png')
image5 = pygame.image.load('r5.png')
image6 = pygame.image.load('r6.png')
image7 = pygame.image.load('r7.png')
image8 = pygame.image.load('r8.png')
image9 = pygame.image.load('r9.png')
image10 = pygame.image.load('r10.png')
image11 = pygame.image.load('r11.png')
image12 = pygame.image.load('l1.png')
image13 = pygame.image.load('l2.png ')
image14 = pygame.image.load('l3.png')
image15 = pygame.image.load('l4.png')
image16 = pygame.image.load('l5.png')
image17 = pygame.image.load('l6.png')
image18 = pygame.image.load('l7.png')
image19 = pygame.image.load('l8.png')
image20 = pygame.image.load('l9.png')
image21 = pygame.image.load('l10.png')
image22 = pygame.image.load('l11.png')
images = [image1,
image2,
image3,
image4,
image5,
image6,
image7,
image8,
image9,
image10,
image11,
image12,
image13,
image14,
image15,
image16,
image17,
image18,
image19,
image20,
image21,
image22]
#Create The Backgound
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((250, 250, 250))
#Display The Background
screen.blit(background, (0, 0))
pygame.display.flip()
clock = pygame.time.Clock()
mega = AnimatedSprite(images)
allsprites = pygame.sprite.RenderPlain((mega))
#Main Loop
going = True
while going:
clock.tick(60)
for event in pygame.event.get():
if event.type == QUIT or \
(event.type == KEYDOWN and event.key == K_ESCAPE):
return
keystate = pygame.key.get_pressed()
if keystate[K_RIGHT] or keystate[K_LEFT]:
direction = keystate[K_RIGHT] - keystate[K_LEFT]
mega.move(direction)
else :
mega._frame = 0
#mega.animate(pygame.time.get_ticks())
screen.blit(background, (0,0))
allsprites.draw(screen)
pygame.display.flip()
pygame.quit
if __name__ == '__main__':
main()
My problem is this : My sprite's movement and animations are smooth in the left direction ( <----- ), but seem jittery in the right direction (-------> )
Also : The sprite clamps correctly to the left ( stops at the edge of the screen ), but not to the right.
I have a feeling these two problems are related.
Any help would be greatly appreciated, I've been staring at this code for about an hour without any luck.
:/