简体   繁体   English

问:Pygame 如何让关卡随着玩家的移动左右滚动?

[英]Q:Pygame How Do I Make my level Scroll Side-To-Side As The Player Move?

Q:Pygame How Do I Make my level Scroll Side-To-Side As The Player Move?问:Pygame 如何让关卡随着玩家的移动左右滚动?

I am trying to figure out how can I make a side-scrolling view in pygame if i am moving right or left or Up want to portion of the level shown on the screen to shift follow the character Heres A Animation of my pygame: https://gyazo.com/636fef8a16198b24bc0496f566c57d9c我想弄清楚如果我向右或向左移动或向上移动,我如何才能在 pygame 中制作横向滚动视图,以跟随角色 Heres A Animation of my pygame: https: //gyazo.com/636fef8a16198b24bc0496f566c57d9c

And my Script is below:我的脚本如下:

pygame.init()


window = pygame.display.set_mode((500,500))
pygame.display.set_caption("GET LOAD OF THIES")


# Character class
class players(object):
    def __init__(self,x,y,height,width):
        self.x = x
        self.y = y
        self.height = height
        self.width = width
        self.isJump = False
        self.JumpCount = 10
        self.fall = 0
        self.speed = 5

# enemy class 1
class enemys(object):
    def __init__(self,cordx,cordy,cordheight,cordwidth):
        self.cordx = cordx
        self.cordy = cordy
        self.cordheight = cordheight
        self.cordwidth = cordwidth

# enemy class 2
class enemyss(object):
    def __init__(self,xs,ys,sheights,swidths):
        self.xs = xs
        self.ys = ys
        self.sheights = sheights
        self.swidths = swidths

class lava(object):
    def __init__(self,lavax,lavay,lavawidth,lavaheight):

        self.lavax = lavax
        self.lavay = lavay
        self.lavawidth = lavawidth
        self.lavaheight = lavaheight



# enemy class 3
class enemysss(object):
    def __init__(self,velx,vely,velheight,velwidth):
        self.velx = velx
        self.vely = vely
        self.velwidth = velwidth
        self.velheight = velheight


# Colors
NiceBlue = (0,214, 82)
NiceGreen = (214,0,82)

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

# Defininition for the class player/enemys
playerman = players(50,390,20,20)
enemy1 = enemys(150,390,100,10)
enemy2 = enemyss(320,320,100,10)
enemy3 = enemysss(120,250,100,10)
enemy4 = lava(0,459,500,40)


# Main loop
runninggame = True
while runninggame:
    # Frames per second
    clock.tick(FPS)
    # Exit Event
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            runninggame = False
        #Draw The enemys and Main Characte
    window.fill((0,0,0))
    player = pygame.draw.rect(window, (NiceGreen), (playerman.x,playerman.y,playerman.height,playerman.width))
    enemy = pygame.draw.rect(window, (NiceBlue), (enemy1.cordx,enemy1.cordy,enemy1.cordheight,enemy1.cordwidth))
    soenemy = pygame.draw.rect(window, (NiceBlue), (enemy2.xs,enemy2.ys,enemy2.sheights,enemy2.swidths))
    dudeenemy = pygame.draw.rect(window, (NiceBlue), (enemy3.velx,enemy3.vely,enemy3.velheight,enemy3.velwidth))
    thisanotherenemy = pygame.draw.rect(window, (230,4,231), (enemy4.lavax,enemy4.lavay,enemy4.lavawidth,enemy4.lavaheight))


    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        playerman.x -= playerman.speed
    if keys[pygame.K_RIGHT]:
        playerman.x += playerman.speed
    if not playerman.isJump:

        playerman.y += playerman.fall
        playerman.fall += 1
