Python Not Responding when I Run it

Question:

Python doesn’t respond when I play it. No syntax error appears either. Not sure what is wrong. I tried running another game I made which worked fine so I don’t think it’s my computer.
error message

This is my code:

import pygame 

pygame.init()

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

pygame.display.set_caption("Draft")

icon = pygame.image.load("doctor.png")

pygame.display.set_icon(icon)

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

def draw_ground():
    groundx = 20
    groundy = 30
    Ground = pygame.Rect(groundx,groundy,width = 800,height = 20)
    pygame.draw.rect(screen,green,Ground)

running = True
while running:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    draw_ground()

It isn’t complete yet, I’m trying to test it first before moving ahead.

Answers:

There is a typo in your code:

black = (0,0,0) red = (255,0,0)

It has to be

black = (0,0,0) 
red = (255,0,0)

However, there is more.


pygame.Rect does not accept keyword arguments:

Ground = pygame.Rect(groundx,groundy,width = 800,height = 20)

Ground = pygame.Rect(groundx,groundy,800,20)

You have to update the display by calling either pygame.display.update() or pygame.display.flip():

clock = pygame.time.Clock()
running = True
while running:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # clear dispaly
    screen.fill(0)

    # draw objects
    draw_ground()

    # update dispaly
    pygame.display.flip()

The typical PyGame application loop has to:

Answered By: Rabbid76
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.