samedi 21 mai 2022

This code I found on the internet please explain it to me? especially the def main() [closed]

import time

import pygame

import pygame.freetype

import random

Global instances

# color 
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
BLUE = (187, 238, 255)
PURPLE = (192, 204, 255)

# create window 
WIN = pygame.display.set_mode((640, 480))

Class Screen that shows everything that displays on the screen Why need return None


# Show the layout of screen 
class Screen: 
 
    Font = None
    def __init__(self, next_screen, *text):
        self.background = pygame.Surface((640, 480))  
        self.background.fill(BLUE)  

        y = 80
        if text:
            Screen.Font = pygame.freetype.SysFont('times', 32)  
            for line in text:
                Screen.Font.render_to(self.background, (120, y), line, BLACK)  
                y += 50  

        self.next_screen = next_screen
        self.add_text = None

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

    def draw(self):
        WIN.blit(self.background, (0, 0))  
        if self.add_text:
            y = 180
            for line in self.add_text:
                Screen.Font.render_to(WIN, (120, y), line, BLACK)
                y += 50
    
# Why need return None 
    def update(self, events):
        for event in events:
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:  
                    return self.next_screen, None

Layout of frames on the screen Create the frames to put answers inside Check the position of the mouse

class SettingScreen:  

    def __init__(self):
        self.background = pygame.Surface((640, 480))
        self.background.fill(BLUE)

        Screen.Font.render_to(self.background, (120, 50), 'Select your difficulty level', BLACK)

        self.rects = []
        x = 120
        y = 120

# Create the frames to put answers inside 
        for i in range(4):  
            rect = pygame.Rect(x, y, 80, 80)  
            self.rects.append(rect)  
            x += 100

# Is this necessary 
    def start(self, *args): 
        pass

    def draw(self):
        WIN.blit(self.background, (0, 0))
        n = 1

# Check the position of the mouse 
        for rect in self.rects:  
            if rect.collidepoint(pygame.mouse.get_pos()):  
                pygame.draw.rect(WIN, PURPLE, rect)
            pygame.draw.rect(WIN, PURPLE, rect, 5)  
            Screen.Font.render_to(WIN, (rect.x + 30, rect.y + 30), str(n), BLACK)  
            n += 1  

# 'GAME' in the main() 
    def update(self, events):
        for event in events:
            if event.type == pygame.MOUSEBUTTONDOWN:  
                for rect in self.rects:
                    if rect.collidepoint(event.pos):
                        return 'GAME', Level()

Level that changes every time player chooses answer time.sleep to make sure not to choose 2 answers continuity The screen that show your results

# Change the screen every time choose the answer 
class Level:  

    def __init__(self):
        self.questions = [
            ('Find x: (5-x) + (6+x) = 11', 3),
            ('Find x: 4(x+7) + 11(x-2) -3 = 0', 2),
            ('Find the missing number 1357986_2', 4),
            ('1 x1 = ? ', 1)
        ]
        self.current_question = None
        self.background = None
        self.right = 0
        self.wrong = 0

    def pop_question(self):
        ques = random.choice(self.questions)  
        self.questions.remove(ques)  
        self.current_question = ques  
        return ques

# time.sleep to make sure not to choose 2 answer continuity 
    def answer(self, answer):
        if answer == self.current_question[1]:  
            time.sleep(0.5)     
            self.right += 1
        else:
            time.sleep(0.5)
            self.wrong += 1

# Screen that show your results 
    def get_result(self):
        return f'{self.right} answers correct', f'{self.wrong} answers wrong', '', 'Good!' \
            if self.right > self.wrong else 'You can do better!'

Start the Game Play and stop if there are no more questions What 'GAME' and 'RESULT' doing


# start the game

class GamePlay(Level):

    def __init__(self):
        super().__init__()
        self.rects = []
        x = 120
        y = 120

        for n in range(4):  
            rect = pygame.Rect(x, y, 400, 80)
            self.rects.append(rect)
            y += 90

        self.game_level = None

    def start(self, game_level):
        self.background = pygame.Surface((640, 480))
        self.background.fill(BLUE)
        self.game_level = game_level

# Why 2 Variables 
        question, answer = game_level.pop_question()  
        Screen.Font.render_to(self.background, (120, 50), question, BLACK)

    def draw(self):
        WIN.blit(self.background, (0, 0))
        n = 1
        for rect in self.rects:
            if rect.collidepoint(pygame.mouse.get_pos()):
                pygame.draw.rect(WIN, PURPLE, rect)
            pygame.draw.rect(WIN, PURPLE, rect, 5)
            Screen.Font.render_to(WIN, (rect.x + 30, rect.y + 30), str(n), BLACK)
            n += 1

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

                            return 'GAME', self.game_level
                        else:
                            return 'RESULT', self.game_level.get_result()
                    n += 1
# What 'GAME' and 'RESULT' doing 


main() to run the game

def main():

    pygame.init()
    clock = pygame.time.Clock()

# this new to me 
    screens = {
        'TITLE': Screen('SETTING', 'Welcome to the game', '', '', '', 'Press [Space] to start'),
        'SETTING': SettingScreen(),
        'GAME': GamePlay(),
        'RESULT': Screen('TITLE', 'Here is your result:'),
    }  



    screen = screens['TITLE']
    while True:
        clock.tick(60)
        events = pygame.event.get()
        for event in events:
            if event.type == pygame.QUIT:
                return False

        result = screen.update(events)
        if result:
            next_screen, level = result
            if next_screen:
                screen = screens[next_screen]
                screen.start(level)

        screen.draw()
        pygame.display.update()


if __name__ == '__main__':

    main()



Aucun commentaire:

Enregistrer un commentaire