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()
如果玩家
如果你触摸稻草人,那么你就会传送到一个新的水平。
答案 0 :(得分: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一起工作过,我可能会遇到一些错误(可能是语言规则),但我知道逻辑是如何工作的,我希望如果不问我,我会清楚地解释一切!
答案 1 :(得分:0)
如果想要在玩家走过某些地点时更改屏幕,你需要知道某些地图的位置,我已经编辑了你的脚本,以便在稻草人的下面进行传送作为例子。基本理念:
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()
正如您所看到的那样,当我们移动到第二个屏幕时,我们处于与上一个屏幕相同的x,y位置:
答案 2 :(得分: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"
或者......你可以创建一个新的MAP,当你点击一个对象时就可以这样做,你声明当前的位置,例如:PlayerX和PlayerY,然后你让屏幕移除所有的对象并改变&#34; Map&#34;,声明一个新的地图,你有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()
这是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
字符=&#34; boy.png&#34; 玩家= pygame.image.load(字符).convert_alpha()
将玩家X设置为PlayerX,玩家Y