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




如果玩家



如果您触摸稻草人,则可以传送到下一个等级。

最佳答案

做这样的事情(支持多个级别)的最好方法是认为“ 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工作时,可能会有一些错误(可能是语言的规则),但是我知道逻辑的工作原理,希望我能清楚地解释一切,如果不问我!

关于python - pygame传送,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19752292/

10-12 20:29