开发者社区> 问答> 正文

为什么我的循环布尔值一直被设置为True?

所以我在编程一个游戏,这是我第一次使用类。我有一个类,它对背景的不同方面有不同的字段,对不同的背景有不同的对象。 我有一个while循环用于开始屏幕。但是当我点击play时它会将start设为False并停止循环但当我按下它时,它会被设为False然后直接返回True继续循环。我使用print来查看布尔值。 I difine start = True在代码的顶部

import pygame
import sys

pygame.init()

start = True



screen_width = 900
screen_height = 507
center_x = screen_width/2
center_y = screen_height/2

screen = pygame.display.set_mode((screen_width, screen_height))


clock = pygame.time.Clock()
FPS = 100

WHITE = (255,255,255)

bgx = 0
bgx2 = screen_width




main_bg = pygame.image.load("mainmenu.jpg")
main_bg2 = pygame.image.load("mainmenu.jpg")
first_bg = pygame.image.load("firstlvl.png")
first_bg2 = pygame.image.load("firstlvl.png")
second_bg = pygame.image.load("secondlvl.jpg")
second_bg2 = pygame.image.load("secondlvl.jpg")
third_bg = pygame.image.load("thirdlvl.jpg")
third_bg2 = pygame.image.load("thirdlvl.jpg")

#for i in range(1,7):
    #pygame.mixer.music.load("music"+str(i)+".mp3")


class layout:

#""" This class makes the layout of each screen (including buttons, music and background)"""       
    def __init__(self, background, background2, back_button, pause_button, play_button):
        self.bg = background
        self.bg2 = background2
        self.backbut = back_button
        self.pausebut = pause_button
        self.playbut = play_button

    def create_screen(self):
        screen.fill(WHITE)
        global bgx, bgx2
        bgx -= 3
        bgx2 -= 3
        screen.blit(self.bg, [bgx,0])
        screen.blit(self.bg2, [bgx2,0])

        if bgx + screen_width == 0:
            bgx = screen_width
        elif bgx2 + screen_width == 0:
            bgx2 = screen_width

        if self.playbut == True:
            butimg = pygame.image.load("playbutton.png")
            butrect = pygame.Rect(center_x-(358/2),center_y-(146/2),358,146)
            for event in pygame.event.get():
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if butrect.collidepoint(event.pos):
                        start = False
                        print (start)

            screen.blit(butimg,butrect)

        if self.backbut == True:
            butimg2 = pygame.image.load("backbut.png")
            butimg2 = pygame.transform.scale(butimg2, (62, 62))
            butrect2 = pygame.Rect(10,screen_height-72,62,62)
            for event in pygame.event.get():
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if butrect2.collidepoint(event.pos):
                        pass
            screen.blit(butimg2,butrect2)
        if self.pausebut == True:
            pass

        pygame.display.flip() 

main_screen = layout(main_bg, main_bg2, False, False, True)
icon_select = layout(main_bg, main_bg2, True, False, False)
lvl_select = layout(main_bg, main_bg2, True, False, True)
lvl_1 = layout(first_bg, first_bg2, False, True, False)
lvl_2 = layout(second_bg, second_bg2, False, True, False)
lvl_3 = layout(third_bg, third_bg2, False, True, False)


while start:
    print (start)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            start = False
            pygame.quit()
            sys.exit()

    main_screen.create_screen()
    clock.tick(FPS)




pygame.quit()
sys.exit


enter code here

问题来源StackOverflow 地址:/questions/59384941/why-does-my-boolean-for-my-loop-keep-being-set-back-to-true

展开
收起
kun坤 2019-12-26 10:51:35 565 0
1 条回答
写回答
取消 提交回答
  • 在函数create_screen()中,您需要添加一行,表示全局开始,以告诉python解释器变量是在不同的范围内定义的。

    2019-12-26 10:51:54
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载