如何解决我该如何使播放器不从平台侧入迷
我已经开始玩这个游戏了,我一直在尝试制作它,所以当玩家触摸我的方块的侧面时,它不会从里面消失并真正停止,我希望它像一个完全正常的方块一样工作,让您停止当您触摸侧面时。
https://gyazo.com/272b729154b0790fd3e004c761cdb658
如您所见,每次我碰到砌块的侧面时,它都会穿透它,但是我不希望它融化,我想要它实际碰到侧面,我知道如何使其碰到砖块。平台的顶部,但我不知道如何使其接触平台的侧面和底部。我已经尝试了下面显示的代码,但是没有用。
我尝试过的代码
if event.type == pygame.K_s:
if keys == pygame.K_a:
x_change = -7
if keys == pygame.K_d:
x_change = 7
if event.type == pygame.KEYUP:
if keys == pygame.K_a or keys == pygame.K_d:
x_change = 0
x += x_change
if x > 500 - playerman.width or x < 0:
x = old_x
我的完整代码
import pygame
pygame.init
window = pygame.display.set_mode((700,500))
pygame.display.set_caption("Noobs First Game")
# Playerman
class Player:
def __init__(self,x,y,width,height,color):
self.x = x
self.y = y
self.width = width
self.height = height
self.color = color
self.speed = 5
self.isJump = False
self.JumpCount = 10
self.fall = 0
self.rect = pygame.Rect(x,width)
def draw(self):
self.rect.topleft = (self.x,self.y)
pygame.draw.rect(window,self.color,self.rect)
class Platform:
def __init__(self,color):
self.x = x
self.y =y
self. width = width
self.color = color
self.height = height
self.color = color
self.rect = pygame.Rect(x,height)
def draw(self):
self.rect.topleft = (self.x,self.rect)
# Colors for hitBox
white = (255,255,255)
# Drawing Player
playerman = Player(255,40,white)
#Drawing Platforms
platform1 = Platform(200,470,100,30,white)
platform2 = Platform(400,410,white)
# List
platforms = [platform1,platform2]
# Windows color
def redrawwindow():
window.fill((0,0))
# Drawing the player and other stuff to the screen
playerman.draw()
for Platform in platforms:
Platform.draw()
x = 255
y = 255
x_change = 0
y_change = 0
old_x = x
old_y = y
fps = (30)
clock = pygame.time.Clock()
run = True
while run:
clock.tick(fps)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# lets player move
keys = pygame.key.get_pressed()
if keys[pygame.K_a]and playerman.x > playerman.speed:
playerman.x -= playerman.speed
if keys[pygame.K_d]and playerman.x < 700 - playerman.height - playerman.speed:
playerman.x += playerman.speed
if keys[pygame.K_w]and playerman.y > playerman.speed:
playerman.y -= playerman.speed
if keys[pygame.K_s]and playerman.y <500 - playerman.width - playerman.speed:
playerman.y += playerman.speed
if event.type == pygame.K_s:
if keys == pygame.K_a:
x_change = -7
if keys == pygame.K_d:
x_change = 7
if event.type == pygame.KEYUP:
if keys == pygame.K_a or keys == pygame.K_d:
x_change = 0
x += x_change
if x > 500 - playerman.width or x < 0:
x = old_x
# About isJump
if not playerman.isJump:
playerman.y += playerman.fall
playerman.fall += 1
playerman.isJump = False
# this part lets you jump on platform only the top
collide = False
for Platform in platforms:
if playerman.rect.colliderect(Platform.rect):
collide = True
playerman.isJump = False
playerman.y = Platform.rect.top - playerman.height + 1
if playerman.rect.right > Platform.rect.left and playerman.rect.left < Platform.rect.left - playerman.width:
playerman.x = Platform.rect.left - playerman.width
if playerman.rect.left < Platform.rect.right and playerman.rect.right > Platform.rect.right + playerman.width:
playerman.x = Platform.rect.right
# colliding with floor
if playerman.rect.bottom >= 500:
collide = True
playerman.isJump = False
playerman.Jumpcount = 10
playerman.y = 500 - playerman.height
# Jumping
if collide:
if keys[pygame.K_SPACE]:
playerman.isJump = True
playerman.fall = 0
# Jump Count
else:
if playerman.JumpCount >= 0:
playerman.y -= (playerman.JumpCount*abs(playerman.JumpCount))*0.3
playerman.JumpCount -= 1
else:
playerman.isJump = False
playerman.JumpCount = 10
redrawwindow()
pygame.display.update()
pygame.quit()
解决方法
当我制作平台游戏时,我会检查每一帧,看玩家是否正在与障碍物碰撞。 伪代码:
if player.movement_vel.x>0 and player.collide_obst():
player.right=player.collide_obst().rect.left
这有点抽象。 我只是假设player.collide_obst()函数返回与他碰撞的一个障碍。 当然,您必须: -使它也可以与玩家左右移动来配合使用,我建议您这样做,因此collide_obst()函数返回一个您在其中循环的列表。 学习这样的东西的好来源是: https://www.youtube.com/watch?v=Qdeb1iinNtk他在视频中以相同的方式报道了碰撞。 希望我能帮到您,拉斯
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。