Reputation:
Pygame I was wondering if any one knows how to swap maps as you touch or go over something.
here is my code:
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()
If the player
if you touch the scarecrow then you teleport to the next level.
Upvotes: 1
Views: 3025
Reputation: 100
if you hit an object or a specific x or y axis, you gotta change the background,
current_map = "map5.png"
if (Put code here for the touching of the object you want)
current_map = "Example_next_Map.png"
Or... You can make a new MAP, and you make it so when you hit an object you declare your current position as for example: PlayerX and PlayerY, and then you make the screen remove all of the objects and change "Map", declare a new map where you have X and y for objects and then you make every object disapear and spawn player in PlayerX and PlayerY and load the Code for the new Map like this:
this is 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()
THis is Map TWO!
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
character="boy.png" player=pygame.image.load(character).convert_alpha()
Set players X as PlayerX here and Player Y
Upvotes: 0
Reputation: 1494
the best way to do something like that (that supports multiple levels) is thinking that "bif" is the variable of the map. So after input check the location of the hero (x, y) and if it's the value you want then change the map to the next level. Here is the code:
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()
in case it helps you I'll write the "loadStage" as well
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
sorry I worked with python some years ago I might have some mistakes (probably with the language's rules) but I know how the logic works, I hope I explained everything clearly if not ask me!
Upvotes: 1
Reputation: 7906
If want to change the screen when the player walks over certain spots your going to need to know where those things are with some kind of map, I've edited your script to do a teleport from under the scarecrow as an example of the basic idea:
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()
As you see when we move to the second screen we are in the same x,y position as the previous screen:
Upvotes: 0