gpt4 book ai didi

python - Pygame 迷宫游戏

转载 作者:行者123 更新时间:2023-11-28 19:19:56 25 4
gpt4 key购买 nike

好的,所以在我的学校,我们需要做一个高级项目,我决定尝试学习编程。首先,我决定开始学习 VEX 类(class),这教会了我一些基本的“C”语言。我想为我的实际项目制作一款游戏,所以我决定制作一款愚蠢的迷宫游戏,你必须避免用鼠标触摸墙壁。当我将鼠标悬停在就绪按钮上时,我已经达到了加载实际 map 的程度,但实际游戏不会在那里结束。到目前为止,这是我的代码,我很困惑,因为在加载迷宫后,当我触摸墙壁或触摸终点时,程序不会执行它应该做的事情。

import pygame
from pygame import *
pygame.init()

done = False

getready = image.load('ready.png')
backdrop = image.load('map.png')
goon = image.load('continue.png')
maze2 = image.load('map2.png')
loose = image.load('loose.png')
screen = display.set_mode((700, 500))
display.set_caption('Maze Game')
event.set_grab(1)

while done == False:
screen.blit(getready, (0, 0))
display.update()

for e in event.get():
if e.type == KEYUP:
if e.key == K_ESCAPE:
done = True

if screen.get_at((mouse.get_pos())) == (0, 0, 0):
while done == False:
screen.blit(backdrop, (0, 0))
display.update()

if screen.get_at((mouse.get_pos())) == (0, 0, 0):
print("You touched the wall!")
done = True

elif screen.get_at((mouse.get_pos())) == (0, 255, 0):

screen.blit(goon, (0, 0))
display.update()

if e in event.get():
if e.type == KEYUP:
if e.key == K_y:

screen.blit(maze2, (0, 0))
display.update()

if e in event.get():
if e.type == KEYUP:
if e.key == K_y:
done = True

if screen.get_at((mouse.get_pos())) == (0, 0, 0):
screen.blit(victory, (0, 0))
display.update()
time.sleep(3)


for e in event.get():
if e.type == KEYUP:
if e.key == K_ESCAPE:
done = True

pygame.quit()

我知道这可能是非常粗糙的代码,但请记住,我才刚刚开始,欢迎任何有用的输入:)

更新:我把代码发给我堂兄,他把它改成这样:

 import pygame
from pygame import *
pygame.init()

done = False
done2 = False

ref = image.load('ready.png')
loose = image.load('loose.png')
cntnu = image.load('continue.png')
goon = 0
screen = display.set_mode((700, 500))
display.set_caption('Maze Game')
event.set_grab(1)

while done == False:
screen.blit(ref, (0, 0))
display.update()
done2 = False

for e in event.get():
if e.type == KEYUP:
if e.key == K_ESCAPE:
done = True
if screen.get_at((mouse.get_pos())) == (0, 0, 0):

ref = image.load('map.png')
done2 = True

if screen.get_at((mouse.get_pos())) == (1, 0, 0):

screen.blit(loose, (0, 0))
display.update()
done2 = True
time.wait(2000)
done = True

if screen.get_at((mouse.get_pos())) == (0, 255, 0):
screen.blit(cntnu, (0, 0))
display.update()
time.wait(3000)

pygame.quit()

问题实际上不在我的代码中,而在我的 python 文件夹中。我重新安装了 python(使用新的安装程序),它工作正常。感谢大家的帮助:)

最佳答案

从您模糊的问题看来,您没有从程序中获得足够的反馈来定位和修复错误,而这可能是您可能想要解决的实际问题。

使用可以让您逐行运行代码并检查每个变量的状态的工具,您可以从中获益良多,这样您就可以轻松找出程序未按预期运行的时间和原因。我会推荐this discussion on StackOverflow (以及其中的引用资料)不同的调试方法。

除了使用代码调试器之外,开始使用记录器在程序执行期间以所需的特异性级别打印调试信息可能是个好主意。您可以从上面的链接中获取有关记录器的更多信息,或者您可以直接在谷歌上搜索——我相信一旦您知道要寻找什么,您会在那里找到大量的教程。

关于python - Pygame 迷宫游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27832096/

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