程序中奇怪的错误,在使用某些对象后“关闭”它们

认知72

我正在尝试使用字体和临时按钮在窗口中创建“基于文本的游戏”。一切正常,但是当屏幕上有多个按钮且仅按下一个按钮时,其他按钮的碰撞就会转移到下一个屏幕,并弄乱一切。在进行调试时,我注意到短暂按下下一个屏幕上的按钮是有效的,但是随后从第一个屏幕转到第二个按钮,例如。我按下第一个按钮,第二个屏幕上的按钮可以工作,但是通过触发第一个屏幕上的第二个按钮可以解决问题。这是代码:

import pygame, sys
from pygame.locals import *

pygame.init()

window = pygame.display.set_mode((640, 480))

starting = 0
choiceNum = 0
isReady = False
isReady2 = False

#old debug tool - hasBeenClicked = False

fList = []

FONTtitle = pygame.font.SysFont('arial black', 20)
FONT = pygame.font.SysFont('arial black', 15)

YELLOW = (255, 255, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 40)
BLUE = (0, 0, 255)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

class StoryFont():
    def __init__(self, FONT, string, x, y, window):
        self.FONT = FONT
        self.string = string
        self.x = x
        self.y = y
        self.window = window
        self.fSFont = 0

    def create(self):
        self.fSFont = self.FONT.render(self.string, False, WHITE)
        self.fSFontr = self.fSFont.get_rect()
        self.fSFontr.center = (self.x,self.y)

    def blit(self):
        self.window.blit(self.fSFont, self.fSFontr)

class Button():
    def __init__(self, FONT, string, x1, y1, x2, y2, fontx, fonty, choiceNum, window):
        self.FONT = FONT
        self.string = string
        self.x1 = x1
        self.y1 = y1
        self.x2 = x2
        self.y2 = y2
        self.fontx = fontx
        self.fonty = fonty
        self.choiceNum = choiceNum
        self.hasBeenClicked = False
        self.window = window
        self.fSFont = 0

    def create(self):
        self.fSFont = self.FONT.render(self.string, False, WHITE)
        self.fSFontr = self.fSFont.get_rect()
        self.fSFontr.center = (self.fontx, self.fonty)
        self.window.blit(self.fSFont, self.fSFontr)


        if self.hasBeenClicked == False:
            if pos[1] >= self.fonty - 17.5 and pos[1] <= self.fonty + 17.5 and pos[0] >= self.fontx - 47.5 and pos[0] <= self.fontx + 47.5:
                pygame.draw.rect(window, BLUE, ((self.x1, self.y1), (self.x2, self.y2)), 0)
                window.blit(self.fSFont, self.fSFontr)

                if pygame.mouse.get_pressed()[0] == True:
                    global starting
                    self.hasBeenClicked = True
                    starting = self.choiceNum
                    isReady2 = False
                    window.fill(BLACK)
                    return starting


            else:
                global isReady2
                isReady2 = False






def blitMenu(FONTtitle):
    pygame.display.set_caption("Text Game")
    fTITLE = StoryFont(FONTtitle, "Text Game", 320, 100, window)
    fTITLE.create()

    fBUTTON = Button(FONTtitle, "Start", 285, 285, 70, 30, 320, 300, 1, window)

    def blitMenuDraw():

        window.fill(BLACK)
        fTITLE.blit()
        fBUTTON.create()
    blitMenuDraw()


def choice1(FONT):
    fSTORY = StoryFont(FONT, "You are Mr. Stetkiewicz, a seventh grade science teacher.", 320, 50, window)
    fSTORY.create()

    fSTORY2 = StoryFont(FONT, "One day, when coming into your class room to teach about the", 320, 70, window)
    fSTORY2.create()

    fSTORY3 = StoryFont(FONT, "behavior of convection, you notice that no one is there.", 320, 90, window)
    fSTORY3.create()

    fSTORY4 = StoryFont(FONT, "Everything seems cold and somewhat dark.", 320, 140, window)
    fSTORY4.create()

    fSTORY5 = StoryFont(FONT, "Eventually you realize that there is a lack of thermal energy,", 320, 190, window)
    fSTORY5.create()

    fSTORY6 = StoryFont(FONT, "as if it has been stolen. Infuriated, you grab your trusty frying pan", 320, 210, window)
    fSTORY6.create()

    fSTORY7 = StoryFont(FONT, "engraved with '212 degree boiling point', and set out on your journey", 320, 230, window)
    fSTORY7.create()

    fSTORY8 = StoryFont(FONT, "to restore thermal energy to the school.", 320, 250, window)
    fSTORY8.create()

    fSTORY9 = StoryFont(FONT, "Good Luck!", 320, 300, window)
    fSTORY9.create()

    fBUTTON2 = Button(FONT, "Continue", 270, 385, 100, 30, 320, 400, 2, window)


    def choice1Draw():

        window.fill(BLACK)
        fSTORY.blit()
        fSTORY2.blit()
        fSTORY3.blit()
        fSTORY4.blit()
        fSTORY5.blit()
        fSTORY6.blit()
        fSTORY7.blit()
        fSTORY8.blit()
        fSTORY9.blit()
        fBUTTON2.create()

    choice1Draw()

