首页 > 解决方案 > 问;如何解决移动平台问题?在pygame中

问题描述

所以我想让我的平台在屏幕范围内左右移动所以我这样做了,它只是向右移动不停

    for enemy in enemies:
        if moving.x  > 10:
            moving.x += 0.5
        else:
            if moving.x != 10:
                moving.x -= 0.5
    for enemy in enemies:
        if moving.y  < 10:
            moving.y -= 0.5
        else:
            if moving.y < 10:
                moving.y -= 0.5

我的完整代码

import pygame
pygame.init()


# screen
windowheight = 500
windowwidth = 500
window = pygame.display.set_mode((windowheight,windowwidth))
pygame.display.set_caption("platformer")

# colors
whitecolor = (255, 255, 255)
NiceLime = (0,255,0)
NiceYellow =(255,255,0)
NiceGreen = (145,245,105)


# player class
class player:
    def __init__(self,x,y,height,width,color):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.color = color
        self.isJump = False
        self.JumpCount = 10
        self.speed = 5
        self.fall = 0
        self.rect = pygame.Rect(x,y,height,width)
    def draw(self):
        self.rect.topleft = (self.x,self.y)
        pygame.draw.rect(window, self.color, self.rect)



# enemy class
class enemy:
    def __init__(self,x,y,height,width,color):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.color = color
        moving = False
        self.rect = pygame.Rect(x,y,height,width)
    def draw(self):
        self.rect.topleft = (self.x,self.y)
        pygame.draw.rect(window, self.color, self.rect)

# coin class
class coin:
    def __init__(self,x,y,height,width,color):
        self.x = x
        self.y = y
        self.height = height
        self.width = width
        self.color = color

        self.rect = pygame.Rect(x,y,height,width)
    def draw(self):
        self.rect.topleft = (self.x,self.y)
        pygame.draw.rect(window, self.color, self.rect)


font = pygame.font.Font('freesansbold.ttf', 32)
score = 0
text = font.render('Score = ' + str(score), True, (255, 255, 255))
textRect = text.get_rect()  
textRect.center = (100, 40)



# FPS
FPS  = 60
clock = pygame.time.Clock()

# define player and enemy class
playerman = player(50,390,30,30, NiceLime)
enemy1 = enemy(190,380,150,10, NiceGreen)
enemy2 = enemy(340,280,150,10, NiceGreen)
enemy3 = enemy(70,250,150,10, NiceGreen)
enemy4 = enemy(-10000,450,9999999,50, NiceGreen)
moving = enemy(150,200,150,10, NiceGreen)
enemies = [enemy1,enemy2,enemy3,enemy4]

# define coins
coin1 = coin(200,360,20,20, NiceYellow)
coin2 = coin(230,360,20,20, NiceYellow)
coin3 = coin(250,360,20,20, NiceYellow)
coin3 = coin(260,360,20,20, NiceYellow)
coin4 = coin(290,360,20,20, NiceYellow)
coin5 = coin(410,240,20,20, NiceYellow)
coin6 = coin(410,210,20,20, NiceYellow)
coin7 = coin(140,180,20,20, NiceYellow)
coin8 = coin(140,210,20,20, NiceYellow)

Coins_list = [coin1,coin2,coin3,coin4,coin5,coin6,coin7,coin8]


# main loop
runninggame = True
while runninggame:
    clock.tick(FPS)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            runninggame = False

#--------------------------------moving platform

    for enemy in enemies:
        if moving.x  > 10:
            moving.x += 0.5
        else:
            if moving.x != 10:
                moving.x -= 0.5
    for enemy in enemies:
        if moving.y  < 10:
            moving.y -= 0.5
        else:
            if moving.y < 10:
                moving.y -= 0.5

#--------------------------------------------          





    window.fill((0,0,0))
    moving.draw()
    playerman.draw()
    for enemy in enemies:
        enemy.draw()
    window.blit(text,textRect)
    for coin in Coins_list:
        coin.draw()

    if playerman.y < 250:
        playerman.y += 1
        for enemy in enemies:
            enemy.y += playerman.speed
        for coin in Coins_list:
            coin.y += playerman.speed

    if playerman.y > 450:
        playerman.y -= playerman.fall
        for enemy in enemies:
            enemy.y -= playerman.fall
        for coin in Coins_list:
            coin.y -= playerman.fall

    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        playerman.x -= playerman.speed
        if playerman.x < 100:
            playerman.x += playerman.speed
            for enemy in enemies:
                enemy.x += playerman.speed
            for coin in Coins_list:
                coin.x += playerman.speed




    if keys[pygame.K_RIGHT]:
        playerman.x += playerman.speed
        if playerman.x > 400:
            playerman.x -= playerman.speed
            for enemy in enemies:
                enemy.x -= playerman.speed
            for coin in Coins_list:
                coin.x -= playerman.speed



    if not playerman.isJump:
        playerman.y += playerman.fall
        playerman.fall += 1
        collide = False
        for enemy in enemies:
            if playerman.rect.colliderect(enemy.rect):
                collide = True
                playerman.isJump = False
                playerman.y = enemy.rect.top - playerman.height + 1
                if playerman.rect.right > enemy.rect.left and playerman.rect.left < enemy.rect.left - playerman.width:
                    playerman.x = enemy.rect.left - player.width
                if playerman.rect.left < enemy.rect.right and playerman.rect.right > enemy.rect.right + playerman.width:
                    playerman.x = enemy.rect.right
        for i in range(len(Coins_list)-1,-1,-1):
            if playerman.rect.colliderect(Coins_list[i].rect):
                del Coins_list[i]
                score += 1
                text = font.render('Score = ' + str(score), True, (255, 255, 255))
                textRect = text.get_rect()  
                textRect.center = (100, 40)    
        if playerman.rect.bottom >= 500:
            collide = True
            playerman.isJump = False
            playerman.JumpCount  = 10
            playerman.y = 500 - playerman.height


        if collide:
            if keys[pygame.K_SPACE]:
                playerman.isJump = True
            playerman.fall = 0
    else:
        if playerman.JumpCount > 0:
            playerman.y -=(playerman.JumpCount*abs(playerman.JumpCount))*0.3
            playerman.JumpCount -= 1
        else:
            playerman.isJump = False
            playerman.JumpCount = 10



    pygame.display.update()

pygame.quit()


















































标签: pythonpygame

解决方案


在第一个循环中。然后你if moving.x > 10向右移动 0.5。这将是问题所在,因为它始终高于 10。您需要的是移动的速度/方向,然后在到达某个点时反转方向。

class enemy:
    def __init__(self,x,y,height,width,color):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.color = color
        moving = False
        self.veloctity= [0.5,0.5] #this is direction and speed
        self.rect = pygame.Rect(x,y,height,width)

您也没有将移动添加到敌人列表中,因此玩家不能碰撞(只是抬头),并且您循环敌人但只移动一个敌人,因此您moving每帧移动 4 次,所以如果您将其取出循环,它移动得非常慢,那是因为你每帧移动它 0.5 而不是 0.5*4 次

现在您可以检查平台是否需要转身并反转速度

moving.x += moving.velocity[0] #move on x axis
moving.y += moving.velocity[1] #move on y axis
if moving.x < 10 or moving.x > 400: #if outside moving area
    moving.velocity[0] *= -1 #reverse direction
if moving.y < 10 or moving.y > 400: #if outside moving area
    moving.velocity[1] *= -1 #reverse direction

现在,只需将 10 和 400 更改为您希望平台移动的位置,它将在这些点之间移动


推荐阅读