# ----------------------------------------------------- # enem1 collisio
# both of my 2 enemy squares collisions push me back when ever I Jump on the top of them on there sides but when I jump on the middle of of both of them it seems to work if I just want it so when I jump on both of my squares I just don't get pushed back 
        player.topleft = (playerman.x, playerman.y)
        collide = False
        if player.colliderect(enemy):
            collide = True
            playerman.y = enemy.top - player.height
            if player.right > enemy.left and  player.left < enemy.left - player.width:
                playerman.x = enemy.left - player.width
            if player.left < enemy.right and  player.right > enemy.right + player.width:
                playerman.x = enemy.right

        if player.colliderect(soenemy):
            collide = True
            playerman.y = soenemy.top - player.height
            if playerman.y > soenemy.left and player.left < enemy.left - player.width:
                playerman.x = soenemy.left  - player.width
            if player.left < soenemy.left and player.right > soenemy.right + player.width:
                playerman.x = soenemy.right
        if player.colliderect(dudeenemy):
            collide = True
            playerman.y = dudeenemy.top - player.height
            if playerman.y > dudeenemy.left and player.left < dudeenemy.left - player.width:
                playerman.x = dudeenemy.left  - player.width
            if player.left < dudeenemy.left and player.right > dudeenemy.right + player.width:
                playerman.x = dudeenemy.right
        if player.colliderect(thisanotherenemy):
            collide = True
            playerman.y = thisanotherenemy.top - player.height
            if playerman.y > thisanotherenemy.left and player.left < thisanotherenemy.left - player.width:
                playerman.x = thisanotherenemy.left  - player.width
            if player.left < thisanotherenemy.left and player.right > thisanotherenemy.right + player.width:
                playerman.x = thisanotherenemy.right





        if player.bottom >= 500:
            collide = True
            playerman.isJump = False
            playerman.JumpCount = 10
            playerman.y = 500 - player.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.JumpCount = 10
            playerman.isJump = False



        # update winodw event
    pygame.display.update()
# quit event
pygame.quit()

Lets start by by making the code as simple as possible so its easier to add scrolling让我们从使代码尽可能简单开始,以便更容易添加滚动

First, you have a separate class for each object, that is unnecessary, the way classes work is you can create multiple instances off one class, so instead of having a class for every enemy, you can have one.首先,每个 object 都有一个单独的 class,这是不必要的,类的工作方式是您可以从一个 class 创建多个实例,因此您可以拥有一个,而不是每个敌人都有一个 class。

# enemy class 1
class enemys(object):
    def __init__(self,x,y,height,width):
        self.x = x
        self.y = y
        self.height = height
        self.width = width
# Defininition for the class player/enemys
playerman = players(50,390,20,20)
enemy1 = enemys(150,390,100,10)
enemy2 = enemys(320,320,100,10)
enemy3 = enemys(120,250,100,10)
enemy4 = enemys(0,459,500,40)

you also have another variable for each enemies rect.每个敌人矩形还有另一个变量。 lets move it to the class. In fact, lets move all the drawing to the class让我们将它移动到 class。事实上,让我们将所有绘图移动到 class

# enemy class 1
class enemys(object):
    def __init__(self,x,y,height,width):
        self.x = x
        self.y = y
        self.height = height
        self.width = width
        self.rect = pygame.Rect(x,y,width,height)

    def draw(self):
        pygame.draw.rect(window, (NiceBlue), self.rect) 
playerman.draw()
enemy1.draw()
enemy2.draw()
enemy3.draw()
enemy4.draw()

i did notice that lava has a different colour, so lets make that an argument for creating the enemy我确实注意到熔岩有不同的颜色,所以让我们以此作为创造敌人的理由

# enemy class 1
class enemys: #you dont need an (object) only in python2
    def __init__(self,x,y,height,width,color):
        self.x = x
        self.y = y
        self.height = height
        self.width = width
        self.rect = pygame.Rect(x,y,width,height)
        self.color = color

    def draw(self):
        pygame.draw.rect(window, self.color, self.rect)
enemy1 = enemys(150,390,100,10, NiceBlue)
enemy2 = enemys(320,320,100,10, NiceBlue)
enemy3 = enemys(120,250,100,10, NiceBlue)
enemy4 = enemys(0,459,500,40, (230,4,231))

Now to clean up the collision, it works, but you dont need all of those if statements, you can use a loop.现在清理碰撞,它起作用了,但你不需要所有这些 if 语句,你可以使用循环。 lets also fix up everywhere there was a playe and change it to playerman.rect, and the same with enemy让我们也修复所有有 playe 的地方并将其更改为 playerman.rect,与敌人相同

enemies = [enemy1,enemy2,enemy3,enemy4]
collide = False
    for enemy in enemies:
        if playerman.rect.colliderect(enemy.rect):
            collide = True
            playerman.y = enemy.rect.top - playerman.height + 1 #had to add 1 to look smooth
            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  #break so dont check every object

    if playerman.rect.bottom >= 500:
        collide = True
        playerman.isJump = False
        playerman.JumpCount = 10
        playerman.y = 500 - playerman.height