def choice2(FONT):

    fSTORY = StoryFont(FONT, "You have just exited the doorway to your classroom.", 320, 170, window)
    fSTORY.create()

    fSTORY2 = StoryFont(FONT, "What would you like to do?", 320, 230, window)
    fSTORY2.create()

    fBUTTON3 = Button(FONT, "1.Check Inventory", 240, 300, 160, 25, 320, 310, 3, window)

    fBUTTON4 = Button(FONT, "2.Look left down the hallway.", 190, 330, 260, 25, 320, 345, 4, window)

    fBUTTON5 = Button(FONT, "3.Look right down the hallway.", 185, 365, 265, 25, 320, 380, 5, window)

    global fList
    fList = [fSTORY, fSTORY2, fBUTTON3, fBUTTON4, fBUTTON5]

    if fBUTTON3.hasBeenClicked == True:
        fBUTTON4.hasBeenClicked = True
        fBUTTON5.hasBeenClicked = True

    if fBUTTON4.hasBeenClicked == True:
        fBUTTON3.hasBeenClicked = True
        fBUTTON5.hasBeenClicked = True

    if fBUTTON5.hasBeenClicked == True:
        fBUTTON3.hasBeenClicked = True
        fBUTTON4.hasBeenClicked = True

    print(str(fBUTTON3.hasBeenClicked) + "button 3")
    print(str(fBUTTON4.hasBeenClicked) + "button 4")
    print(str(fBUTTON5.hasBeenClicked) + "button 5")


    choice2Draw(fList, window)

def choice2Draw(fList, window):
    window.fill(BLACK)
    fList[0].blit()
    fList[1].blit()
    fList[2].create()
    fList[3].create()
    fList[4].create()

    global isReady2


def choice3(FONT):
    #print("HI")

    fSTORY = StoryFont(FONT, "You are currently holding a frying pan and a pencil in your hands.", 320, 200, window)
    fSTORY.create()

    fSTORY2 = StoryFont(FONT, "You are also wearing glasses, and your work clothes.", 320, 220, window)
    fSTORY2.create()

    fBUTTON = Button(FONT, "Back", 190, 330, 260, 25, 320, 345, 2, window)

    def choice3Draw():
        window.fill(BLACK)
        fSTORY.blit()
        fSTORY2.blit()
        fBUTTON.create()

        global isReady2

    choice3Draw()

def choice4(FONT):
    print("in choice 4")

def choice5(FONT):
    print("in choice 5")

while True:
    pos = pygame.mouse.get_pos()
    if starting == 0:
        blitMenu(FONTtitle)
        print(str(starting))

    if starting == 1:
        choice1(FONT)
        print(str(starting))

    if starting == 2:
        choice2(FONT)
        print(str(starting))

    if starting == 2.5:
        choice2Draw(fList, window)
        print(str(starting))

    if starting == 3:
        choice3(FONT)
        print(str(starting))

    if starting == 4:
        choice4(FONT)
        print(str(starting))

    if starting == 5:
        choice5(FONT)
        print(str(starting))



    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    pygame.display.update()

问题源于选择2:

