我目前正在做一个简单的学校项目,我正在编写Platformer。我的问题是,要逐一检查玩家和平台之间的碰撞,因此,如果我先检查X碰撞,平台侧面的碰撞就可以正常工作,但是一旦我移动,我的角色将被传送到平台的侧面,因为重力将我拉入平台,并且我向左或向右移动,因此程序认为我是从侧面击打平台,并将玩家设置在平台的侧面平台。如果我先检查X Collision,但在平台侧面的Collisions无法正常工作时,会发生相同的事情。
是否有解决方法,所以我的碰撞就像在马里奥游戏中一样起作用?我尝试了几件事,并使用“摩擦力和加速度”移除了计算以使运动平稳,但似乎没有任何效果。
这是我的完整代码:
import random
import os
WIDTH = 640
HEIGHT = 600
FPS = 60
TITLE = "Game"
game_folder = os.path.dirname(__file__)
img_folder = os.path.join(game_folder,"textures")
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
DARKGREY = (40, 40, 40)
LIGHTGREY = (100, 100, 100)
BLUE = (0,0,255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)
PLAYER_ACC = 0.5
PLAYER_FRICTION = -0.12
PLAYER_GRAV = 0.5
PGROUND = "dirtplatform.png"
PBLOCK = "dirt.png"
PLATFORM_LIST = [(0, HEIGHT - 64, PGROUND),
(WIDTH / 2, HEIGHT / 2, PBLOCK),
(WIDTH / 4, HEIGHT / 4, PBLOCK)]
vec = pygame.math.Vector2
class Player(pygame.sprite.Sprite):
def __init__(self, game):
pygame.sprite.Sprite.__init__(self)
self.game = game
self.image = pygame.image.load(os.path.join(img_folder,"player.png")).convert()
self.image.set_colorkey((130,255,230))
self.rect = self.image.get_rect()
self.rect.center = (WIDTH/2,HEIGHT/2)
self.vel = vec(0,0)
def jump(self):
self.rect.y += 1
hits = pygame.sprite.spritecollide(self, self.game.platforms, False)
self.rect.y -= 1
if hits:
self.vel.y = -20
def collision(self, direction):
if direction == 'x':
hits = pygame.sprite.spritecollide(self, self.game.platforms, False)
if hits:
print("Hit x")
if self.vel.x > 0:
self.rect.right = hits[0].rect.left
if self.vel.x < 0:
self.rect.left = hits[0].rect.right
self.vel.x = 0
if direction == 'y':
hits = pygame.sprite.spritecollide(self, self.game.platforms, False)
if hits:
print("Hit y")
if self.vel.y > 0:
self.rect.bottom = hits[0].rect.top
if self.vel.y < 0:
self.rect.top= hits[0].rect.bottom
self.vel.y = 0
def update(self):
self.acc = vec(0,PLAYER_GRAV)
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
self.vel.x = -5
elif keys[pygame.K_RIGHT]:
self.vel.x = 5
self.rect.x = self.rect.x + self.vel.x
self.rect.y = self.rect.y + self.vel.y
self.collision('x')
self.collision('y')
print("vel",self.vel)
print("rectX",self.rect.x)
print("rectY",self.rect.y)
self.vel.y = self.vel.y + PLAYER_GRAV
self.vel.x = 0
class Platform(pygame.sprite.Sprite):
def __init__(self, x, y, image):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(os.path.join(img_folder,image)).convert()
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
class Game:
def __init__(self):
pygame.init()
pygame.mixer.init()
self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption(TITLE)
self.clock = pygame.time.Clock()
self.running = True
def new(self):
self.all_sprites = pygame.sprite.Group()
self.platforms = pygame.sprite.Group()
self.player = Player(self)
self.all_sprites.add(self.player)
for plat in PLATFORM_LIST:
p = Platform(*plat)
self.all_sprites.add(p)
self.platforms.add(p)
self.run()
def run(self):
self.playing = True
while self.playing:
self.clock.tick(FPS)
self.events()
self.update()
self.draw()
def collision(self):
if self.player.vel.y > 0 and pygame:
hits = pygame.sprite.spritecollide(self.player, self.platforms, False)
if hits:
self.player.pos.y = hits[0].rect.top
self.player.vel.y = 0
def update(self):
self.all_sprites.update()
keys = pygame.key.get_pressed()
if self.player.rect.right >= WIDTH-100:
self.player.rect.x = WIDTH - 110
for plat in self.platforms:
plat.rect.right -= abs(self.player.vel.x)
if self.player.rect.left <= 100:
self.player.rect.x = 110
for plat in self.platforms:
plat.rect.right += abs(self.player.vel.x)
def events(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
if self.playing:
self.playing = False
self.running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
self.player.jump()
def draw(self):
self.screen.fill(BLACK)
self.all_sprites.draw(self.screen)
pygame.display.flip()
def show_start_screen(self):
pass
def show_go_screen(self):
pass
g = Game()
g.show_start_screen()
while g.running:
g.new()
g.show_go_screen
pygame.quit()
注意:dirtplatform.png代表游戏的基础,dirty.png是一个块,玩家应该可以跳上该块
冲突很困难,似乎没有完美的方法可以做到,而且每次我做时,它每次都会改变。这样说,这似乎与我刚刚进行的2分钟测试非常有效:
def collision(self):
for platform in self.game.platforms: #check every platform
if self.rect.colliderect(platform.rect): #if the two rects collided
if self.rect.right <= platform.rect.left + self.vel.x:
self.rect.right = platform.rect.left
self.vel.x = 0
if self.rect.left >= platform.rect.right + self.vel.x:
self.rect.left = platform.rect.right
self.vel.x = 0
if self.rect.bottom <= platform.rect.top + self.vel.y:
self.rect.bottom = platform.rect.top
self.vel.y = 0
if self.rect.top >= platform.rect.bottom + self.vel.y:
self.rect.top = platform.rect.bottom
self.vel.y = 0