Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 107 additions & 26 deletions wiki/pygbag-debug/README.md
Original file line number Diff line number Diff line change
@@ -1,39 +1,120 @@
pygbag comes with an interactive Python-like REPL that can be used for debugging.
import pygame
import random

## to open
To open the REPL, visit `http://localhost:8000/#debug` instead of just `http://localhost:8000`.
pygame.init()

To get debugger at runtime, open the [javascript console](https://developer.mozilla.org/en-US/docs/Learn/Common_questions/Tools_and_setup/What_are_browser_developer_tools#the_javascript_console) and run `debug()` by typing in `debug()` followed by pressing enter.
# إعدادات الشاشة
WIDTH, HEIGHT = 600, 400
win = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("لعبة الثعبان")

## to use
The REPL is a Python REPL that has loaded the project. This means if your pygbag main file includes a `main() function`, calling `type(main)` will return `<class 'function'>` instead of a `NameError`.
# ألوان
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)

It also implements a variety of shell-like commands, defined in the [shell](https://github.com/pygame-web/pygbag/blob/72b34546e23086c78f5b193c05e3e961b807f214/src/pygbag/support/cpythonrc.py#L266) class, that are reminiscent of shell languages common on Linux systems. These can be used in the REPL.
For example,
```bash
>>> cat main.py
```
will dump the binary contents of `main.py` into the REPL, if `main.py` exists in your pygbag project directory.
# إعدادات الثعبان
snake_block = 20
snake_speed = 15

Other helpful commands are available. For example,`rx` takes a list of files and downloads them from the browser's filesystem to a user's filesystem. For example,
```bash
>>> rx main.py assets/image.png assets/otherimage.png
```
will try to download the given three files.
clock = pygame.time.Clock()

## Python versus browser logging
`print` statements will write to this REPL instead of the browser's console.
font_style = pygame.font.SysFont(None, 35)
score_font = pygame.font.SysFont(None, 25)

To write to the browser's console instead, use `platform.console.log`. For example,
```py
import sys, platform
if sys.platform == "emscripten":
platform.console.log("logged message")
```
def your_score(score):
value = score_font.render("النتيجة: " + str(score), True, WHITE)
win.blit(value, [0, 0])

def message(msg, color, x, y):
mesg = font_style.render(msg, True, color)
win.blit(mesg, [x, y])

def gameLoop():
game_over = False
game_close = False

x1 = WIDTH // 2
y1 = HEIGHT // 2

x1_change = 0
y1_change = 0

snake_List = []
Length_of_snake = 1

[edit this page](https://github.com/pygame-web/pygame-web.github.io/edit/main/wiki/pygbag-debug/README.md)
foodx = round(random.randrange(0, WIDTH - snake_block) / 20.0) * 20.0
foody = round(random.randrange(0, HEIGHT - snake_block) / 20.0) * 20.0

while not game_over:

while game_close:
win.fill(BLACK)
message("خسرت! اضغط Q للخروج أو C للعب مجددًا", RED, 50, HEIGHT // 3)
your_score(Length_of_snake - 1)
pygame.display.update()

for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
game_over = True
game_close = False
if event.key == pygame.K_c:
gameLoop()

for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x1_change = -snake_block
y1_change = 0
elif event.key == pygame.K_RIGHT:
x1_change = snake_block
y1_change = 0
elif event.key == pygame.K_UP:
y1_change = -snake_block
x1_change = 0
elif event.key == pygame.K_DOWN:
y1_change = snake_block
x1_change = 0

if x1 >= WIDTH or x1 < 0 or y1 >= HEIGHT or y1 < 0:
game_close = True

x1 += x1_change
y1 += y1_change
win.fill(BLACK)
pygame.draw.rect(win, GREEN, [foodx, foody, snake_block, snake_block])

snake_Head = []
snake_Head.append(x1)
snake_Head.append(y1)
snake_List.append(snake_Head)

if len(snake_List) > Length_of_snake:
del snake_List[0]

for x in snake_List[:-1]:
if x == snake_Head:
game_close = True

for x in snake_List:
pygame.draw.rect(win, WHITE, [x[0], x[1], snake_block, snake_block])

your_score(Length_of_snake - 1)

pygame.display.update()

if x1 == foodx and y1 == foody:
foodx = round(random.randrange(0, WIDTH - snake_block) / 20.0) * 20.0
foody = round(random.randrange(0, HEIGHT - snake_block) / 20.0) * 20.0
Length_of_snake += 1

clock.tick(snake_speed)

pygame.quit()
quit()

gameLoop()