【发布时间】:2017-02-20 01:14:37
【问题描述】:
好的,我正在尝试使用 pygame 库创建汤姆和杰瑞游戏。
游戏的重点是在老鼠出现在洞中时点击它们来捕捉老鼠。问题 是有时会出现一只猫而不是鼠标,如果玩家错误地点击了 cat (s)he 失去了所有获得的积分,但游戏继续进行。 老鼠是老鼠的形象,猫是猫的形象。 如果你点击鼠标,你会得到鼠标,否则猫会得到分数。 代码一团糟,那是因为我不知道自己在做什么,只是设置了另一个事件循环,因为它可以工作,因为它在我创建鼠标后运行。它可以单击鼠标,但然后您单击其他位置,然后就像您没有单击鼠标一样。
鼠标是在循环中创建的,应该等待 5 秒,如果您在这些秒内单击鼠标,则会在控制台中打印出相应的消息,Jerry clicked!" else "1 click"。如果你在 5 秒内没有点击鼠标,图像覆盖了鼠标,所以她消失了。
现在,我现在要做的是在玩家没有点击任何内容时打印消息 1 click ,而是在玩家点击鼠标时打印 1 click jerry clicked 。我有一个鼠标孔的图像,然后我将鼠标放在鼠标孔上,即另一个图像上。
此代码至少适用于一张图片:
pygame.init()
width=350;
height=400
screen = pygame.display.set_mode( (width, height ) )
pygame.display.set_caption('clicked on image')
redSquare = pygame.image.load("images/red-square.png").convert()
x = 20; # x coordnate of image
y = 30; # y coordinate of image
screen.blit(redSquare , ( x,y)) # paint to screen
pygame.display.flip() # paint screen one time
running = True
while (running):
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
# Set the x, y postions of the mouse click
x, y = event.pos
if redSquare.get_rect().collidepoint(x, y):
print('clicked on image')
#loop over, quite pygame
pygame.quit()
我的问题是,当我点击鼠标然后我不点击鼠标时,我无法在另一个位置再次点击鼠标。
那怎么了?我在这里做错了什么?
这是我的代码:
import pygame
from pygame import *
from random import *
init()
run = True
screen = (800,800)
screen = display.set_mode(screen)
xpos = 0
ypos = 0
mouseorcatxpos = 5
mouseorcatypos = 0
mousehole = image.load("mousehole.png").convert()
cat = image.load("tom.png")
jerry = image.load("jerry.png")
def makeholes():
global ypos
global xpos
for holey in range(1,9):
for holex in range(1,9):
screen.blit(mousehole,(xpos,ypos))
display.flip()
xpos += 100
ypos += 100
xpos = 0
def mouseorcat():
global xpos
mouseorcatxpos = 5
ypos = 0
for mousecaty in range(1,9):
pygame.event.pump()
for mousecatx in range(1,9):
randommouse = randint(1, 3)
randomcat = randint(1, 10)
if(randommouse == 2):
screen.blit(jerry, (mouseorcatxpos, ypos))
display.flip()
for event in pygame.event.get():
if (event.type == MOUSEBUTTONDOWN):
if jerry.get_rect().collidepoint(xpos, ypos) == False:
print("l clicked!")
x, y = event.pos
if jerry.get_rect().collidepoint(xpos, y):
print("JERRY CLICKED!!")
x, y = event.pos
print(x, y)
time.wait(5000)
#screen.blit(mousehole, (mouseorcatxpos - 5, ypos))
display.flip()
elif(randomcat == 2):
screen.blit(cat, (mouseorcatxpos, ypos))
display.flip()
time.wait(1500)
screen.blit(mousehole, (mouseorcatxpos-5, ypos))
display.flip()
mouseorcatxpos += 100
mouseorcatxpos = 0
ypos += 100
makeholes()
while run == True:
for event in pygame.event.get():
mouseorcat()
if event.type == QUIT:
run = False
【问题讨论】:
-
请详细说明游戏的规则和机制。我不确定它应该如何工作。看起来代码需要大幅重组。例如,应该只有一个事件循环和一个 display.flip() 调用。
-
对于投反对票的人,不要粗鲁,不加解释就投反对票,而是帮助改善问题。
-
我提交了一个带有全新示例的答案,但明天我会再看看你的程序,看看它是否可以“拯救”。 ;) 但是,它看起来确实不太好。不用太担心,大家一开始都是这样写代码的。另外,也许下次在reddit.com/r/pygame之类的论坛上寻求帮助会更好。
-
我在回答中添加了一些注释。顺便说一句,我提到 reddit 是因为您的问题需要大量解释和调试,所以 SO 似乎不是正确的地方。
-
看看Program Arcade Games。有一些有趣的示例可能会帮助您更好地学习构建代码。