1

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

我的地图文件。

如果玩家播放器

在此处输入图像描述

如果你触摸稻草人,那么你会传送到下一个级别。

在此处输入图像描述

4

3 回答 3

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,我可能有一些错误(可能是语言规则),但我知道逻辑是如何工作的,如果不问我,我希望我能清楚地解释一切!

于 2013-11-28T08:55:02.000 回答
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 位置:

在部分屏幕上

于 2013-11-03T11:58:57.077 回答
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 中生成玩家,并像这样加载新地图的代码:

这是地图一!

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

character="boy.png" player=pygame.image.load(character).convert_alpha()

在这里将玩家 X 设置为 PlayerX 和玩家 Y

于 2015-01-21T10:36:37.110 回答