0
votes

I'm using this bit of code in pycharm, but none of the print for events works(the quit, mouse button click or key pressing on keyboard)

Although I see the pygame window but the events doesn't work.

I also used the get() instead of wait() but still no luck.

any ideas?

import pygame

pygame.init()
screen = pygame.display.set_mode((800, 600))

while True:
    event = pygame.event.wait()

    if event.type == pygame.QUIT:
        print('Quit')

    if event.type == pygame.KEYDOWN:
        print('Key Down')
        print(event.key)
        print(event.unicode)

    if event.type == pygame.KEYUP:
        print('Key Up')
        print(event.key)

    if event.type == pygame.MOUSEBUTTONDOWN:
        print('Mouse Button Down')
        print(event.pos)
        print(event.button == pygame.BUTTON_RIGHT)
        print(event.button == pygame.BUTTON_LEFT)

    if event.type == pygame.MOUSEBUTTONUP:
        print('Mouse Button Up')
        print(event.pos)
        print(event.button == pygame.BUTTON_RIGHT)
        print(event.button == pygame.BUTTON_LEFT)

    if event.type == pygame.MOUSEMOTION:
        print('Mouse Motion')
        print(event.pos)
        print(event.rel)

UPDATE I found out that it's a problem with pycharm. when I run any code with pygame just a black pygame windows pop up, it doesn't run any other code(events, filling window with color ,...). Even the pygame window is not in dimensions that I've given.

Here another code example.

import pygame

pygame.init()
screen = pygame.display.set_mode((200, 200))

red = (255, 0, 0)
screen.fill(red)
pygame.display.update()
pygame.time.delay(10000)

when I run it in VS code:

Vs code pygame test

and when I run it in pycharm :( :

Pycharm pygame test

also I'm defining the same Interpreter for both VS code and pycharm and I've already reinstalled pygame package.

2
What do you see in your PyCharm run console? Your print statements appear to be working there. - DaveStSomeWhere
@RSH It's working for me too. I hope you are not expecting to see the prints in pygame window, because the prints will rather appear in IDE's console. - Sagar Gupta
@SagarGuptaFTW No i know that it must appear in IDE console. I found out that it is rather a problem with pycharm cause I tested it in VS code and it worked!! - RSH
@RSH It's strange, I'm using PyCharm, working for me. It would be interesting to find the cause in your case :) - Sagar Gupta
@DaveStSomeWhere nothing prints there! - RSH

2 Answers

0
votes

You can try to use a full game loop, as this would update the window repeatedly.

Try:

import pygame

pygame.init()
screen = pygame.display.set_mode((200, 200))

red = (255, 0, 0)
running = True
while running:
    screen.fill(red)
    pygame.display.flip()
    for event in pygame.event.get():
        if event.type = pygame.QUIT:
            running = False
pygame.quit()
0
votes

i was using pycharm but all the code did not wok so i right clicked it with the mouse and selected run in python cnsole. OR :::::::::::: SHIFT + ALT + E