2
import pygame, sys

pygame.init()

display_width = 800
display_height = 500

white = (255, 255, 255)

display = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('Pong')
clock = pygame.time.Clock()

platform_y = display_height * 0.38

def platform(color, x, y):
    global platform_y

    pygame.draw.rect(display, color, (x, y, 25, 100))

    pygame.display.update()
    clock.tick(80)

def ball():
    pygame.draw.circle(display, white, (400, 250), 12)
    pygame.display.update()

def game():
    global platform_y
    y_change = 0
    while True:
        platform(white, 100, platform_y)
        platform(white, 675, platform_y)
        ball()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit(0)
            elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                sys.exit(0)

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    y_change -= 2
                if event.key == pygame.K_DOWN:
                    y_change += 2

            if event.type == pygame.KEYUP:
                    if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                        y_change = 0

        platform_y += y_change
        pygame.display.update()
        clock.tick(80)

if __name__ == '__main__':
    game()

I guess it's not the best code but I've been messing around with pygame and got to the point when I have to create Pong, although I'm not sure why the rectangles (platforms) are just getting higher instead of going up and down (I know that both platforms will rise together, will fix that, this is just for test)

Ilija
  • 1,556
  • 1
  • 9
  • 12
Kefir
  • 23
  • 3

1 Answers1

3

You had couple of issues with your code:

  1. You have to call pygame.display.update only once, not after every pygame.draw,
  2. clock.tick is set in main game loop, not in other places,
  3. You have to clear the screen before drawing.

Here is what will probably work as you expect:

import pygame, sys

pygame.init()

display_width = 800
display_height = 500

white = (255, 255, 255)

display = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('Pong')
clock = pygame.time.Clock()

platform_y = display_height * 0.38


def platform(color, x, y):
    global platform_y

    pygame.draw.rect(display, color, (x, y, 25, 100))


def ball():
    pygame.draw.circle(display, white, (400, 250), 12)


def game():
    global platform_y
    y_change = 0

    while True:
        platform(white, 100, platform_y)
        platform(white, 675, platform_y)
        ball()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit(0)
            elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                sys.exit(0)

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    y_change -= 2
                if event.key == pygame.K_DOWN:
                    y_change += 2

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                    y_change = 0

        platform_y += y_change

        pygame.display.update()
        clock.tick(80)
        display.fill((0, 0, 0))


if __name__ == '__main__':
    game()

Couple of advices:

  1. Don't use global! It's smells on bad code. There are certainly better ways to achieve what you wanted without using it.
  2. Try to avoid a lot of conditions nesting. It's very hard to read, and will require much mental energy to read after day or two.

Happy coding!

Ilija
  • 1,556
  • 1
  • 9
  • 12
  • Oh, right! I forgot to clear the screen. I knew making a gloabl was wrong, but didn't actually se any other way as moving was in the game function. Looking at your answer I feel damn embarrased, these are so obvious mistakes. Thank you! – Kefir Feb 13 '18 at 15:34
  • Actually, now as I look at that code I see no sense to make a global, this is actually way easier than i thought. – Kefir Feb 13 '18 at 15:36
  • Keep up the enthusiasm and you'll make a progress for sure. Don't forget to mark the answer and close this question. – Ilija Feb 13 '18 at 15:55