T: Pygame Bagaimana Saya Membuat Level Saya Menggulir Sisi-Ke-Sisi Saat Pemain Bergerak?

Saya mencoba mencari cara bagaimana saya bisa membuat tampilan gulir samping di pygame jika saya bergerak ke kanan atau kiri atau Atas ingin bagian dari level yang ditampilkan di layar untuk bergeser, ikuti karakternya Inilah Animasi pygame saya: https://gyazo.com/636fef8a16198b24bc0496f566c57d9c

Dan Script saya ada di bawah ini:

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()
0
Habib Ismail 5 Mei 2020, 04:07

1 menjawab

Jawaban Terbaik

Mari kita mulai dengan membuat kode sesederhana mungkin sehingga lebih mudah untuk menambahkan pengguliran

Pertama, Anda memiliki kelas terpisah untuk setiap objek, yang tidak perlu, cara kerja kelas adalah Anda dapat membuat beberapa instance dari satu kelas, jadi alih-alih memiliki kelas untuk setiap musuh, Anda dapat memilikinya.

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

Anda juga memiliki variabel lain untuk setiap musuh rect. mari kita pindahkan ke kelas. Bahkan, mari kita pindahkan semua gambar ke kelas

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

Saya memang memperhatikan bahwa lava memiliki warna yang berbeda, jadi mari kita jadikan itu argumen untuk menciptakan musuh

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

Sekarang untuk membersihkan tabrakan, ini berfungsi, tetapi Anda tidak memerlukan semua pernyataan if itu, Anda dapat menggunakan loop. mari kita perbaiki juga di mana-mana ada playe dan ubah ke playerman.rect, dan sama dengan musuh

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

Sekarang Mari membuat gulir, untuk menggulir, Anda perlu memindahkan semua yang ada di layar begitu pemain bergerak di dekat tepi layar

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     

Dan hal yang sama untuk lompat

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

Sekarang saya memilih angka untuk menjadi dekat dengan tepi, Anda harus mengganti 400, 100 dan 250 ke variabel sehingga Anda dapat mengubahnya.

Berikut kode lengkapnya:

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
2
The Big Kahuna 8 Mei 2020, 01:25