试图让A"固体"块pygame的,但它不工作 [英] Trying to make a "solid" block in pygame but it doesnt work
问题描述
希望你能帮助我, 我一直在努力奋斗着这个问题的最后几天。 我想打一个实块我的2D平台游戏的游戏,我的工作,但是当我进入了实挡我传送到对方或块的底部,我不知道为什么,希望你知道为什么 因此,这里是我的code:
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
非常感谢! :)
Thanks alot! :)
推荐答案
很抱歉,我的code不是基于关闭的事实,我真的只是把它一起在过去的5分钟,以便它不很pretty的,但在这里它是:
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
在底部附近。
near the bottom.
它的工作方式是,只要你去移动,它移动播放器,然后查找,看是否是否相撞块。如果有,在相反的方向,直到玩家移动确信它不会碰撞了。
跳使用速度,从而完成了 Player.yvel
和重力
。 重力
是可以改变的,但如果它更高,跳跃会更短,反之亦然。
周围有一出戏与code和尝试像块位置( self.block =块(225,400,50,50)
)和玩家的凝视位置( self.player =播放器(0,0)
)
如果您发现在code任何错误,只是让我知道,我会尽力解决这些问题。
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.
这篇关于试图让A&QUOT;固体&QUOT;块pygame的,但它不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!