问题描述
我正在创建基于瓦片的4向运动rpg。我为碰撞检测编写的代码相对简单,但图形错误很小。
玩家直接在均匀间隔的瓷砖之间移动。为了控制玩家移动的速度,有200秒的步行缓冲。当玩家与墙壁碰撞时,应以撞击墙壁的相同方向向后推。但是,这种方法很简单,玩家精灵会在墙上闪烁。
我怀疑这与播放器更新功能以及它的顺序有关,但我弄乱了它,无济于事。
import sys
vec = pg.math.Vector2
WHITE = ( 255,255,255)
BLACK = ( 0,0)
RED = ( 255,0)
YELLOW = ( 255,0)
BLUE = ( 0,255)
WIDTH = 512 # 32 by 24 tiles
HEIGHT = 384
FPS = 60
TILESIZE = 32
PLAYER_SPEED = 3 * TILESIZE
MAP = ["1111111111111111","1P.............1","1..............1","1..1111........1","1..1..1........1","1..1111.111111.1","1............1.1","1........111.1.1","1........1...1.1","1........11111.1","1111111111111111"]
def collide_hit_rect(one,two):
return one.hit_rect.colliderect(two.rect)
def player_collisions(sprite,group):
hits_walls = pg.sprite.spritecollide(sprite,group,False,collide_hit_rect)
if hits_walls:
sprite.pos -= sprite.vel * TILESIZE
class Player(pg.sprite.Sprite):
def __init__(self,game,x,y):
self.groups = game.all_sprites
pg.sprite.Sprite.__init__(self,self.groups)
self.game = game
self.walk_buffer = 200
self.vel = vec(0,0)
self.pos = vec(x,y) *TILESIZE
self.current_frame = 0
self.last_update = 0
self.walking = False
self.walking_sprite = pg.Surface((TILESIZE,TILESIZE))
self.walking_sprite.fill(YELLOW)
self.image = self.walking_sprite
self.rect = self.image.get_rect()
self.hit_rect = self.rect
self.hit_rect.bottom = self.rect.bottom
def update(self):
self.get_keys()
self.rect = self.image.get_rect()
self.rect.topleft = self.pos
self.pos += self.vel * TILESIZE
self.hit_rect.topleft = self.pos
player_collisions(self,self.game.walls)
self.rect.midbottom = self.hit_rect.midbottom
def get_keys(self):
self.vel = vec(0,0)
Now = pg.time.get_ticks()
keys = pg.key.get_pressed()
if Now - self.last_update > self.walk_buffer:
self.vel = vec(0,0)
self.last_update = Now
if keys[pg.K_LEFT] or keys[pg.K_a]:
self.vel.x = -1
elif keys[pg.K_RIGHT] or keys[pg.K_d]:
self.vel.x = 1
elif keys[pg.K_UP] or keys[pg.K_w]:
self.vel.y = -1
elif keys[pg.K_DOWN] or keys[pg.K_s]:
self.vel.y = 1
class Obstacle(pg.sprite.Sprite):
def __init__(self,y):
self.groups = game.walls
pg.sprite.Sprite.__init__(self,self.groups)
self.x = x * TILESIZE
self.y = y * TILESIZE
self.w = TILESIZE
self.h = TILESIZE
self.game = game
self.image = pg.Surface((self.w,self.h))
self.image.fill(BLACK)
self.rect = self.image.get_rect()
self.hit_rect = self.rect
self.rect.x = self.x
self.rect.y = self.y
class Game:
def __init__(self):
pg.init()
self.screen = pg.display.set_mode((WIDTH,HEIGHT))
pg.display.set_caption("Hello Stack Overflow")
self.clock = pg.time.Clock()
pg.key.set_repeat(500,100)
def new(self):
self.all_sprites = pg.sprite.Group()
self.walls = pg.sprite.Group()
for row,tiles in enumerate(MAP):
for col,tile in enumerate(tiles):
if tile == "1":
Obstacle(self,col,row)
elif tile == "P":
print("banana!")
self.player = Player(self,row)
def quit(self):
pg.quit()
sys.exit()
def run(self):
# game loop - set self.playing = False to end the game
self.playing = True
while self.playing:
self.dt = self.clock.tick(FPS) / 1000
self.events()
self.update()
self.draw()
def events(self):
# catch all events here
for event in pg.event.get():
if event.type == pg.QUIT:
self.quit()
def update(self):
self.player.update()
def draw(self):
self.screen.fill(WHITE)
for wall in self.walls:
self.screen.blit(wall.image,wall.rect)
for sprite in self.all_sprites:
self.screen.blit(sprite.image,sprite.rect)
pg.display.flip()
# create the game object
g = Game()
while True:
g.new()
g.run()
pg.quit()```
解决方法
您要在检查碰撞之前设置rect,但在碰撞检查之后不要重新设置。
这是更新后的代码(在Player类中,更新方法):
self.hit_rect.topleft = self.pos
player_collisions(self,self.game.walls) # may change postion
self.hit_rect.topleft = self.pos # reset rectangle