繁体   English   中英

如何添加暂停和取消暂停 function 以便整个游戏在 Pygame 中暂停和取消暂停

[英]How can i add a Pause and unpause function so the whole game Pause and unpause in 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_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 )

在这个 Class 中就是一切,以便游戏运行并且一切都会发生。 当您按 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()
    

Game class 添加pause属性。

class Game(object):
    # [...]

    def run(self):
        self.runn = True
        self.pause = False

        while self.runn:
            self.clock.tick(60)
            self.watch_for_events()
 
            if not self.pause:
                self.draw()
                self.events()

当按下一个键(例如p )时切换pause state :

class Game(object):
    # [...]

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

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_p:
                    self.pause = not self.pause

            if not self.pause and event.type == MOUSEBUTTONDOWN:
                for bubble in self.bubble:
                    if bubble.rect.collidepoint(event.pos):
                        bubble.kill()
                        self.score.score_up()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM