Pygame传送白方块如何修复?

问题描述 投票:0回答:1

我做了一个传送方块,如果我的玩家碰到它,它就会传送到另一个位置。

问题是我的屏幕,当我滚动到其他地方时,它没有把我传送到我想要的地方。示例-视频. 如果我在滚动位置,我可以传送到我想要的位置,但是如果我在其他地方滚动并尝试传送,它就不会把我传送到那里。

我的传送脚本


if playerman.rect.colliderect(teleport.rect):
      for enemy in enemies:
           if playerman.rect.centerx:
                 playerman.x = 50
                 playerman.y = 150

完整代码

# import module
import pygame
pygame.init()

# window
window = pygame.display.set_mode((500,500))
pygame.display.set_caption("YELOLL")

# draw the player
class player:
    def __init__(self,x,y,height,width,color):
        self.x = x
        self.y = y
        self.height = height
        self.width = width
        self.isJump = False
        self.JumpCount = 10
        self.fall = 0
        self.speed = 5
        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)
# enemy
class enemy:
    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)


# coins
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()

# colors
Green = (0,255,2)
white = (255,255,255)
Yellow = (248,255,0)
Blue = (0,255,201)

# define player and enemy
playerman = player(50,390,30,30, Blue)
enemy1 = enemy(180,390,150,10, Green)
enemy2 = enemy(300,290,150,10, Green)
enemy3 = enemy(70,250,150,10, Green)
enemy4 = enemy(-10000,490,150000,50, white)
teleport = enemy(390,390,40,80, white)

enemies = [enemy1,enemy2,enemy3,enemy4]

# define coins
coin1 = coin(230,370,20,20, Yellow)
coin2 = coin(350,270,20,20, Yellow)
coin3 = coin(150,230,20,20, Yellow)
coin4 = coin(250,450,20,20, Yellow)

Coins_list = [coin1,coin2,coin3,coin4,teleport]


# main loop
runninggame = True
while runninggame:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            runninggame = False
    clock.tick(FPS)
    window.fill((0,0,0))
    teleport.draw()
    window.blit(text,textRect)
    playerman.draw()
    for enemy in enemies:
        enemy.draw()
    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 > 450:
            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.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
                break
            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.colliderect(teleport.rect):
                for enemy in enemies:
                    if playerman.rect.centerx:
                        playerman.x = 50
                        playerman.y = 150

        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.4
            playerman.JumpCount -= 1
        else:
            playerman.isJump = False
            playerman.JumpCount = 10


    pygame.display.update()
pygame.quit()
python pygame
1个回答
1
投票

好吧,首先,你需要用卷轴移动传送方块,这将使它更接近你想要的。

不要把它添加到硬币列表中,因为它不是硬币

Coins_list = [coin1,coin2,coin3,coin4]

要滚动它,做同样的事情,其他的一切

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

你也可以尝试使用一个变量来滚动,这样你就可以重置所有的东西,它也可以清理代码,现在你得到了很多不同的对象。

让我们为x和y创建两个变量。

scrollX = 0
scrollY = 0

现在用新的滚动方式来绘制对象

def draw(self):
    self.rect.topleft = (self.x + scrollX,self.y + scrollY) #i did this in all classes

现在让我们把滚动从向后移动所有的东西改为改变滚动变量

if playerman.y + scrollY < 250:
    scrollY += 1

现在当玩家进入传送方块时,将scrollX重置为0。

if playerman.rect.colliderect(teleport.rect):
        playerman.x = 50
        playerman.y = 150
        scrollX = 0
if playerman.y + scrollY > 450:
    scrollY -= playerman.fall

if keys[pygame.K_LEFT]:
    playerman.x -= playerman.speed
    if playerman.x + scrollX < 100:
        scrollX += playerman.speed

if keys[pygame.K_RIGHT]:
    playerman.x += playerman.speed
    if playerman.x + scrollX > 450:
        scrollX -= playerman.speed

它的视频

完整代码:

# import module
import pygame
pygame.init()

# window
window = pygame.display.set_mode((500,500))
pygame.display.set_caption("YELOLL")

scrollX = 0
scrollY = 0

# draw the player
class player:
    def __init__(self,x,y,height,width,color):
        self.x = x
        self.y = y
        self.height = height
        self.width = width
        self.isJump = False
        self.JumpCount = 10
        self.fall = 0
        self.speed = 5
        self.color = color
        self.rect = pygame.Rect(x,y,height,width)
    def draw(self):
        self.rect.topleft = (self.x + scrollX,self.y + scrollY)
        pygame.draw.rect(window, self.color, self.rect)
# enemy
class enemy:
    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 + scrollX,self.y + scrollY)
        pygame.draw.rect(window, self.color, self.rect)


# coins
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 + scrollX,self.y + scrollY)
        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()

# colors
Green = (0,255,2)
white = (255,255,255)
Yellow = (248,255,0)
Blue = (0,255,201)

# define player and enemy
playerman = player(50,390,30,30, Blue)
enemy1 = enemy(180,390,150,10, Green)
enemy2 = enemy(300,290,150,10, Green)
enemy3 = enemy(70,250,150,10, Green)
enemy4 = enemy(-10000,490,150000,50, white)
teleport = enemy(390,390,40,80, white)

enemies = [enemy1,enemy2,enemy3,enemy4]

# define coins
coin1 = coin(230,370,20,20, Yellow)
coin2 = coin(350,270,20,20, Yellow)
coin3 = coin(150,230,20,20, Yellow)
coin4 = coin(250,450,20,20, Yellow)

Coins_list = [coin1,coin2,coin3,coin4]


# main loop
runninggame = True
while runninggame:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            runninggame = False
    clock.tick(FPS)
    window.fill((0,0,0))
    teleport.draw()
    window.blit(text,textRect)
    playerman.draw()
    for enemy in enemies:
        enemy.draw()
    for coin in Coins_list:
        coin.draw()



    if playerman.y + scrollY < 250:
        scrollY += 1
        #playerman.y += 1
        #for enemy in enemies:
            #enemy.y += playerman.speed
        #for coin in Coins_list:
            #coin.y += playerman.speed
        #teleport.y += playerman.speed
    if playerman.y + scrollY > 450:
        scrollY -= playerman.fall
        print(playerman.y - scrollY)
        #playerman.y -= playerman.fall
        #for enemy in enemies:
            #enemy.y -= playerman.fall
        #for coin in Coins_list:
            #coin.y -= playerman.fall
        #teleport.y -= playerman.fall

    keys = pygame.key.get_pressed()



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




    if keys[pygame.K_RIGHT]:
        playerman.x += playerman.speed
        if playerman.x + scrollX > 450:
            #playerman.x -= playerman.speed
            scrollX -= playerman.speed
            #for enemy in enemies:
                #enemy.x -= playerman.speed
            #for coin in Coins_list:
                #coin.x -= playerman.speed
            #teleport.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.y = enemy.rect.top - playerman.height + 1 - scrollY
                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
                break
        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.colliderect(teleport.rect):
            playerman.x = 50
            playerman.y = 150
            scrollX = 0

        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.4
            playerman.JumpCount -= 1
        else:
            playerman.isJump = False
            playerman.JumpCount = 10


    pygame.display.update()
pygame.quit()
© www.soinside.com 2019 - 2024. All rights reserved.