0

Here is my underdeveloped pygame ping-pong game, but my sprites(player&opponent) ain't moving, on giving a keyboard input. And when I quit the program, it yells an error pygame.error: video system not initialized. My pygame is the latest 1.9.6 version with all the files up-to-daee. However, I am certain that pygame.display is generating this error, but I even tried pygame.display.init() and that too didn't worked :(

import pygame

# Initialization
pygame.init()
# Screen, Caption and Icon
width = 800
height = 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('PyGame')
icon = pygame.image.load('ping-pong.png')
pygame.display.set_icon(icon)
# Create Rects
player = pygame.Rect((5, 230), (10, 120))
opponent = pygame.Rect((785, 230), (10, 120))
# Game Variables
playerY_change = 0
opponentY_change = 0

game_over = False
while not game_over:
    # Coloring the Screen
    screen.fill((27, 35, 43))
    # Draw Rects
    pygame.draw.rect(screen, (255,255,255), player)
    pygame.draw.rect(screen, (255,255,255), opponent)
    # Managing Events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_over = True
            pygame.quit()
        if event.type == pygame.KEYUP:
            if event.type == pygame.K_UP:
                opponentY_change -= 3
            if event.type == pygame.K_DOWN:
                opponentY_change += 3
            if event.type == pygame.K_w:
                playerY_change -= 3
            if event.type == pygame.K_s:
                playerY_change += 3
        if event.type == pygame.KEYDOWN:
            if (event.type == pygame.K_UP) or (event.type == pygame.K_DOWN):
                opponentY_change = 0
            if (event.type == pygame.K_w) or (event.type == pygame.K_s):
                playerY_change = 0

    # Moving my sprites
    player.y += playerY_change
    opponent.y += opponentY_change
    # Updating the screen on every iter of loop
    pygame.display.update()
Malik
  • 13
  • 3

1 Answers1

0

Here, you have two different problems :

First the movement is not working because to differentiate the keys, you use event.type to compare where it should be event.key. Try with for example :

if event.key == pygame.K_UP:
   opponentY_change -= 3

The other problem is that when the game is over, you use pygame.quit() but later on the loop, you use pygame.display.update(). You should put the display update at the beginning of the loop.

Log1k
  • 95
  • 6