Python 皮加梅精灵会跳但不会走
我一直在研究Pygame Python 3.3.1版,现在我的精灵可以跳跃,但只能在任意方向行走几个像素。我真的很想继续写这段代码,因为我喜欢它的结构,而不是Main方法中的很多代码。有人能发现是什么导致我的雪碧卡住了吗Python 皮加梅精灵会跳但不会走,python,collision-detection,sprite,Python,Collision Detection,Sprite,我一直在研究Pygame Python 3.3.1版,现在我的精灵可以跳跃,但只能在任意方向行走几个像素。我真的很想继续写这段代码,因为我喜欢它的结构,而不是Main方法中的很多代码。有人能发现是什么导致我的雪碧卡住了吗 import pygame # Define some colors black = ( 0, 0, 0) white = ( 255, 255, 255) green = ( 0, 255, 0) red = ( 255,
import pygame
# Define some colors
black = ( 0, 0, 0)
white = ( 255, 255, 255)
green = ( 0, 255, 0)
red = ( 255, 0, 0)
class Player(pygame.sprite.Sprite):
def __init__(self, *groups):
super(Player, self).__init__(groups)
self.image = pygame.image.load('Images\player1.png')
self.rect = pygame.rect.Rect((50, 650), self.image.get_size())
self.resting = False
self.dy = 0 #dy represents change in y velocity
def update(self, dt, game):
last = self.rect.copy()
key = pygame.key.get_pressed()
if key[pygame.K_LEFT]:
self.rect.x -= 300 * dt
if key[pygame.K_RIGHT]:
self.rect.x += 300 * dt
#if key[pygame.K_UP]:
# self.rect.y -= 300 * dt
#if key[pygame.K_DOWN]:
# self.rect.y += 300 * dt
if self.resting and key[pygame.K_SPACE]:
self.dy = -500 #If space bar is pressed, increase velocity.
self.dy = min(400, self.dy + 40) #Speed capped at 400. Gravity set at 40.
self.rect.y += self.dy * dt
new = self.rect
self.resting = False
for cell in pygame.sprite.spritecollide(self, game.walls, False):
#self.rect = last
cell = cell.rect
if last.right <= cell.left and new.right > cell.left:
new.right = cell.left
if last.left >= cell.right and new.left < cell.right:
new.left = cell.right
if last.bottom <= cell.top and new.bottom > cell.top:
#if you hit something while jumping, stop.
self.resting = True
new.bottom = cell.top
self.dy = 0
if last.top >= cell.bottom and new.top < cell.bottom:
new.top = cell.bottom
self.dy = 0 #If you hit the floor while jumping, stop
class Game(object):
def main(self, screen):
clock = pygame.time.Clock()
dt = clock.tick(30)
#image = pygame.image.load('Images\player1.gif')
background = pygame.image.load('Images\_rec_bg.png')
sprites = pygame.sprite.Group()
self.player = Player(sprites)
self.walls = pygame.sprite.Group()
block = pygame.image.load('Images\dia_tile.png')
for x in range(0, 800, 20):
for y in range(0, 800, 20):
if x in (0, 800-20) or y in (0, 800-20):
wall = pygame.sprite.Sprite(self.walls)
wall.image = block
wall.rect = pygame.rect.Rect((x, y), block.get_size())
sprites.add(self.walls)
running = True
while running:
clock.tick(30) #run no more than 30 time per second
dt - clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT or \
(event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
running = False
#sprites.update()
#sprites.update(dt / 1000.)
#screen.fill(black)
sprites.update(dt / 1000., self)
#screen.blit(image, (320, 240)) #Transfer to video RAM
screen.blit(background, (0,0))
sprites.draw(screen)
pygame.display.flip() #Dispaly to Screen
if __name__ == '__main__':
pygame.init()
screen = pygame.display.set_mode((800, 800))
Game().main(screen)
我不确定,但看起来你只允许球员在空中跳跃时左右移动 如果我是对的,你需要让你的球员能够左右移动,即使是在self.resting=True的情况下 如果您这样做会发生什么:
if key[pygame.K_LEFT]:
self.rect.x -= 75
if key[pygame.K_RIGHT]:
self.rect.x += 75
这方面的问题是,玩家将从一侧移动到另一侧,但当你跳跃并按下右键或左键时,玩家将飞得到处都是,而不是进入稳定的跳跃并摔倒
因此,你需要弄清楚如何包含*dt来调节球员在空中的移动,同时让球员能够在地面上移动来控制球
尝试使用两套if语句:
if key[pygame.K_LEFT]:
self.rect.x -= 300 * dt
if key[pygame.K_RIGHT]:
self.rect.x += 300 *dt
if key[pygame.K_LEFT]and self.resting:
self.rect.x -= 50
if key[pygame.K_RIGHT]and self.resting:
self.rect.x += 50
第一个用于空中,第二个用于球员在地面时
试试看,告诉我是否有效
我在我的电脑上试过了,效果很好,但我可能使用了不同的艺术,这也可能是问题所在,因为它可能是在某个东西后面闪动,所以请检查所有东西
祝你好运 我尝试了你上面的建议,但得到了同样的结果。我想补充更多细节,但我无法在这条评论上加分,使我的文本格式正确,感觉像这样一个傻瓜。这确实表明我的照片可能是问题的一部分。我已经用完全不同的方法重写了代码,可以让我的精灵毫无问题地行走和跳跃。但我稍后会再讨论这个问题。是的,我对我的艺术进行了调整,效果很好,但当我将艺术更改为不同的东西时,它没有。因为我没有你的艺术,很抱歉我帮不了你