pygame-web / pygbag

python and pygame wasm for everyone ( packager + test server + simulator )
https://github.com/pygame-web
MIT License
338 stars 39 forks source link

Please help me. I won't fall for the black screen #174

Closed wlfyddlek closed 2 months ago

wlfyddlek commented 3 months ago

I want to run Tetris code on the web, but the game window doesn't show up on the black screen. How do I fix this

169

import pygame
import sys
import asyncio
from button import Button
from settings import *
from sys import exit
from os.path import join
from game import Game
from score import Score
from preview import Preview
from random import choice

async def main():
    pygame.init()

    # 화면 설정
    SCREEN = pygame.display.set_mode((650, 720))
    pygame.display.set_caption("Tetris")
    BG = pygame.image.load("graphics/Background.png")

    def get_font(size):  
        return pygame.font.Font("graphics/font.ttf", size)

    class Main:
        def __init__(self):
            self.display_surface = SCREEN
            self.clock = pygame.time.Clock()

            self.next_shapes = [choice(list(TETROMINOS.keys())) for shape in range(3)]
            self.game = Game(self.get_next_shape, self.update_score)
            self.score = Score()
            self.preview = Preview()

            self.music = pygame.mixer.Sound(join('.', 'sound', 'music.wav'))
            self.music.set_volume(0.05)
            self.music.play(-1)

            self.game_over = False

        def update_score(self, lines, score, level):
            self.score.lines = lines
            self.score.score = score
            self.score.level = level

        def get_next_shape(self):
            next_shape = self.next_shapes.pop(0)
            self.next_shapes.append(choice(list(TETROMINOS.keys())))
            return next_shape

        def run(self):
            while True:
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        pygame.quit()
                        exit()

                if self.game_over:
                    self.music.stop()
                    self.display_game_over_screen()
                    if self.wait_for_enter():
                        self.__init__()
                        continue

                self.display_surface.fill(GRAY)
                self.game.run()
                self.score.run()
                self.preview.run(self.next_shapes)

                if self.game.is_game_over():  
                    self.game_over = True

                pygame.display.update()
                self.clock.tick()
        def display_game_over_screen(self):

            self.display_surface.fill((0, 0, 0))
            game_over_text = get_font(50).render("GAME OVER", True, "White")
            score_text = get_font(30).render(f"Score: {self.score.score}", True, "White")
            lines_text = get_font(30).render(f"Lines: {self.score.lines}", True, "White")
            self.display_surface.blit(game_over_text, (125, 200))
            self.display_surface.blit(score_text, (245, 400))
            self.display_surface.blit(lines_text, (245, 550))
            pygame.display.update()

        def wait_for_enter(self):
            while True:
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        pygame.quit()
                        exit()
                    if event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN:
                        return True

    def play():
        main = Main()
        main.run()

    def main_menu():
        while True:
            SCREEN.blit(BG, (0, 0))
            MENU_MOUSE_POS = pygame.mouse.get_pos()

            MENU_TEXT = get_font(70).render("MAIN MENU", True, "#b68f40")
            MENU_RECT = MENU_TEXT.get_rect(center=(325, 100))

            PLAY_BUTTON = Button(image=pygame.image.load("graphics/Play Rect.png"), pos=(325, 250),
                                text_input="PLAY", font=get_font(75), base_color="#d7fcd4", hovering_color="White")
            OPTIONS_BUTTON = Button(image=pygame.image.load("graphics/Options Rect.png"), pos=(325, 400),
                                    text_input="OPTIONS", font=get_font(75), base_color="#d7fcd4", hovering_color="White")
            QUIT_BUTTON = Button(image=pygame.image.load("graphics/Quit Rect.png"), pos=(325, 550),
                                text_input="QUIT", font=get_font(75), base_color="#d7fcd4", hovering_color="White")

            SCREEN.blit(MENU_TEXT, MENU_RECT)

            for button in [PLAY_BUTTON, OPTIONS_BUTTON, QUIT_BUTTON]:
                button.changeColor(MENU_MOUSE_POS)
                button.update(SCREEN)

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if PLAY_BUTTON.checkForInput(MENU_MOUSE_POS):
                        play()
                    if OPTIONS_BUTTON.checkForInput(MENU_MOUSE_POS):
                        options()
                    if QUIT_BUTTON.checkForInput(MENU_MOUSE_POS):
                        pygame.quit()
                        sys.exit()

            pygame.display.update()

    main_menu()
    await asyncio.sleep(0)
asyncio.run(main())
pmp-p commented 3 months ago

you should have only one flip/update async loop, the other menu / screens should be game states that only draw/render. You can have a look to this one it uses game states https://github.com/pmp-p/pygame-breakout-wasm/