希望你能帮助我, 过去几天我一直在努力解决这个问题。 我想做一个坚实的"阻止我正在进行的我的2d平台游戏,但是当我进入那个"固体"阻止我传送到另一侧或块的底部,我不知道为什么,希望你知道为什么 所以这是我的代码:
class Block(pygame.sprite.Sprite):
def __init__(self, color = blue,widht = 64, height = 64):
super(Block, self).__init__()
self.image = pygame.Surface((widht, height))
self.image.fill(color)
self.rect = self.image.get_rect()
self.sound = pygame.mixer.Sound("2dSounds/Walk.wav")
self.hspeed = 0
self.vspeed = 0
def update(self, collidable):
self.rect.x += self.hspeed
collision_list = pygame.sprite.spritecollide(self, collidable, False)
for collided_object in collision_list:
if (self.hspeed > 0):
# right direction
self.rect.right = collided_object.rect.left
elif (self.hspeed < 0):
# left direction
self.rect.left = collided_object.rect.right
self.rect.y += self.vspeed
collision_list = pygame.sprite.spritecollide(self, collidable, False)
for collided_object in collision_list:
if (self.vspeed > 0):
# down direction
self.rect.bottom = collided_object.rect.top
elif (self.vspeed < 0):
# up direction
self.rect.top = collided_object.rect.bottom
def change_speed(self, hspeed, vspeed):
self.hspeed += hspeed
self.vspeed += vspeed
def set_position(self, x, y):
self.rect.x = x
self.rect.y = y
非常感谢! :)
答案 0 :(得分:0)
很抱歉,我的代码并非基于您的代码,我在过去的5分钟里只是把它放在一起,所以它不是很漂亮,但这里是:< / p>
from pygame.locals import *
import pygame
import sys
GRAVITY = 1.2
class Player:
def __init__(self, x, y):
self.rect = pygame.Rect(x, y, 25, 25)
self.yvel = 0
def tick(self):
self.yvel += GRAVITY
self.rect.y += int(self.yvel)
if self.rect.y >= 475:
self.rect.y = 475
self.yvel = 0
def set(self, y):
if y:
self.yvel = y
class Block:
def __init__(self, x, y, w, h):
self.rect = pygame.Rect(x, y, w, h)
class Game:
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode((500, 500))
self.player = Player(0, 0)
self.block = Block(225, 400, 50, 50)
def main(self):
right = left = False
while True:
self.screen.fill((230, 230, 230))
pygame.draw.rect(self.screen, (40, 40, 40), self.block.rect)
pygame.draw.rect(self.screen, (100, 200, 100), self.player.rect)
pygame.display.flip()
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit(0)
elif event.type == KEYDOWN:
if event.key == K_SPACE:
self.player.set(-20)
elif event.key == K_RIGHT:
right = True
elif event.key == K_LEFT:
left = True
elif event.type == KEYUP:
if event.key == K_RIGHT:
right = False
elif event.key == K_LEFT:
left = False
if right:
self.player.rect.x += 5
while self.player.rect.colliderect(self.block.rect):
self.player.rect.x -= 1
if left:
self.player.rect.x -= 5
while self.player.rect.colliderect(self.block.rect):
self.player.rect.x += 1
self.player.tick()
if self.player.yvel > 0:
while self.player.rect.colliderect(self.block.rect):
self.player.rect.y -= 1
self.player.yvel = 0
elif self.player.yvel < 0:
while self.player.rect.colliderect(self.block.rect):
self.player.rect.y += 1
self.player.yvel = 0
game = Game()
game.main()
播放器速度非常快,但可以在以下行改变:
self.player.rect.x -= 5
和
self.player.rect.x += 5
靠近底部。
它的工作方式是每当你移动时,它会移动玩家,然后查看是否与该块相撞。如果是这样,则玩家向相反方向移动,直到确信它不再发生碰撞为止。
跳跃是使用速度完成的,因此Player.yvel
和GRAVITY
。可以更改GRAVITY
但如果更高,则跳转会更短,反之亦然。
玩弄代码并尝试阻止位置(self.block = Block(225, 400, 50, 50)
)和玩家的凝视位置(self.player = Player(0, 0)
)之类的东西
如果您在代码中发现任何错误,请告诉我,我会尝试修复它们。