2015-10-11 195 views
-4

另一個層面我想使它所以當你走下屏幕就會進入2級的權利,但我對如何做到這一點沒有任何線索。如何使蟒蛇平臺遊戲

我也有一個平臺故障,但沒有人知道該怎麼回答它在我做了,所以我想我就離開它,因爲我不能修復它的帖子。

這是我的代碼:

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() 
+0

爲了做到這一點,您可能需要改進代碼的設計和結構。具體而言,您需要在某種變量中存儲每個級別的佈局。制定一個特定的數據結構來存儲每個級別將是一個非常好的主意。 – shuttle87

回答

0

我存儲我在文本文件中的水平,但你有它設置的工作方式。只需測試玩家是否經過地圖的右邊緣(例如:if playerX > 1200),然後將地圖重新​​加載爲第二張地圖(基本上,只需要一張活動地圖將其他地圖複製到其上以供活動使用)然後將玩家X爲0或某物。

對於我的比賽,我有我所有的存儲與他們的文本文件命名爲世界,X,Y地圖。 我用file = open(''+(str(World))+','+(str(X))+','+(str(Y))+'','r')加載它們(我不在乎人們怎麼看我的字符串:P)然後它大部分是自動的。