def choice2(FONT):

    fSTORY = StoryFont(FONT, "You have just exited the doorway to your classroom.", 320, 170, window)
    fSTORY.create()

    fSTORY2 = StoryFont(FONT, "What would you like to do?", 320, 230, window)
    fSTORY2.create()

    fBUTTON3 = Button(FONT, "1.Check Inventory", 240, 300, 160, 25, 320, 310, 3, window)

    fBUTTON4 = Button(FONT, "2.Look left down the hallway.", 190, 330, 260, 25, 320, 345, 4, window)

    fBUTTON5 = Button(FONT, "3.Look right down the hallway.", 185, 365, 265, 25, 320, 380, 5, window)

    global fList
    fList = [fSTORY, fSTORY2, fBUTTON3, fBUTTON4, fBUTTON5]

    if fBUTTON3.hasBeenClicked == True:
        fBUTTON4.hasBeenClicked = True
        fBUTTON5.hasBeenClicked = True

    if fBUTTON4.hasBeenClicked == True:
        fBUTTON3.hasBeenClicked = True
        fBUTTON5.hasBeenClicked = True

    if fBUTTON5.hasBeenClicked == True:
        fBUTTON3.hasBeenClicked = True
        fBUTTON4.hasBeenClicked = True

    print(str(fBUTTON3.hasBeenClicked) + "button 3")
    print(str(fBUTTON4.hasBeenClicked) + "button 4")
    print(str(fBUTTON5.hasBeenClicked) + "button 5")


    choice2Draw(fList, window)

def choice2Draw(fList, window):
    window.fill(BLACK)
    fList[0].blit()
    fList[1].blit()
    fList[2].create()
    fList[3].create()
    fList[4].create()

    global isReady2

在调试时,我发现了这一点:(单独的数字是选择数字)

3
3
3
3
3
True inside button class
2
False button 3
False button 4
False button 5
True inside button class
4
in choice 4
4
in choice 4
4
in choice 4
4
in choice 4
4
in choice 4
4
in choice 4

我不太确定为什么会这样,或者为什么在这个IDLE shell中程序像预期的那样返回到选项2,然后又返回到选项2中的第二个按钮声明。这就像禁用fBUTTON4之前的所有操作一样。请尝试自己运行该程序,看看是否遇到相同的错误。感谢您的帮助!

埃里克·布洛赫(Eric Bulloch)

我已经审查了您的代码,并且了解发生了什么。主要问题实际上发生在Button类的create函数中。

当某些按钮在屏幕上呈现时,它们的位置大致相同。这样的一个示例是choice1中的继续按钮和3。在choose2中向右下看走廊。

当我单击choice1中继续按钮的顶部时,屏幕转到choice2并认为我单击了3。在choic2中向右下看走廊按钮。那将立即把我带到选择5。

我们需要做的是确保用户在两次单击之间释放鼠标按钮。

为此,我向您的程序添加了6行。在文件的顶部,我在变量下添加了一个新isReady2变量:

isReady2 = False
hasBeenReleased = not pygame.mouse.get_pressed()[0]

这将检查玩家是否按住了鼠标键,或者在他们开始游戏时释放了鼠标键。

接下来,我添加了代码,因此他们必须释放鼠标按钮才能再次单击。我正在粘贴您已经拥有的代码,然后粘贴我的解决方案。

if self.hasBeenClicked == False:
        if pos[1] >= self.fonty - 17.5 and pos[1] <= self.fonty + 17.5 and pos[0] >= self.fontx - 47.5 and pos[0] <= self.fontx + 47.5:
            pygame.draw.rect(window, BLUE, ((self.x1, self.y1), (self.x2, self.y2)), 0)
            window.blit(self.fSFont, self.fSFontr)

            if pygame.mouse.get_pressed()[0] == True:
                global starting
                self.hasBeenClicked = True
                starting = self.choiceNum
                isReady2 = False
                window.fill(BLACK)
                return starting

这是我的解决方法:

if self.hasBeenClicked == False:
    if pos[1] >= self.fonty - 17.5 and pos[1] <= self.fonty + 17.5 and pos[0] >= self.fontx - 47.5 and pos[0] <= self.fontx + 47.5:
        pygame.draw.rect(window, BLUE, ((self.x1, self.y1), (self.x2, self.y2)), 0)
        window.blit(self.fSFont, self.fSFontr)

        global hasBeenReleased
        if hasBeenReleased and pygame.mouse.get_pressed()[0] == True:
            hasBeenReleased = False
            global starting
            self.hasBeenClicked = True
            starting = self.choiceNum
            isReady2 = False
            window.fill(BLACK)
            return starting
        elif not hasBeenReleased:
            hasBeenReleased = not pygame.mouse.get_pressed()[0]

该代码检查按钮是否以前已释放。如果已发布,那么我们可以检查当前是否正在单击它。如果它被点击了,那么我们hasBeenReleased = False让我们知道他们确实点击了。

让他们再次单击的部分是:

elif not hasBeenReleased:
    hasBeenReleased = not pygame.mouse.get_pressed()[0]

如果我们检查他们最近是否单击过,然后将值设置为等于他们是否释放了按钮或是否仍在按住按钮。

使用此代码,我能够在选择2和3之间来回切换。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Dmenu启动的某些程序在打开后立即关闭

