简体   繁体   English

如何在python平台游戏中制作另一个关卡

[英]how to make another level in python platform game

I want to make it so when you walk off the screen to the right it will enter level 2, but I have no clue on how to do this.我想让它当你从屏幕向右走时它会进入第 2 级,但我不知道如何做到这一点。

I also have a platform glitch but no one knows how to answer it on the post I made so I guess I'll just leave it because I can't fix it.我也有一个平台故障,但没有人知道如何在我发布的帖子中回答它,所以我想我会离开它,因为我无法修复它。

This is my code:这是我的代码:

import pygame

pygame.init()

bg = pygame.image.load('C:\Python34/2d/bg.png')
player1 = pygame.image.load('C:\Python34/2d/player.png')
screen = pygame.display.set_mode((800,600))

pygame.display.set_caption("use arows")

movex = 0

class player:

    def __init__(self ,x, y):
        self.x = x
        self.y = y
        self.width = 112
        self.height = 112
        self.velocity = 0
        self.falling = False
        self.onGround = False

    def jump(self):
        if(self.onGround == False):
            return

        self.velocity = 8
        self.onGround = False

    def detectCollisions(self,x1,y1,w1,h1,x2,y2,w2,h2):
        if (x2+w2>=x1>=x2 and y2+h2>=y1>=y2):
            return True
        elif (x2+w2>=x1+w1>=x2 and y2+h2>=y1>=y2):
            return True
        elif (x2+w2>=x1>=x2 and y2+h2>=y1+h1>=y2):
            return True
        elif (x2+w2>=x1+w1>=x2 and y2+h2>=y1+h1>=y2):
            return True    
        else:
            return False

    def update(self, gravity, blockList):
        if (self.velocity < 0):
            self.falling = True

        collision = False
        blockX,blockY =  0,0
        for block in blockList:

            collision = self.detectCollisions(self.x, self.y, self.width, self.height, block.x, block.y, block.width, block.height )
            if collision == True:
                blockx = block.x
                blocky = block.y
                break

        if(collision == True):
            if self.falling == True:
                self.falling = False
                self.onGround = True
                self.velocity = 0
                self.y = blocky - self.height

        if (self.onGround == False):
            self.velocity += gravity
        self.y -= self.velocity

    def render(self,screen):
        screen.blit(player1,(self.x,self.y))

class Block:
    def __init__ (self, x, y):
       self.x = x
       self.y = y
       self.width = 32
       self.height = 32

    def render(self,screen):
        pygame.draw.rect(screen,(9,203,27),(self.x, self.y, self.width, self.height))

gravity = -0.5

black = (0,0,0)
white = (255,255,255)
blue = (50,60,200)

clock = pygame.time.Clock()

player = player(0,0)

# 25 colums and 19 rows
level1 = [
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
]

blockList = []

for y in range (0,len(level1)):
    for x in range (0,len(level1[y])):
        if (level1[y][x] == 1):
            blockList.append(Block(x*32, y*32))

gameloop = True

while gameloop:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            gameloop = False

        if(event.type == pygame.KEYDOWN):
            if (event.key == pygame.K_RIGHT):
                movex = 5
            elif(event.key == pygame.K_LEFT):
                movex = -5
            elif (event.key == pygame.K_UP):
                player.jump()

        if(event.type == pygame.KEYUP):
            if (event.key == pygame.K_RIGHT):
                movex = 0
            elif(event.key == pygame.K_LEFT):
                movex = 0

    screen.fill(white)
    screen.blit(bg,(0,0))

    for block in blockList:
        block.render(screen)
    player.x += movex

    player.update(gravity, blockList)
    player.render(screen)
    clock.tick(60)

    pygame.display.update()

pygame.quit()

I store my levels in text files, but the way you have it set up will work.我将关卡存储在文本文件中,但您的设置方式将起作用。 Just test if the player is past the right edge of the map, (eg: if playerX > 1200 ) then reload the map as the second map, (basically, just have an active map that copies other maps onto it for active use) Then set the playerX to 0 or something.只需测试玩家是否越过地图的右边缘,(例如: if playerX > 1200 )然后将地图重新​​加载为第二张地图,(基本上,只需将其他地图复制到其上以供主动使用的活动地图即可)然后将 playerX 设置为 0 或其他值。

For my game, I have all of my maps stored as text files with them named World,X,Y.对于我的游戏,我将所有地图都存储为文本文件,它们名为 World,X,Y。 I load them with file = open(''+(str(World))+','+(str(X))+','+(str(Y))+'','r') (I don't care what people think of how I do my strings. :P) Then it it mostly automated.我用file = open(''+(str(World))+','+(str(X))+','+(str(Y))+'','r')加载它们(我不不在乎人们如何看待我如何处理我的字符串。:P) 然后它主要是自动化的。

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

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