+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

如何删除pygame中的不同场景?

发布于2021-03-02 15:35     阅读(496)     评论(0)     点赞(28)     收藏(3)


0

1

2

3

4

5

6

对于我的程序,我试图创建一个选择游戏。

我想删除代码中的场景。这包括难度屏幕。

难度画面:

在此处输入图片说明

下面显示了我代码的相关部分。

import pygame
pygame.init()
import pygame.freetype
import random

X = 1275
Y = 775
green = (50, 205, 50)
blue = (0, 0, 205)
screen = pygame.display.set_mode((X,Y))
font100 = pygame.font.SysFont(None, 100)


class SimpleScene:
   FONT = None

   def __init__(self, next_scene, *text):
       self.background = pygame.Surface((1275, 775))
       self.background.fill(pygame.Color('white'))

       y = 200
       if text:
           if SimpleScene.FONT == None:
               SimpleScene.FONT = pygame.freetype.SysFont(None, 32)
           for line in text:
               SimpleScene.FONT.render_to(self.background, (220, y), line, pygame.Color('black'))
               SimpleScene.FONT.render_to(self.background, (220, y - 1), line, pygame.Color('black'))
               y += 50

       self.next_scene = next_scene
       self.additional_text = None

   def start(self, text):
       self.additional_text = text

   def draw(self, screen):
       screen.blit(self.background, (0, 0))
       if self.additional_text:
           y = 180
           for line in self.additional_text:
               SimpleScene.FONT.render_to(screen, (120, y), line, pygame.Color('black'))
               SimpleScene.FONT.render_to(screen, (119, y - 1), line, pygame.Color('white'))
               y += 50

   def update(self, events, dt):
       for event in events:
           if event.type == pygame.KEYDOWN:
               if event.key == pygame.K_SPACE:
                   return (self.next_scene, None)


class GameState:
   def __init__(self, difficulty):
       self.difficulty = difficulty

   def pop_question(self):
       q = self.questions[0]
       self.current_question = q
       return q

class SettingScene:

   def __init__(self):
       self.background = pygame.Surface((1275, 775))
       self.background.fill(pygame.Color('white'))

       if SimpleScene.FONT == None:
           SimpleScene.FONT = pygame.freetype.SysFont(None, 32)

       SimpleScene.FONT.render_to(self.background, (120, 50), 'Select your difficulty level', pygame.Color('black'))
       SimpleScene.FONT.render_to(self.background, (119, 49), 'Select your difficulty level', pygame.Color('black'))

       self.rects = []

       # CHANGE
       for n in range(4):
           rect = pygame.Rect(50, (n * 70) + 100, 500, 50)
           self.rects.append(rect)

   def start(self, *args):
       pass

   def draw(self, screen):
       screen.blit(self.background, (0, 0))
       n = 1
       for rect in self.rects:
           if rect.collidepoint(pygame.mouse.get_pos()):
               pygame.draw.rect(screen, pygame.Color('darkgrey'), rect)
           pygame.draw.rect(screen, pygame.Color('darkgrey'), rect, 5)

           # CHANGE
           SimpleScene.FONT.render_to(screen, (rect.x + 30, rect.y + 15), str(n), pygame.Color('black'))
           SimpleScene.FONT.render_to(screen, (rect.x + 29, rect.y + 14), str(n), pygame.Color('black'))

           n += 1

   def update(self, events, dt):
       for event in events:
           if event.type == pygame.MOUSEBUTTONDOWN:
               n = 1
               for rect in self.rects:
                   if rect.collidepoint(event.pos):
                       return ('GAME', GameState(n))
                   n += 1

class GameScene:
   def __init__(self):
       if SimpleScene.FONT == None:
           SimpleScene.FONT = pygame.freetype.SysFont(None, 32)

       self.rects = []

       for n in range(4):
           rect = pygame.Rect(420, (n * 70) + 300, 500, 50)
           self.rects.append(rect)

   def start(self, gamestate):
       self.background = pygame.Surface((1275, 775))
       self.background.fill(pygame.Color('white'))


   def draw(self, screen):
       screen.blit(self.background, (0, 0))

       n = 0
       for rect in self.rects:
           if rect.collidepoint(pygame.mouse.get_pos()):
               pygame.draw.rect(screen, pygame.Color('darkgrey'), rect)
           pygame.draw.rect(screen, pygame.Color('darkgrey'),
                            rect, 5)


   def update(self, events, dt):
       for event in events:
           if event.type == pygame.MOUSEBUTTONDOWN:
               n = 1
               for rect in self.rects:
                   if rect.collidepoint(event.pos):
                       self.gamestate.answer(n)




class QuitScene:
   def __init__(self):
       if SimpleScene.FONT == None:
           SimpleScene.FONT = pygame.freetype.SysFont(None, 32)

   def start(self, gamestate):
       self.background = pygame.Surface((1275, 775))
       self.background.fill(pygame.Color('white'))
       self.gamestate = gamestate

   def draw(self, screen):
       screen.blit(self.background, (0, 0))

   def update(self, events, dt):
       for event in events:
           if event.type == pygame.MOUSEBUTTONDOWN:
               quit()


def main():
   pygame.init()
   screen = pygame.display.set_mode((1275, 775))
   clock = pygame.time.Clock()
   dt = 0

   scenes = {
       'TITLE': SimpleScene('SETTING', 'You have chosen category 1: Introduction to Programming ', '', '', '', 'press [SPACE] to start'),
       'SETTING': SettingScene(),
       'GAME': GameScene(),
       'QUIT': QuitScene(),
   }
   scene = scenes['TITLE']
   while True:
       events = pygame.event.get()
       for e in events:
           if e.type == pygame.QUIT:
               return

       game = scene.update(events, dt)
       if game:
           next_scene, state = game
           if next_scene:
               scene = scenes[next_scene]
               scene.start(state)


       scene.draw(screen)

       pygame.display.flip()
       dt = clock.tick(60)


if __name__ == '__main__':
   main()

如果有删除此场景的解决方案,请共享。我尝试了删除部分代码的可能方法,但是没有任何效果。谢谢你。


解决方案


要“删除”旧屏幕,必须用一种颜色填充整个屏幕。Pygame当您调用每个屏幕的self.background.fill(pygame.Color('white'))每个start方法时,就是这样处理的,并且您已经在执行此操作

因此,在绘制新屏幕之前,您必须使用Surface.fill函数(在您的示例中,self.background初始化为Surface :)self.background = pygame.Surface((1275, 775))

其他屏幕将不在当前屏幕的背景中,因为Pygame仅绘制您当前正在看到的屏幕。

0

1

2

3

4



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接: https://www.pythonheidong.com/blog/article/869886/ea6bba89968df113621e/

来源: python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

28 0
收藏该文
已收藏

评论内容:(最多支持255个字符)