2013-11-03 27 views
1

Pygame我想知道如果任何人知道如何交換地圖,當你觸摸或過去的東西。Pygame傳送

這裏是我的代碼:

import pygame, sys 
from pygame.locals import * 

pygame.init() 

size = width, height = 1276,650 
screen = pygame.display.set_mode(size) 
r = 0 
bif = pygame.image.load("map5.png") 
pygame.display.set_caption("Pygame 2D RPG !") 
x,y=0,0 
movex, movey=0,0 
character="boy.png" 
player=pygame.image.load(character).convert_alpha() 
while True: 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      pygame.quit() 
      sys.exit() 
     if event.type==KEYDOWN: 
      if event.key==K_a: 
       movex=-1 
      elif event.key==K_d: 
       movex=+1 
      elif event.key==K_w: 
       movey=-1 
      elif event.key==K_s: 
       movey=+1 
     if event.type==KEYUP: 
      if event.key==K_a: 
       movex=0 
      elif event.key==K_d: 
       movex=0 
      elif event.key==K_w: 
       movey=0 
      elif event.key==K_s: 
       movey=0 

x+=movex 
y+=movey 

screen.fill((r,0,0)) 
screen.blit(bif,(0,0)) 
screen.blit(player,(x,y)) 
pygame.display.flip() 

My map file.

如果玩家Player

enter image description here

如果觸摸稻草人那麼你瞬移到一個新的水平。

enter image description here

+0

首先,你需要編寫一些代碼來表示的地圖,有一類什麼的。 – rlms

回答

1

如果想改變屏幕當玩家走到某個點您將需要知道這些東西有某種地圖,我已經編輯你的腳本由下做一個瞬移稻草人爲基本理念的一個例子:

import pygame, sys 
from pygame.locals import * 

pygame.init() 

size = width, height = 1276,650 
screen = pygame.display.set_mode(size) 
r = 0 
current_map = "map5.png" 
bif = pygame.image.load(current_map) 
pygame.display.set_caption("Pygame 2D RPG !") 
x,y=0,0 
movex, movey=0,0 
character="boy.png" 
player=pygame.image.load(character).convert_alpha() 

while True: 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      pygame.quit() 
      sys.exit() 
     if event.type==KEYDOWN: 
      if event.key==K_a: 
       movex=-1 
      elif event.key==K_d: 
       movex=+1 
      elif event.key==K_w: 
       movey=-1 
      elif event.key==K_s: 
       movey=+1 
     if event.type==KEYUP: 
      if event.key==K_a: 
       movex=0 
      elif event.key==K_d: 
       movex=0 
      elif event.key==K_w: 
       movey=0 
      elif event.key==K_s: 
       movey=0 

    x+=movex 
    y+=movey 
    print x,y 
    if x in range(680,702) and y in range(377,403): # This is the location of the tile. 
     bif = pygame.image.load("map6.png") 
    screen.fill((r,0,0)) 
    screen.blit(bif,(0,0)) 
    screen.blit(player,(x,y)) 
    pygame.display.flip() 

next to scare crow

正如你看到的,當我們移動到第二個屏幕中,我們都在相同的x,y座標爲上一屏幕:

on section screen

1

做這樣的事情(支持多級別)被認爲「BIF」是圖的變量的最佳方式。因此,在輸入檢查英雄(x,y)的位置後,如果它是您想要的值,則將地圖更改爲下一級。下面是代碼:

while True: 
for event in pygame.event.get(): 
    if event.type == pygame.QUIT: 
     pygame.quit() 
     sys.exit() 
    if event.type==KEYDOWN: 
     if event.key==K_a: 
      movex=-1 
     elif event.key==K_d: 
      movex=+1 
     elif event.key==K_w: 
      movey=-1 
     elif event.key==K_s: 
      movey=+1 
    if event.type==KEYUP: 
     if event.key==K_a: 
      movex=0 
     elif event.key==K_d: 
      movex=0 
     elif event.key==K_w: 
      movey=0 
     elif event.key==K_s: 
      movey=0 

x+=movex 
y+=movey 

#If you want hero to be on specific location - 100 and 50 are examples 
if x == 100 and y == 50: 
    bif = pygame.image.load("nextMap.png") 
#If you want hero to be on a specific area 
if x >= 100 and x < 150 and y >= 50 and y < 100: 
    bif = pygame.image.load("nextMap.png") 
#If you plan on making multiple levels you can try something like this 
if x == 100 and y == 50: 
    stage += 1 
    big = loadStage(stage) 
    #Where stage is the number of the currentLevel 
    #and loadStage() is a method that according to the stage returns the currect stage 
#If you want you can also reset the x and y values to 0 or the starting position you want 

screen.fill((r,0,0)) 
screen.blit(bif,(0,0)) 
screen.blit(player,(x,y)) 
pygame.display.flip() 

的情況下,它可以幫助你,我會寫「loadStage」以及

def loadStage(stageNumber): 
    if stageNumber == 1: 
     return pygame.image.load("map1.png") 
    if stageNumber == 2: 
     return pygame.image.load("map2.png") 
    #Make it as long as you want 

對不起,我與Python工作若干年前,我可能有一些錯誤(可能與語言的規則),但我知道邏輯是如何工作的,我希望如果不問我,我會清楚地解釋一切。

0

如果你打的對象或特定的X或Y軸,你必須改變的背景下,

current_map = "map5.png" 

if (Put code here for the touching of the object you want) 
current_map = "Example_next_Map.png" 

或者......你可以讓新的地圖,你可以做的,所以當你打的對象你聲明你的當前位置,例如:PlayerX和PlayerY,然後你讓屏幕移除所有的對象,並改變「地圖」,聲明一個新的地圖,你有X和Y的對象,然後你讓每個對象消失並在PlayerX和PlayerY中產生播放器,並像這樣加載新映射的代碼:

這是Map ONE!

bif = pygame.image.load("map5.png") 
pygame.display.set_caption("Pygame 2D RPG !") 
x,y=0,0 
movex, movey=0,0 
character="boy.png" 
player=pygame.image.load(character).convert_alpha() 

這是地圖二!

bif = pygame.image.load("NEWMAP.png") 
pygame.display.set_caption("Pygame 2D RPG !") 
x,y=0,0 
movex, movey=0,0 

This is code example for changing Map 

if TouchOfObject 
bif = pygame.image.load("NEWMAP.png") 
pygame.display.set_caption("Pygame 2D RPG !") 
x,y=0,0 
movex, movey=0,0 

字符= 「boy.png」 玩家= pygame.image.load(字符)。convert_alpha()

設置玩家X作爲PlayerX的位置和遊戲者Y