gpt4 book ai didi

python - Pygame:翻转时奇怪的 Sprite 偏移?

转载 作者:行者123 更新时间:2023-12-01 05:29:24 26 4
gpt4 key购买 nike

我正在开发一款基于马里奥的跑酷/自由奔跑游戏。当我向左移动时,就会出现问题,马里奥的 Sprite 在向左转动时会向后推几个像素。因此,相对于 Sprite 的矩形,图像距离它所碰撞的墙壁只有几个像素。

我在矩形周围画了一个红色框来显示我的意思:

enter image description here

我不确定发生了什么,我正在使用 Sprite 表进行动画,当向左运行时,它会加载适当的帧图像然后翻转它..所以我需要创建一个全新的向左跑的表还是什么?我被难住了。

抱歉,如果这个问题描述性不够,我无法描述这一点..

这是代码和所有文件的链接:

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()

文件:http://host-a.net/u/PlanetForgeGame/game.zip

最佳答案

我没有深入研究你的代码,但我认为你的问题与图像没有在最后一个“重要像素”结束的地方有关,如果你明白我的意思的话。例如,如果 X 是彩色像素,O 是白色,则您有

OOOXXXXX
OOOXXXXX
OOOXXXXX

而不是

XXXXXX
XXXXXX
XXXXXX

我希望这是有道理的。

关于python - Pygame:翻转时奇怪的 Sprite 偏移?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20599854/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com