pygame-web / pygbag

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

Pygbag Crash, Black Screen #108

Closed frankiephs closed 1 year ago

frankiephs commented 1 year ago

I tried creating the first part of the screen to red (instead of black) to determine whether if it is working or not if I run it. It displays this black screen and by using #debug, it crashes. Help. Am I missing something? I even tried changing the pygame delay to time.sleep thinking the delay is probably the problem

import pygame
import os
import random
import asyncio
import time

COUNT_DOWN = 3

pygame.font.init()
pygame.mixer.init()

WIDTH, HEIGHT = 600, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Space Game")

WHITE = (255,255,255)
BLACK = (0,0,0)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)

# border rect
BORDER = pygame.Rect(WIDTH//2 - 5, 0, 10, HEIGHT)
BLACK_SCREEN = pygame.Rect(0, 0, WIDTH, HEIGHT)

BULLET_HIT_SOUND = pygame.mixer.Sound(os.path.join('assets', 'Grenade-1.ogg'))

BULLET_FIRE_SOUND = pygame.mixer.Sound(os.path.join('assets', 'Gun-Silencer.ogg'))
TADA_SOUND = pygame.mixer.Sound(os.path.join('assets', 'tada.ogg'))

HEALTH_FONT = pygame.font.SysFont('Arial Rounded MT', 40)
WINNER_FONT = pygame.font.SysFont('Arial Rounded MT', 100)
INTRO_FONT = pygame.font.SysFont('Lemon', 50)

FPS = 60
VEL = 10

BULLET_VEL = 30
MAX_BULLETS = 3

SPACESHIP_WIDTH, SPACESHIP_HEIGHT = 55, 40

YELLOW_HIT = pygame.USEREVENT + 1
RED_HIT = pygame.USEREVENT + 2

YELLOW_SPACESHIP_IMAGE = pygame.image.load(os.path.join('assets','spaceship_yellow.png'))
YEllOW_SPACESHIP = pygame.transform.rotate(pygame.transform.scale(YELLOW_SPACESHIP_IMAGE, (SPACESHIP_WIDTH, SPACESHIP_HEIGHT)), 90)

RED_SPACESHIP_IMAGE = pygame.image.load(os.path.join('assets','spaceship_red.png'))
RED_SPACESHIP = pygame.transform.rotate(pygame.transform.scale(RED_SPACESHIP_IMAGE, (SPACESHIP_WIDTH, SPACESHIP_HEIGHT)), 270)

SPACE = pygame.transform.scale(pygame.image.load(os.path.join('assets', 'space.jpg')),(WIDTH, HEIGHT))

def draw_window(red, yellow, red_bullets, yellow_bullets, red_health, yellow_health):

    WIN.blit(SPACE, (0, 0))
    pygame.draw.rect(WIN,BLACK,BORDER)

    red_health_text = HEALTH_FONT.render("Health: " + str(red_health), 1, WHITE)
    yellow_health_text = HEALTH_FONT.render("Health: " + str(yellow_health), 1, WHITE)

    WIN.blit(red_health_text, (WIDTH - red_health_text.get_width() - 10, 10))
    WIN.blit(yellow_health_text, (10, 10))

    WIN.blit(YEllOW_SPACESHIP, (yellow.x, yellow.y))
    WIN.blit(RED_SPACESHIP, (red.x, red.y))

    for bullet in red_bullets:
        pygame.draw.rect(WIN, RED, bullet)
    for bullet in yellow_bullets:
        pygame.draw.rect(WIN, YELLOW, bullet)
    pygame.display.update()

def yellow_handle_movement(keys_pressed, yellow):
        keys_pressed = pygame.key.get_pressed()
        if keys_pressed[pygame.K_a] and yellow.x - VEL > 0: # left
            yellow.x -= VEL # yellow x pos is incremented by the velocity
        if keys_pressed[pygame.K_d] and yellow.x + VEL + yellow.width < BORDER.x: # right
            yellow.x += VEL
        if keys_pressed[pygame.K_w] and yellow.y - VEL > 0: # up 
            yellow.y -= VEL
        if keys_pressed[pygame.K_s] and yellow.y + VEL + yellow.width < HEIGHT : # down
            yellow.y += VEL

def red_handle_movement(keys_pressed, red):
        keys_pressed = pygame.key.get_pressed()
        if keys_pressed[pygame.K_LEFT] and red.x - VEL > BORDER.x + BORDER.width: # left
            red.x -= VEL
        if keys_pressed[pygame.K_RIGHT] and red.x + VEL + red.width < WIDTH: # right
            red.x += VEL
        if keys_pressed[pygame.K_UP] and red.y - VEL > 0:
            red.y -= VEL
        if keys_pressed[pygame.K_DOWN] and red.y + VEL + red.width < HEIGHT: # down
            red.y += VEL

def handle_bullets(yellow_bullets,red_bullets, yellow, red):

    for bullet in yellow_bullets:
        bullet.x += BULLET_VEL
        if red.colliderect(bullet):
            pygame.event.post(pygame.event.Event(RED_HIT))
            yellow_bullets.remove(bullet)
        elif bullet.x > WIDTH:
            yellow_bullets.remove(bullet)

    for bullet in red_bullets:
        bullet.x -= BULLET_VEL
        if yellow.colliderect(bullet):
            pygame.event.post(pygame.event.Event(YELLOW_HIT))
            red_bullets.remove(bullet)
        elif bullet.x < 0:
            red_bullets.remove(bullet)

def draw_winner(text):
    draw_text = WINNER_FONT.render(text, 1, WHITE)

    BLACK_WINNER = pygame.Surface((1000,750))
    alpha = 0
    BLACK_WINNER.fill((0,0,0))
    BLACK_WINNER.set_alpha(alpha)

    while True:
        if BLACK_WINNER.get_alpha() >= 255:

            WIN.blit(BLACK_WINNER, (0,0))
            pygame.display.update()

            break
        else:

            time.sleep(0.03)
            alpha += 5
            BLACK_WINNER.set_alpha(alpha)
            WIN.blit(BLACK_WINNER, (0,0))
            pygame.display.update()
            continue

    WIN.blit(draw_text, (WIDTH//2 - draw_text.get_width()//2, HEIGHT//2 - draw_text.get_height()//2))
    pygame.display.update()
    time.sleep(1)

    BLACK_WINNER = pygame.Surface((1000,750))
    alpha = 0
    BLACK_WINNER.fill((0,0,0))
    BLACK_WINNER.set_alpha(alpha)

    while True:
        if BLACK_WINNER.get_alpha() >= 255:
            WIN.blit(BLACK_WINNER, (0,0))
            pygame.display.update()
            break
        else:
            time.sleep(0.03)

            alpha += 5
            BLACK_WINNER.set_alpha(alpha)
            WIN.blit(BLACK_WINNER, (0,0))
            pygame.display.update()
            continue
    time.sleep(1)

def draw_intro():
    pygame.draw.rect(WIN,RED,BLACK_SCREEN)
    pygame.display.update()
    time.sleep(1)

    TADA_SOUND.play()
    intro_text = INTRO_FONT.render("MADE BY FRANKLIN", 1, WHITE)
    WIN.blit(intro_text, (WIDTH//2 - intro_text.get_width()//2, HEIGHT//2 - intro_text.get_height()//2))
    pygame.display.update()
    time.sleep(2)

    pygame.draw.rect(WIN,BLACK,BLACK_SCREEN)
    pygame.display.update()
    time.sleep(1)

async def main():
    global COUNT_DOWN

    COUNT_DOWN = 3

    red = pygame.Rect(random.randint(WIDTH // 2 + 5,WIDTH - SPACESHIP_WIDTH),random.randint(0,HEIGHT - SPACESHIP_HEIGHT),SPACESHIP_WIDTH, SPACESHIP_HEIGHT) # Creating the hitbox(Not the display)
    yellow = pygame.Rect(random.randint(0, WIDTH // 2 - (SPACESHIP_WIDTH + 5)),random.randint(0,HEIGHT - SPACESHIP_HEIGHT),SPACESHIP_WIDTH, SPACESHIP_HEIGHT)

    red_bullets = []
    yellow_bullets = []

    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LALT and len(yellow_bullets) < MAX_BULLETS:
                bullet = pygame.Rect(-100, -100, 0, 0)
                yellow_bullets.append(bullet)

    red_health = 15
    yellow_health = 15
    draw_intro()

    time.sleep(3)
    clock = pygame.time.Clock()

    pygame.event.get()

    run = True
    while run:
        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
                pygame.quit()

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LALT and len(yellow_bullets) < MAX_BULLETS:
                    bullet = pygame.Rect(yellow.x + yellow.width, yellow.y + yellow.height//2 - 2, 10, 5)
                    yellow_bullets.append(bullet)
                    BULLET_FIRE_SOUND.play()

                if event.key == pygame.K_RALT and len(red_bullets) < MAX_BULLETS: # red bullet
                    bullet = pygame.Rect(red.x, red.y + red.height//2 - 2, 10, 5)
                    red_bullets.append(bullet)
                    BULLET_FIRE_SOUND.play()

            if event.type == RED_HIT:
                red_health -= 1
                BULLET_HIT_SOUND.play()

            if event.type == YELLOW_HIT:
                yellow_health -= 1
                BULLET_HIT_SOUND.play()
        winner_text = ""
        if red_health <= 0:
            winner_text = "Yellow Wins!"

        if yellow_health <= 0:
            winner_text = "Red Wins!"

        if winner_text != "":
            draw_winner(winner_text)
            red_bullets.clear()
            yellow_bullets.clear()
            pygame.display.update()
            break

        keys_pressed = pygame.key.get_pressed()
        yellow_handle_movement(keys_pressed, yellow)
        red_handle_movement(keys_pressed, red)

        handle_bullets(yellow_bullets,red_bullets,yellow,red)

        draw_window(red, yellow, red_bullets, yellow_bullets, red_health, yellow_health)

    await asyncio.sleep(0)

    if not COUNT_DOWN:
            return

    COUNT_DOWN = COUNT_DOWN - 1

while True:
    asyncio.run(main())
pmp-p commented 1 year ago

the while True: is wrong, the asyncio.run() in pygbag is a special non-blocking function which override normal asyncio

just call last asyncio.run(main()) with no loop. And be sure to add await asyncio.sleep(0) in the main loop - your actual indentation of it may be wrong , use ```py block to enclose your code in the github markdown

pmp-p commented 1 year ago

i hope you fixed it