试图做一个“固体"在 pygame 中阻止但它不起作用 [英] Trying to make a "solid" block in pygame but it doesnt work
问题描述
希望你能帮助我,最近几天我一直在努力解决这个问题.我想为我正在开发的 2d 平台游戏制作一个固体"方块,但是当我进入那个固体"方块时,我会传送到另一侧或方块底部,我不知道为什么,希望你知道为什么所以这是我的代码:
Hopefully you can help me, i have been struggling with this problem for the last few days. I want to make a "solid" block for my 2d platformer game that i am working on but when i go into that "solid" block i teleport to the other side or to the bottom of the block, i dont know why, hopefully you know why So here is my code:
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
非常感谢!:)
推荐答案
抱歉我的代码不是基于你的,我只是在过去 5 分钟内把它放在一起,所以它不是很漂亮,但它是:
Sorry about the fact that my code isn't based off your's, I literally just threw it together in the past 5 minutes so it isn't very pretty, but here it is:
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()
播放器速度相当快,但可以通过以下方式更改:
The player speed is quite fast, but that can be changed at the lines:
self.player.rect.x -= 5
和
self.player.rect.x += 5
靠近底部.
它的工作方式是,每当您移动时,它都会移动玩家,然后查看是否与方块相撞.如果确实如此,则玩家向相反的方向移动,直到对不再发生碰撞感到满意为止.跳跃是使用速度完成的,因此 Player.yvel
和 GRAVITY
.GRAVITY
可以改变,但如果它更高,跳跃会更短,反之亦然.
试玩一下代码并尝试诸如方块位置 (self.block = Block(225, 400, 50, 50)
) 和玩家的注视位置 (self.player= 玩家(0, 0)
)
如果您确实发现代码中有任何错误,请告诉我,我会尽力修复它们.
The way it works is whenever you go to move, it moves the player, then looks to see if if collided with the block. If it did, the player moves in the opposite direction until is is satisfied that it isn't colliding any more.
Jumping is accomplished using velocity, hence Player.yvel
and GRAVITY
. GRAVITY
can be changed but if it it higher, jumps will be shorter and vice-versa.
Have a play around with the code and experiment with things like the blocks position (self.block = Block(225, 400, 50, 50)
) and the player's staring position (self.player = Player(0, 0)
)
If you do find any errors in the code, just let me know and I'll try to fix them.
这篇关于试图做一个“固体"在 pygame 中阻止但它不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!