Reputation: 965
I am working on a Mario based parkour/freerunning game. And the problem in question occurs when I am moving to the left, Mario's sprite is pushed back a few pixels when turned to the left. So relative to the sprite's rect, the image is a few pixels away from the wall it is colliding with.
I drew a red box around the rect to show what I mean:
I am not sure what is going on, I am using a sprite sheet for the animation, and when running to the left, it loads the appropriate frame image and then flips it.. so do I need to create a whole new sheet for running to the left or what? I am stumped.
Sorry if this question isn't descriptive enough, I am having trouble describing this..
Here is the code and a link to all the files:
import pygame
from pygame.locals import *
from collections import namedtuple
pygame.init()
screen=pygame.display.set_mode((640,480))
stand=pygame.image.load('images/stand.png').convert();stand.set_colorkey((255,255,255))
run_strip=pygame.image.load('images/run_strip.png').convert();run_strip.set_colorkey((255,255,255))
block1=pygame.image.load('images/block1.png')
p1=stand
clock=pygame.time.Clock()
px=0
py=30
f=8
direct=0
Move = namedtuple('Move', ['up', 'left', 'right'])
move = Move(0,0,0)
max_gravity = 100
def spr_fromsheet(spriteLocX, spriteLocY, spriteSizeX, spriteSizeY, sheetName):
sheet = sheetName
sprite = sheet.subsurface(pygame.Rect(spriteLocX, spriteLocY, spriteSizeX, spriteSizeY)) #grabs the sprite at this location
return sprite
rectexample=pygame.image.load('images/mariomask.png')
class Player(object):
sprite=rectexample
def __init__(self, x, y):
self.rect = self.sprite.get_rect(centery=y, centerx=x)
# indicates that we are standing on the ground
# and thus are "allowed" to jump
self.on_ground = True
self.xvel = 0
self.yvel = 0
self.jump_speed = 5
self.move_speed = 2
def update(self, move, blocks):
# check if we can jump
if move.up and self.on_ground:
self.yvel -= self.jump_speed
# simple left/right movement
if move.left:
self.xvel = -self.move_speed
if move.right:
self.xvel = self.move_speed
# if in the air, fall down
if not self.on_ground:
self.yvel += 0.3
# but not too fast
if self.yvel > max_gravity: self.yvel = max_gravity
# if no left/right movement, x speed is 0, of course
if not (move.left or move.right):
self.xvel = 0
# move horizontal, and check for horizontal collisions
self.rect.left += self.xvel
self.collide(self.xvel, 0, blocks)
# move vertically, and check for vertical collisions
self.rect.top += self.yvel
self.on_ground = False;
self.collide(0, self.yvel, blocks)
def collide(self, xvel, yvel, blocks):
# all blocks that we collide with
for block in [blocks[i] for i in self.rect.collidelistall(blocks)]:
# if xvel is > 0, we know our right side bumped
# into the left side of a block etc.
if xvel > 0:
self.rect.right = block.rect.left
if xvel < 0:
self.rect.left = block.rect.right
# if yvel > 0, we are falling, so if a collision happpens
# we know we hit the ground (remember, we seperated checking for
# horizontal and vertical collision, so if yvel != 0, xvel is 0)
if yvel > 0:
self.rect.bottom = block.rect.top
self.on_ground = True
self.yvel = 0
# if yvel < 0 and a collision occurs, we bumped our head
# on a block above us
if yvel < 0: self.rect.top = block.rect.bottom
class Block(object):
def __init__(self,x,y,sprite):
self.x=x
self.y=y
self.sprite=sprite
self.rect=self.sprite.get_rect(x=self.x,y=self.y)
blocks=[]
player=Player(40,40)
while True:
screen.fill((15,20,150))
key=pygame.key.get_pressed()
move = Move(key[K_w], key[K_a], key[K_d])
if key[K_d]:
direct=0
try:
p1=spr_fromsheet(f,0,28,37,run_strip)
f+=40
except:
f=8
elif key[K_a]:
direct=1
try:
p1=spr_fromsheet(f,0,28,37,run_strip)
p1=pygame.transform.flip(p1,1,0)
f+=40
except:
f=8
else:
if direct==0:
p1=stand
else:
p1=pygame.transform.flip(stand,1,0)
for e in pygame.event.get():
if e.type==QUIT:
exit()
if e.type==MOUSEBUTTONUP:
if e.button==1:
blocks.append(Block((pygame.mouse.get_pos()[0]/16)*16,(pygame.mouse.get_pos()[1]/16)*16,block1))
if e.button==3:
player=Player((pygame.mouse.get_pos()[0]/16)*16,(pygame.mouse.get_pos()[1]/16)*16)
for b in blocks:
screen.blit(b.sprite,b.rect)
player.update(move,blocks)
screen.blit(p1,player.rect)
clock.tick(60)
pygame.draw.rect(screen, (255,0,0), player.rect, 1)
pygame.display.update()
Files: http://host-a.net/u/PlanetForgeGame/game.zip
Upvotes: 1
Views: 486
Reputation: 1967
I didn't look too far into your code, but I think your problem has something to do with the images not ending where the last "significant pixel" is, if you know what I mean. For example, if X's are colored pixels and O's are white, you have
OOOXXXXX
OOOXXXXX
OOOXXXXX
instead of
XXXXXX
XXXXXX
XXXXXX
I hope this makes sense.
Upvotes: 3