Having problems creating an animation from a sprite sheet in pygame - python

I am relatively new to Python and started messing around with pygame a few days ago. I made a sprite sheet in photoshop and I am trying to use it to animate the player in the game. The sprite sheet has 8 images, 4 for walking left and 4 for walking right. I want to show these animations at the correct time. I have tried many different methods I've found online but I haven't managed to get any working and I am still quite confused about it.
This is the Char class that controls the character's movement and sprite. I tried indexing the sprite and changing the index in the update method but I understand this isn't possible but I've left it in to show what I was trying to do. Any help would be appreciated!
import pygame, sys
from pygame.sprite import Sprite
class Char(Sprite):
def __init__(self, pf_game):
"""initialise char and set its starting location"""
self.screen = pf_game.screen
self.settings = pf_game.settings
self.screen_rect = pf_game.screen.get_rect()
self.dog_sprite = pygame.image.load('images/dog_sprite_sheet.bmp').convert()
self.current_sprite = 0
self.dog_image = self.dog_sprite[self.current_sprite]
self.rect = self.dog_image.get_rect()
# start new char at the bottom of centre of the screen
self.rect.midbottom = self.screen_rect.midbottom
#store a decimal value for the ships horizontal position
self.x = float(self.rect.x)
self.y = float(self.rect.y)
#movement flags
self.moving_right = False
self.moving_left = False
self.is_jump = False
def update(self):
"""Update the chars position based on movement flags"""
#update the chars x value and create animation for moving right
if self.moving_right and self.rect.right<self.screen_rect.right:
if self.current_sprite == 7:
self.current_sprite = 4
self.dog_image = self.dog_sprite[self.current_sprite]
self.current_sprite+=1
self.x+= self.settings.char_speed
#update the chars x value and create animation for moving left
if self.moving_left and self.rect.left>0:
if self.current_sprite == 3:
self.current_sprite = 0
self.dog_image = self.dog_sprite[self.current_sprite]
self.current_sprite+=1
self.x-= self.settings.char_speed
#update rect object from self.x
self.rect.x = self.x
self.rect.y = self.y
def blitme(self):
"""Draw the char at its curretn loaction"""
self.screen.blit(self.dog_image, self.rect)
edit:
I found a spritesheet class that seems to do what I want on http://www.pygame.org/wiki/Spritesheet. I am running into an error though when I try and input the coordinates. I get the error images_at() got multiple values for argument 'colorkey'. When i remove the colorkey I get the error images_at() takes from 2 to 3 positional arguments but 5 were given.
This is the code I am using.
self.dog_sprite=spritesheet.spritesheet('dog_sprite_sheet.bmp')
self.left_images=[]
self.right_images=[]
self.left_images=self.dog_sprite.images_at((0,0,19,19),(20,0,19,19),(39,0,19,19),(58,0,19,19), colorkey=(255, 255, 255))
self.right_images=self.dog_sprite.images_at((77,0,19,19),(96,0,19,19),(115,0,19,19),(134,0,19,19), colorkey=(255, 255, 255))

this may help:
def anim(anim_count):
if anim_count < 6:
screen.blit('anim1.png', player_position)
if anim_count > 6 and anim_count < 12:
screen.blit('anim2.png', player_position)
you should call anim at every frame, and then apply 1 to anim_count

Related

Problems with Sprites Appearing and Collision with Rotated Objects; Pygame object is not iterable

I'm currently trying to make pixel perfect collisions between my pong ball and my player's paddle using the mask and collide_rect functions. I made my own checkCollision function in the pong class which would check for pixel perfect collision. However, right now, I can't even get the Sprites to work or appear on the screen because my "Pong object is not iterable.
Here is my pong class with the important features: (I will post additional code if needed)
class Pong(pygame.sprite.Sprite):
def __init__(self, screensize):
pygame.sprite.Sprite.__init__(self)
self.screensize = screensize
self.centerx = screensize[0] // 2
self.centery = screensize[1] // 2
self.radius = 25
self.rect = pygame.Rect(self.centerx-self.radius,
self.centery-self.radius,
self.radius*2, self.radius*2)
self.pokeimage = pygame.image.load("pokeball.png")
self.pokeimage = pygame.transform.scale(self.pokeimage, (self.radius, self.radius))
#Create the mask
self.mask = pygame.mask.from_surface(self.pokeimage)
def checkCollision(self, player_paddle, ai_paddle):
col = pygame.sprite.collide_rect(self, player_paddle)
return col
def collisionFormula(self, player_paddle, ai_paddle):
if self.checkCollision(self, player_paddle):
def collision_checks(self, player_paddle, ai_paddle):
#if the ball hits the top or bottom of the screen, change the y direction
if self.rect.top <= 0 or self.rect.bottom >= self.screensize[1] - 1:
self.direction[1] *= -1
#if the pong hits the paddles, change how the pong ball moves
if self.rect.colliderect(player_paddle.rect) or self.rect.colliderect(ai_paddle.rect):
self.collisionFormula(player_paddle, ai_paddle)
def update(self, player_paddle, ai_paddle):
self.update_ball_position()
self.reset_ball()
self.collision_checks(player_paddle, ai_paddle)
In my PlayerPaddle class, I do the same mask initialization.
class PlayerPaddle(pygame.sprite.Sprite):
def __init__(self, screensize):
pygame.sprite.Sprite.__init__(self)
self.screensize = screensize
self.centerx = 50
self.centery = screensize[1]//2
self.height = 100
self.width = 20
self.imageMaster = pygame.image.load("naruto.png").convert_alpha()
self.imageMaster = pygame.transform.scale(self.imageMaster, (self.width, self.height))
self.image = self.imageMaster
#mask
self.mask = pygame.mask.from_surface(self.image)
def turnLeft(self):
self.dir += 45
if self.dir > 360:
self.dir = 45
def turnRight(self):
self.dir -= 45
if self.dir < 0:
self.dir = 315
def update(self):
#Rotate functions
oldCenter = self.rect.center
self.image = pygame.transform.rotate(self.imageMaster, self.dir)
self.rect = self.image.get_rect()
self.rect.center = oldCenter
And here is my main function:
def main():
pygame.init()
screensize = (640,700)
screen = pygame.display.set_mode(screensize)
background = pygame.Surface(screen.get_size())
background.fill((0, 255, 0))
clock = pygame.time.Clock()
pong = Pong(screensize)
player_paddle = PlayerPaddle(screensize)
ai_paddle = AIPaddle(screensize)
paddleSprite = pygame.sprite.Group(player_paddle)
pongSprite = pygame.sprite.Group(pong)
while running:
running = True
#object updating phase
ai_paddle.update(pong, player_paddle)
player_paddle.update()
pong.update(player_paddle, ai_paddle)
if pygame.sprite.spritecollide(player_paddle, pong, False, pygame.sprite.collide_mask):
print("Collided")
#rendering phase
ai_paddle.render(screen)
player_paddle.render(screen)
pong.render(screen)
paddleSprite.clear(screen, background)
paddleSprite.update()
paddleSprite.draw(screen)
pongSprite.clear(screen,background)
pongSprite.update()
pongSprite.draw(screen)
pygame.display.flip()
pygame.quit()
main()
I made two "group" objects (the pong and the player_paddle) but I'm not sure why I'm even failing to run the program. Additionally, I know the collision will not work because the pong ball will hit the rectangle of the original image, but not the rotated image, but I'm not sure why that will happen if I use the built in sprite function. Thanks.
Read documentation for spritecollide
Find sprites in a group that intersect another sprite.
spritecollide(sprite, group, dokill, collided = None) -> Sprite_list
Second argument has to be group (pygame.sprite.Group), not single Sprite.
It can be group event with one sprite. But you use pong which is single sprite, not group.
See documentation for collide_mask
Collision detection between two sprites, using masks.
collide_mask(SpriteLeft, SpriteRight) -> point
It checks collision between two sprites using mask.
EDIT: in your code you have problem with
spritecollide(player_paddle, pong,...)
because pong is single Sprite, not Group.
With pong you should use collide_mask
collide_mask(player_paddle, pong)
You can use spritecollidewith pongSprite which is Group
spritecollide(player_paddle, pongSprite,...)`
BTW: you could use better names ie. pong_group instead of pongSprite.
And eventually pong_sprite instead of pong (but pong is OK, too).

How do I set a boundary in which my image can go through in Pygame?How do I keep an image from going behind another?

This is the code for my pygame
import pygame
import os
img_path=os.path.join('C:/Desktop/Python Stuff','Angry Birds.jpg')
class pic(object):
def __init__(self):
""" The constructor of the class """
self.image = pygame.image.load(img_path)
# the bird's position
self.x = 0
self.y = 0
def handle_keys(self):
""" Handles Keys """
key = pygame.key.get_pressed()
dist = 5
if key[pygame.K_DOWN]: # down key
self.y += dist # move down
elif key[pygame.K_UP]: # up key
self.y -= dist # move up
if key[pygame.K_RIGHT]: # right key
self.x += dist # move right
elif key[pygame.K_LEFT]: # left key
self.x -= dist # move left
def draw(self, surface):
""" Draw on surface """
# blit yourself at your current position
surface.blit(self.image, (self.x, self.y))
This is the screen size. Is this where the I should restrict the image's boundaries?
pygame.init()
screen=pygame.display.set_mode([1500,850])
Pic=pic()
pygame.display.set_caption('Angry Birds')
This is the image that I want to have a boundary for
pic=pygame.image.load('Angry Birds.jpg')
keep_going=True
while keep_going:
event=pygame.event.poll()
*emphasized text* if event.type == pygame.QUIT:
pygame.quit()
running = False
Pic.handle_keys()
screen.blit(pic, (-200, 0))
Pic.draw(screen)
This image is what the 'Angry Birds' image is going behind. How do I stop it from going behind this image?
tux=pygame.image.load('Rock Lee.gif')
screen.blit(tux,(500,600))
screen.blit(tux,(500,400))
screen.blit(tux,(500,0))
screen.blit(tux,(900,200))
screen.blit(tux,(900,400))
screen.blit(tux,(900,600))
screen.blit(tux,(1300,0))
screen.blit(tux,(1300,200))
screen.blit(tux,(1300,600))
pygame.display.get_surface([1500,850]).get_size([1500,850])
pygame.display.update()
A) Keep rect on screen
The simplest way would be using Rect.clamp_ip(rect) on a Sprite
screen_size = Rect(1500,850)
# right after when you move the bird
bird.rect.clamp_ip(screen_size)
B) rect on rect collision
# Where .xvel and .yvel are bird's movement per frame
new_rect = bird.rect.move(bird.vxel, bird.yvel)
if not new_rect.collide_rect(other_bird.rect)
bird.rect = new_rect
else
print("Ouch!")
Border collision
An easy way to implement border collision is to just check if the current position is outside the screen, and if it is you move it back. It's easiest done by creating a Rect object from the screen which you could pass in an update method of your class pic (classes should start with capital letter). So start with creating an update method were you pass the screen object.
Also, since the x and y position reference the top left of the image you need to take that in consideration when checking for border collision with the right side and the bottom. Best would be to create attributes width and height instead of what I'm doing below.
def update(self, screen):
"""Method that check border collision for object 'pic'."""
border = screen.get_rect()
width = self.image.get_width()
height = self.image.get_height()
if self.x < border.left:
# You collided with the left side of the border.
# Move your character back to the screen
self.x = border.left
elif self.x > border.right - width:
# You collided with the right side of the border.
# Move your character back to the screen
self.x = border.right - width
if self.y < border.top:
# You collided with the top of the border.
# Move your character back to the screen
self.y = border.top
elif self.y > border.bottom - height:
# You collided with the bottom of the border.
# Move your character back to the screen
self.y = border.bottom - height
All you have to do is call this method every time in your loop, like so:
Pic.handle_keys() # Variables should be all lowercase! Not like it's currently.
Pic.update(screen) # Variables should be all lowercase! Not like it's currently.
Pic.draw(screen) # Variables should be all lowercase! Not like it's currently.
Keep image in front
When blitting to the screen it draws each image on top of each other. In your case you're blitting your character and then the rocks, meaning the rocks always be on top of your character. Changing this is simple, blit the rocks first and the character last and your character will end up in front of your rocks.

Pygame spritecollide hitboxes

I am working on a simple 2D platformer, but I am having some trouble with the hitboxes of my sprites. I use the pygame.sprite.spritecollide function to generate a list of blocks(platforms) touching my player sprite.
Here is my player class:
class Player( pygame.sprite.Sprite ):
def __init__(self,x,y,image):
super( Player, self ).__init__()
self.vel_x = 0
self.vel_y = 0
self.moveSpeed = 3
self.jumpPower = 7
self.direction = idle
self.grounded = False
self.falling = True
self.jumping = False
self.climbing = False
self.image = pygame.Surface((50,50))#pygame.transform.scale( image, (50,50))
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.width = 50
self.height = 50
self.rect.bottom = self.rect.y + self.height
def set_position( self,x,y ):
self.rect.x = x
self.rect.y = y
def experience_gravity(self, gravity = 0.3):
if not self.grounded:
self.vel_y += gravity
self.falling = True
else:
self.vel_y = 0
self.grounded = True
self.falling = False
def jump(self):
self.grounded = False
self.vel_y -= self.jumpPower
def update(self, collidable = pygame.sprite.Group() ):
global collision
self.experience_gravity()
self.rect.x += self.vel_x
self.rect.y += self.vel_y
collision_list = pygame.sprite.spritecollide( self, collidable, False )
for p in collision_list:
if ( self.vel_y > 0 ):
self.vel_y = 0
self.grounded = True
In my the update method, I check for collisions between a sprite group holding all of my platforms (parameter collidable) and the player.
Here's my block class:
class Block( pygame.sprite.Sprite ):
def __init__( self, x, y, width, height):
super( Block, self ).__init__()
self.image = pygame.Surface( ( width, height ) )
self.image.fill( black )
self.rect = self.image.get_rect()
self.type = platform
self.rect.x = x
self.rect.y = y
self.width = width
self.height = height
Pretty self-explanatory block class. The rest of my code is to update and blit everything onto a white background. The problem I run into is that when the player lands on a platform, it only stops falling (becomes grounded) when it is already in the platform. Even stranger, the depth the block sinks intot he platform is not consistent. Sometimes it will fall in 10 pixels, other ties 20 pixels. Here's a screenshot of the player getting stuck:
The player block is stuck inside the platform block
So this is really baffling me, especially since the amount the block falls in is inconsistent. If anyone could give me an idea on how to fix this, I'll really appreciate it.
With kindest regards,
Derek
It's hard to diagnose without any debugging trace, but I have an idea or two.
First of all, dump a debugging trace: print the salient values at each time step: position of the player block, positions of colliding blocks, and applicable velocities. This should give you a table-like snapshot at each time step of the involved objects.
Now, look at the values in the last time step before the collision. I suspect that you might have something like a player velocity of -20, but landing on a platform only 10 pixels below. Your update logic needs to handle the landing between the two time steps, and stop the player at the platform height. It looks to me as if the player is allowed to fall for the entire time step, which will embed his tender lower extremities 10 pixels into the platform.
Second, check that you're comparing the nearer edges: in this case, the player's lower edge with the platform's upper edge. You will later need to handle the case where a player's lower-right corner contacts the upper-left corner of a block, and you have to determine whether the player first hits the block's top (landing) or side (change horizontal velocity and drop).
I wouldn't alter the computed velocity; I would think it's easier just to adjust the final position. The last block of code you posted is where you already deal with a collision as a special case. When you detect a collision, get the top surface (y-value) of the object. If that's above the current position of the bottom of the player, assign that top surface as the correct position. This will leave your player sitting on top of whatever is the highest object on the collision list.
Note that this works only as long as your entire collision list is from falling; for lateral collisions as well, you'll need to separate the lists and adjust each boundary as needed. This can get complicated if there are collisions from several sides on the same time step -- for instance, a goombah slams into the player from the left just as the player hits both a wall and a platform.

Get the position of the collided sprite

I need to get the position of the last sprite that collided between 2 groups. All the sprites in a group have the same name. here is the concentrate code:
rmog = pygame.sprite.Group()
rmg = pygame.sprite.Group()
class rmissileh(pygame.sprite.Sprite):
def __init__(self, image, speed):
pygame.sprite.Sprite.__init__(self)
self.speed = -10
self.speedx = 0
self.image = redm
self.rect = image.get_rect().move(s.rect.x+26,s.rect.y)
def move(self):
self.rect = self.rect.move(self.speedx, self.speed)redshot =
rmissileh(redm, -10 )
rmg.add(redshot)
class crmonstre(pygame.sprite.Sprite):
def __init__(self, image, speed):
pygame.sprite.Sprite.__init__(self)
self.speedy = 2
self.speedx = 1
self.image = rmonstre
self.step = 600
self.rect = rmonstre.get_rect().move(500,-100)
def move(self):
self.rect = self.rect.move(self.speedx,self.speedy)
redmonstre = crmonstre(rmonstre,1)
rmog.add(redmonstre)
redmonstre = crmonstre(rmonstre,1)
rmog.add(redmonstre)
redmonstre = crmonstre(rmonstre,1)# I added only three but there is many more overtime.
rmog.add(redmonstre)
while stop:
for event in pygame.event.get():
if event.type ==pygame.QUIT:
stop = False
for redmonstre in rmog:
fenetre.blit(fond, redmonstre.rect, redmonstre.rect)
redmonstre.move()
fenetre.blit(redmonstre.image,redmonstre.rect)
if pygame.sprite.groupcollide(rmg, rmog,True ,True):
fenetre.blit(fond, (0,0))
pygame.display.update()
pygame.quit()
quit
As you can see when a redmonster collide with a redshot i have to blit the whole background(named "fond") because i don't know his position, if I print(redmonstre.rect.x) that will print the x position of the last object that i created and not the position of the one that collided.
And also in the while loop to blit a moving object i do :
for redmonstre in rmog:
fenetre.blit(fond, redmonstre.rect, redmonstre.rect)
redmonstre.move()
fenetre.blit(redmonstre.image,redmonstre.rect)
i have many many for loop like that in my code and it doesn't feel right if you have an other way to do it please let me know.
I don't know how to find just the last collision. But here is how you could find all of the collisions, and hopefully that's a step in the right direction.
There are two ways you could do this. The first way would be to iterate through the list of sprites yourself. That would look kind of like this:
for sprite in rmg:
#Note that spritecollide will remove every sprite that collides from
#rmog, but it will not remove 'sprite' from rmg.
if (pygame.sprite.spritecollide(sprite, rmog, True)):
print (sprite.x)
rmg.remove(sprite)
spritecollide docs.
Here is the other way you could do it.
Look at the documentation for pygame.sprite.groupcollide. You'll see that it returns a dictionary of all the collisions. The dictionary will have all of the sprites from group 1 (which is rmg) as the keys, and the value for each key will be a list of every sprite from group 2 (which is rmog) that collided with it. So then to find every sprite that collided from group one, you could do:
dict = pygame.sprite.groupcollide(rmg, rmog,True ,True)
for sprite in dict:
if dict[sprite]:
print (sprite.x)
Also, a couple general tips that are unrelated to your problem,
Don't do this:
redmonstre = crmonstre(rmonstre,1)
rmog.add(redmonstre)
redmonstre = crmonstre(rmonstre,1)
rmog.add(redmonstre)
redmonstre = crmonstre(rmonstre,1)
rmog.add(redmonstre)
There is no need to have a name for each redmonstre, and it will only make it more confusing to have them all named the same. Instead, just add them directly to the list:
rmog.add(crmonstre(rmonstre, 1))
rmog.add(crmonstre(rmonstre, 1))
rmog.add(crmonstre(rmonstre, 1))
And don't do this:
for redmonstre in rmog:
fenetre.blit(fond, redmonstre.rect, redmonstre.rect)
redmonstre.move()
fenetre.blit(redmonstre.image,redmonstre.rect)
Blitting "fond" takes a (relatively) long time. If you blit it for every redmonstre, you will only see the last one. Just blit it once before the loop.
fenetre.blit(fond, redmonstre.rect, redmonstre.rect)
for redmonstre in rmog:
redmonstre.move()
fenetre.blit(redmonstre.image,redmonstre.rect)

Error with NPC random movement [duplicate]

This question already has answers here:
How do I make my monster move randomly in my game
(1 answer)
Pygame Pacman - Enemy Random Movement
(1 answer)
Closed 2 years ago.
I made part of a game. In the game there is a player and enemies. I decided to add NPC characters to the game which move randomly. The problem is that they do not draw as expected.
NPCs do not run as smoothly as the rest of the code. When an NPC moves around it leaves big patches of white where it has been. I think it's the speed of the update but I'm not quite sure. The framerate is roughly 40 fps. I have tried to find a problem in the update function of NPCs but it looks perfectly fine. However, NPCs only stop creating the weird white patches when I exclude the def update(self). I'm running Window Vista, Python 2.6 and Pygame. Any help is appreciated.
class NPC(pygame.sprite.Sprite):
change_x = 0
def __init__(self, color, width, height):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface([width, height])
self.image.fill(white)
self.rect = self.image.get_rect()
def update(self):
self.rect.x += self.change_x
speed = 2
Movementlist = ['moveRight', 'moveLeft']
randomMove = random.choice(Movementlist)
if randomMove == 'moveRight':
self.change_x = -6 * speed
elif randomMove == 'moveLeft':
self.change_x = 6 * speed
Here is the where an NPC is created:
if level == 1:
npc = NPC(white, 20, 15)
npc.rect.x = 400
npc.rect.y = 485
NPC_list.add(npc)
all_sprites_list.add(npc)
And here's the update:
NPC_list.update()
The draw commands are here:
# --- Draw Frame
# Set the screen background
if level < 5 or level == 5:
screen.fill(black)
if level == 10:
screen.fill(silver)
all_sprites_list.draw(screen)
# Go ahead and update the screen with what we've drawn.
pygame.display.update()
# Limit to 40 frames per second
clock.tick(40)

Categories