clockworkpi

Pygame development help needed

Hi guys,

Want to dev some games for the Gameshell, but having trouble with even the most basic of things.

I was following this link to get something simple going, and no matter what I do, any key quits the app, and I’m not sure why.

I was taking some key press ideas from Programming GameShell games with Python and the Arcade library and since I was unable to install Arcade on my Gameshell, I decided to go the Pygame route as it’s already installed by default.

FYI, what I did was create a test.py in the games folder, and created a .sh file in the Launcher folder called Test.sh which just runs python ~/games/test.py - this works as I can edit the python file in nano directly, save it, then just click the “Test” game on the launcher menu. Is this how you guys test/do things for the gameshell?

Basically, the problem is that if I press the Menu button, it quits as expected, but if I push any other key, it also quits, instead of moving the rectangle as per this code:

import pygame
successes, failures = pygame.init()
print("{0} successes and {1} failures".format(successes, failures))


screen = pygame.display.set_mode((720, 480))
clock = pygame.time.Clock()
FPS = 60  # Frames per second.
KEY_MENU = pygame.K_ESCAPE
KEY_UP = pygame.K_UP
KEY_DOWN = pygame.K_DOWN

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
# RED = (255, 0, 0), GREEN = (0, 255, 0), BLUE = (0, 0, 255).

rect = pygame.Rect((0, 0), (32, 32))
image = pygame.Surface((32, 32))
image .fill(WHITE)  

while True:
    clock.tick(FPS)

    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == KEY_MENU:
                quit()
            elif event.key == KEY_UP:
                rect.move_ip(0, 2)
            elif event.key == KEY_DOWN:
                rect.move_ip(0, -2)

    screen.fill(BLACK)
    screen.blit(image, rect)
    pygame.display.update()  # Or pygame.display.flip()

I feel that if I can’t get Pygame stuff working, I might resort to learning Love2D, as it is supported and installed too and can be run on other platforms as well.

I’ve tested your code , seems it worked as you expected

you can run any program in the ssh terminal directly
just specified the DISPLAY env variable in linux (gameshell) correctly as the screenshot above

and for the record
this testing method may occur the focus problem in future because some program may steal the focus point away so that the launcher may not being response after you tried what you want, it does not mean the launcher is dead or broken
just sudo reboot after you done the testing job

@guu thanks so much for testing this and for the display tip. Very useful!
Please paste the exact code you used. For me, each time I pushed any button it would quit, so I’d like to compare your code with what I wrote. Ultimately, I’d like to cross-dev if possible, for example using notepad++ and then run some command to copy and run the code on my gameshell. I’ll look more into this soon.

https://paste.ubuntu.com/p/xnnWpCJgkk/

Thanks @guu - I am unsure why my gameshell wasn’t playing ball last night, but today that code ran fine.

On another note, I’ve got a sort of cross-dev setup going now.
Using WinSCP, I can ssh into the GS, edit the .py files directly in Notepad++, save it, then run the DISPLAY:=0 python test.py in the WinSCP commandline. This works quite well I have to say.

So I have some more questions I hope someone can clarify for me…

  1. If I wrote/tested my PyGame on the Windows desktop directly (after obviously installing Python and Pygame on Windows), and I worked directly like that, would the resultant code have to be changed in any way (other than keypresses) for the code to run on the GS? Or is my above method of game dev better (using WinSCP)?
  2. I presume PyGame on the GS is “standard”, in so far that I can follow examples on that website and it would work as-is? I ask, as I obviously want to load graphics and sounds into the game to be used.
  3. Probably too early at this point, but once my game is finished, and I want the game to be distributed for the GS, how do you change the Launcher icon that is shown above your .sh file?

Thanks!

1: I have not using windows for years, But I think there won’t be much to change to deploy your game from win to gs by using windows, and linux / or vmware a linux would be a better env to develop games for GS
2: PyGame on the GS is “standard”
3: put a png file inside your game folder with same name, eg: MyGame/MyGame.png

2 Likes