Now Lets make the scroll, to scroll, you need to move everything on the screen once the player moves near the edge of the screen现在让我们滚动,要滚动,您需要在玩家移动到屏幕边缘附近时移动屏幕上的所有内容

if keys[pygame.K_LEFT]:
    playerman.x -= playerman.speed
    if playerman.x < 100:                  #if the player is close to the edge
        playerman.x += playerman.speed     #move everything the other direction
        for enemy in enemies:
            enemy.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     

and the same for the jump和跳跃一样

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

Now i chose numbers to be close to the edge, you should replace the 400 , 100 and 250 to variables so you can change them.现在我选择靠近边缘的数字,您应该将400100250替换为变量,以便您可以更改它们。

Here is the full code:这是完整的代码:

import pygame

pygame.init()


window = pygame.display.set_mode((500,500))
pygame.display.set_caption("GET LOAD OF THIES")


# Character class
class players(object):
    def __init__(self,x,y,height,width):
        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.rect = pygame.Rect(x,y,width,height)

    #draw the player
    def draw(self):
        self.rect.topleft = (self.x, self.y)
        pygame.draw.rect(window, (NiceGreen), self.rect)

# enemy class 1
class enemys(object):
    def __init__(self,x,y,width,height,color):
        self.x = x
        self.y = y
        self.height = height
        self.width = width
        self.rect = pygame.Rect(x,y,width,height)
        self.color = color

    #draw the enemy
    def draw(self):
        #update the rect with new positions
        self.rect.topleft = (self.x, self.y)
        pygame.draw.rect(window, self.color, self.rect)



# Colors
NiceBlue = (0,214, 82)
NiceGreen = (214,0,82)

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

# Defininition for the class player/enemys
playerman = players(50,390,20,20)
enemy1 = enemys(150,390,100,10, NiceBlue)
enemy2 = enemys(320,320,100,10, NiceBlue)
enemy3 = enemys(120,250,100,10, NiceBlue)
enemy4 = enemys(0,459,500,40, (230,4,231))


enemies = [enemy1,enemy2,enemy3,enemy4]

# Main loop
runninggame = True
while runninggame:
    # Frames per second
    clock.tick(FPS)
    # Exit Event
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            runninggame = False
        #Draw The enemys and Main Characte
    window.fill((0,0,0))
    playerman.draw()
    enemy1.draw()
    enemy2.draw()
    enemy3.draw()
    enemy4.draw()

    #check for scroll up
    if playerman.y < 250:
        playerman.y += 1
        for enemy in enemies:
            enemy.y += playerman.speed   
#-----------------------------------------------------put here---------------
    #check for scroll down
    if playerman.y > 450: #somewhere close to the bottom of the screen
        playerman.y -= playerman.fall  #reverse direction
        for enemy in enemies:
            enemy.y -= playerman.fall
#---------------------------------------------------------------------------

    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        playerman.x -= playerman.speed
        #check to scroll left
        if playerman.x < 100:
            playerman.x += playerman.speed #move player back so doesnt go off screen
            for enemy in enemies:          #move everything in other direction
                enemy.x += playerman.speed
    if keys[pygame.K_RIGHT]:
        playerman.x += playerman.speed
        #check to scroll right
        if playerman.x > 400:             
            playerman.x -= playerman.speed
            for enemy in enemies:
                enemy.x -= playerman.speed        
    if not playerman.isJump:

        playerman.y += playerman.fall
        playerman.fall += 1
# ----------------------------------------------------- # enem1 collisio
# both of my 2 enemy squares collisions push me back when ever I Jump on the top of them on there sides but when I jump on the middle of of both of them it seems to work if I just want it so when I jump on both of my squares I just don't get pushed back 
        collide = False
        for enemy in enemies:
            if playerman.rect.colliderect(enemy.rect):
                collide = True
                playerman.y = enemy.rect.top - playerman.height + 1 #had to add 1 to look smooth
                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

        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.JumpCount = 10
            playerman.isJump = False



        # update winodw event
    pygame.display.update()
# quit event
pygame.quit()

#check for scroll up
    if playerman.y > 450: #somewhere close to the bottom of the screen
        playerman.y -= playerman.fall  #reverse direction
        for enemy in enemies:
            enemy.y -= playerman.fall

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM