gpt4 book ai didi

python - Pygame 传送

转载 作者:太空宇宙 更新时间:2023-11-03 18:44:53 28 4
gpt4 key购买 nike

Pygame 我想知道是否有人知道如何在触摸或越过某些东西时交换 map 。

这是我的代码:

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

最佳答案

执行此类操作(支持多个级别)的最佳方法是认为“bif”是映射的变量。因此,输入后检查英雄的位置(x,y),如果是您想要的值,则将 map 更改为下一个级别。这是代码:

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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com