Python 如何在Pygame中添加暂停和取消暂停功能,使整个游戏暂停和取消暂停

Python 如何在Pygame中添加暂停和取消暂停功能,使整个游戏暂停和取消暂停,python,pygame,Python,Pygame,我想添加一个暂停和取消暂停功能,例如,你按下p键,游戏冻结,当你再次按下p键时,游戏应该取消暂停 import pygame from pygame.constants import( QUIT, KEYDOWN, KEYUP, K_LEFT, K_RIGHT, K_ESCAPE, K_SPACE, MOUSEBUTTONDOWN) import os from random import randint import time import sys class Settings: w

我想添加一个暂停和取消暂停功能,例如,你按下p键,游戏冻结,当你再次按下p键时,游戏应该取消暂停

import pygame
from pygame.constants import( QUIT, KEYDOWN, KEYUP, K_LEFT, K_RIGHT, K_ESCAPE, K_SPACE, MOUSEBUTTONDOWN)
import os
from random import randint
import time
import sys

class Settings:
    w_width = 800
    w_height = 600
    w_border = 50
    file_path = os.path.dirname(os.path.abspath(__file__))
    image_path = os.path.join(file_path, "pictures")
    sound_path = os.path.join(file_path, "game_sounds")

class Background(object):
    def __init__(self, filename):
        self.imageo = pygame.image.load(os.path.join(Settings.image_path, filename))
        self.image = pygame.transform.scale(self.imageo, (Settings.w_width, Settings.w_height)).convert()
        self.rect = self.image.get_rect()

    def draw(self, screen):
        screen.blit(self.image, self.rect)


class Score(object):
    def __init__(self):
        self.black = 0,0,0
        self.count = 0
        self.font = pygame.font.SysFont("comicsans",30, True , True)
        self.text = self.font.render("Score : "+str(self.count),1,self.black)
        
    def show_score(self, screen):
        screen.blit(self.text,(660 ,0))


    def score_up(self):
        self.count += 1
        self.text = self.font.render("Score : "+str(self.count),1,self.black)


class Bubble(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.width = 50
        self.height = 300
        self.image = pygame.image.load(os.path.join(Settings.image_path, "Blase.png ")).convert_alpha()
        self.image = pygame.transform.scale(self.image, (25, 25))
        self.rect_br = (Settings.w_width//2) + 100, (Settings.w_height//2) + 100
        self.rect = self.image.get_rect()
        self.mousex, self.mousey = pygame.mouse.get_pos()
        self.cx = self.width - 25
        self.cy = self.height - 25
        self.rect.left = randint(Settings.w_border, Settings.w_width - Settings.w_border)
        self.rect.top = randint(Settings.w_border, Settings.w_height - (Settings.w_border * 2))


    def update(self):
        pass

    def scale_up(self):
        self.scale["width"] += 2
        self.scale["height"] += 2

    def scale_down(self):
        self.scale["width"] -= 2
        self.scale["height"] -= 2

    def events(self):
        pass
 
    def draw(self, screen):
        screen.blit(self.image,self.rect )
在这门课上,一切都是为了游戏的运行和一切的发生。 我已经试过了,当你按P键时,self.runn会变成False,但这不起作用

    

class Game(object):
    def __init__(self):
        self.screen = pygame.display.set_mode((Settings.w_width, Settings.w_height))
        self.clock = pygame.time.Clock()
        self.runn = False
        self.score = Score()
        self.background = Background("Hintergrund.png")
        self.bubble = pygame.sprite.Group()
        self.sound1 = pygame.mixer.Sound(os.path.join(Settings.sound_path, "Click.wav"))
        self.sound2 = pygame.mixer.Sound(os.path.join(Settings.sound_path, "pop.wav"))
        

    
    def run(self):
        self.runn = True
        while self.runn:
            self.clock.tick(60)
            self.watch_for_events()
            self.draw()
            self.events()
            

    def events(self):
        if len(self.bubble)< 10:
            self.bubble.add(Bubble())
            time.sleep(0.5)
  
    def draw(self):
        self.background.draw(self.screen)
        self.bubble.draw(self.screen)
        self.score.show_score(self.screen)
    
        pygame.display.flip()

    def watch_for_events(self):
        for event in pygame.event.get():
            if event.type == QUIT:
                self.runn = False

            if event.type == MOUSEBUTTONDOWN:
                for bubble in self.bubble:
                    if bubble.rect.collidepoint(event.pos):
                        bubble.kill()
                        self.score.score_up()
                    
                
if __name__ == '__main__':
    os.environ['SDL_VIDEO_WINDOWS_POS'] = "50, 1100"
    pygame.init()
    game = Game()
    game.run()
    pygame.quit()
    

班级游戏(对象):
定义初始化(自):
self.screen=pygame.display.set_模式((Settings.w_width,Settings.w_height))
self.clock=pygame.time.clock()
self.runn=False
self.score=score()
self.background=background(“Hintergrund.png”)
self.bubble=pygame.sprite.Group()
self.sound1=pygame.mixer.Sound(os.path.join(Settings.Sound\u path,“Click.wav”))
self.sound2=pygame.mixer.Sound(os.path.join(Settings.Sound\u path,“pop.wav”))
def运行(自):
self.runn=True
而self.runn:
自我时钟滴答(60)
self.watch_for_events()
self.draw()
self.events()
def事件(自):
如果len(自气泡)<10:
self.bubble.add(bubble())
睡眠时间(0.5)
def牵引(自):
self.background.draw(self.screen)
self.bubble.draw(self.screen)
self.score.show_score(self.screen)
pygame.display.flip()
def监视事件(自身):
对于pygame.event.get()中的事件:
如果event.type==退出:
self.runn=False
如果event.type==MOUSEBUTTONDOWN:
对于self.bubble中的气泡:
如果bubble.rect.collidepoint(事件位置):
bubble.kill()
self.score.score_up()
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
os.environ['SDL_VIDEO_WINDOWS_POS']=“501100”
pygame.init()
游戏
game.run()
pygame.quit()

游戏
类添加
暂停
属性

类游戏(对象):
# [...]
def运行(自):
self.runn=True
self.pause=False
而self.runn:
自我时钟滴答(60)
self.watch_for_events()
如果不是自我暂停:
self.draw()
self.events()
按下一个键(例如p)时,切换暂停状态:

类游戏(对象):
# [...]
def监视事件(自身):
对于pygame.event.get()中的事件:
如果event.type==退出:
self.runn=False
如果event.type==pygame.KEYDOWN:
如果event.key==pygame.K\p:
self.pause=不self.pause
如果不是self.pause和event.type==MOUSEBUTTONDOWN:
对于self.bubble中的气泡:
如果bubble.rect.collidepoint(事件位置):
bubble.kill()
self.score.score_up()

是否可能在暂停时窗口有点灰色?