【发布时间】:2015-01-08 02:37:32
【问题描述】:
好的,所以在我的学校,我们需要做一个高级项目,我决定尝试学习编程。一开始,我决定开始学习 VEX 课程,它教会了我一些基本的“C”语言。我想为我的实际项目制作一款游戏,所以我决定制作一款愚蠢的迷宫游戏,你必须避免用鼠标触摸墙壁。当我将鼠标悬停在就绪按钮上时,它会加载实际地图,但实际游戏不会在那里完成。这是我目前的代码,我很困惑,因为加载迷宫后,当我触摸墙壁或触摸终点时,程序不会做它应该做的事情。
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(使用新的安装程序),它工作正常。感谢大家的帮助:)
【问题讨论】:
-
那么,您面临的真正问题是什么?
-
试过你的代码(在 linux 机器上),它适用于我使用的示例图像。我希望
get_pos()和图像中的颜色存在问题。您是否通过打印检查颜色值? -
Zormit,你使用了所有正确的颜色 I RGB
-
您可能还需要重新安装 pygame。 (也使用新的安装程序)