来自分类Dev

如何删除使用 NodeJS + AngularJS 从文件中删除某些内容的奇怪错误

来自分类Dev

Rails中的奇怪错误-缺少帮助程序

来自分类Dev

在WebSocket ++中关闭连接后出现错误

来自分类Dev

本机脚本:部署应用程序后出现奇怪的错误

来自分类Dev

Xml序列化程序用于内部对象奇怪的错误

来自分类Dev

使用JT400连接池调用IBM i RPG程序后,它们引用了错误的库

来自分类Dev

VBA:使用联接表时,SQL查询中的对象关闭错误

来自分类Dev

Javascript:使用JS对象时出现奇怪的错误

来自分类Dev

Java-关闭所有对象后使用JDBC内存

来自分类Dev

我的c程序中的这个奇怪错误是什么?

来自分类Dev

在CSS中为某些对象使用异常

来自分类Dev

在CSS中对某些对象使用异常

来自分类Dev

PHP中奇怪的关闭行为

来自分类Dev

在另一个程序关闭后,如何使用Powershell关闭程序?

来自分类Dev

关闭Android应用程序后,HashMap中的数据

来自分类Dev

在WPF中关闭事件后保持应用程序运行

来自分类Dev

使用GCC编译32位程序时出现奇怪的错误

来自分类Dev

使用.NET应用程序时出现奇怪的错误消息

来自分类Dev

使用Raygun的快速处理程序后如何关闭进程?

来自分类Dev

在 android 应用程序关闭后使用侦听器

来自分类Dev

在firemonkey中关闭表单后,错误的表单会被聚焦

来自分类Dev

错误后多线程中的电子进程未关闭

来自分类Dev

在使用Caffeine扩展程序的情况下,仅在GNOME中约30秒后才会显示“关闭”对话框

来自分类Dev

如何在执行某些操作后解除所有事件处理程序的绑定并重新绑定它们?

来自分类Dev

奇怪的python对象属性错误

来自分类Dev

由于某些奇怪的原因在 map reduce 程序中获取 NumberFormatException

来自分类Dev

Ubuntu如何在Windows无法关闭的情况下更新运行中的应用程序而不必关闭它们?

来自分类Dev

Book对象程序中的NullPointException错误

Related 相关文章

  1. 1

    Dmenu启动的某些程序在打开后立即关闭

  2. 2

    如何删除使用 NodeJS + AngularJS 从文件中删除某些内容的奇怪错误

  3. 3

    Rails中的奇怪错误-缺少帮助程序

  4. 4

    在WebSocket ++中关闭连接后出现错误

  5. 5

    本机脚本:部署应用程序后出现奇怪的错误

  6. 6

    Xml序列化程序用于内部对象奇怪的错误

  7. 7

    使用JT400连接池调用IBM i RPG程序后,它们引用了错误的库

  8. 8

    VBA:使用联接表时,SQL查询中的对象关闭错误

  9. 9

    Javascript:使用JS对象时出现奇怪的错误

  10. 10

    Java-关闭所有对象后使用JDBC内存

  11. 11

    我的c程序中的这个奇怪错误是什么?

  12. 12

    在CSS中为某些对象使用异常

  13. 13

    在CSS中对某些对象使用异常

  14. 14

    PHP中奇怪的关闭行为

  15. 15

    在另一个程序关闭后,如何使用Powershell关闭程序?

  16. 16

    关闭Android应用程序后,HashMap中的数据

  17. 17

    在WPF中关闭事件后保持应用程序运行

  18. 18

    使用GCC编译32位程序时出现奇怪的错误

  19. 19

    使用.NET应用程序时出现奇怪的错误消息

  20. 20

    使用Raygun的快速处理程序后如何关闭进程?

  21. 21

    在 android 应用程序关闭后使用侦听器

  22. 22

    在firemonkey中关闭表单后,错误的表单会被聚焦

  23. 23

    错误后多线程中的电子进程未关闭

  24. 24

    在使用Caffeine扩展程序的情况下,仅在GNOME中约30秒后才会显示“关闭”对话框

  25. 25

    如何在执行某些操作后解除所有事件处理程序的绑定并重新绑定它们?

  26. 26

    奇怪的python对象属性错误

  27. 27

    由于某些奇怪的原因在 map reduce 程序中获取 NumberFormatException

  28. 28

    Ubuntu如何在Windows无法关闭的情况下更新运行中的应用程序而不必关闭它们?

  29. 29

    Book对象程序中的NullPointException错误

热门标签

归档