0

I'm learning pygame on my mac and I have:

  • python 3.6.1
  • pygame 1.9.3

I just followed a tutorial and tried to create a simple window. But what I found was that after I ran it on the terminal, nothing happened. No window popped out, no error.

I've searched for some solutions and found that using

pythonw test.py could solve my problem, but I tried it and still failed. What's strange was that this worked on other pygame codes that I found on the net.

So here's my code, it would be great if somebody can help me with this problem. I've been searching for the solution for half day. Thanks!

import pygame

display_width = 800
display_height = 600

black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)

pygame.init()
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Bballing')
clock = pygame.time.clock()

ballImg = pygame.image.load('bball.png')

def ball(x,y):
    gameDisplay.blit(ballImg, (x,y))

def game_loop():
    x = (display_width * 0.45)
    y = (display_height * 0.8)

    gameExit = False

    while not gameExit:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameExit = True

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_change = -5
                elif event.key == pygame.K_RIGHT:
                    x_change = 5
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    x_change = 0

        x += x_change

        gameDisplay.fill(white)
        ball(x,y)
        pygame.display.update()

        clock.tick(60)

game_loop()
pygame.quit()
quit()
  • did you run it in console/terminal to see error messages ? inside loop you use `car()` but you didn't define this function. So you should get error message. BTW: you have wrong indensions, all `if` should be inside `for`. Now only first `if` is inside. – furas Dec 17 '17 at 13:35
  • so which python are you using and are you completely sure? Did you type which python - oh yeah and furas is right - you should do ball(x,y) not car(x,y) – Davtho1983 Dec 17 '17 at 13:50
  • After fixing the typos and minor errors it works for me: Change `ball` to `car`, `clock = pygame.time.clock()` to `clock = pygame.time.Clock()`, `event.Type` to `event.type`, add parentheses to `def game_loop():`, add the missing `x_change` variable, and indent the `if event.type` blocks correctly. – skrx Dec 17 '17 at 15:15
  • I fixed the typos but it's still not working. I think the problem is not about the code or there should have been an error before fixing it but there wasn't. –  Dec 17 '17 at 16:26
  • It worked! Though I'm not really sure what was going on, thanks guys. I'll try to figure it out later. –  Dec 17 '17 at 16:45

0 Answers0