1
votes

When I run this code it shows a blank black window and it says x is not defined.

import pygame,sys
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((800,500))
screen.fill((255,255,255))

#basic stuff


 pygame.draw.line(screen,(0,0,0),(500,0),(500,500))
    pygame.draw.rect(screen, (0,255,0), (20,50,460,420))
    #pygame.draw.line(screen, (0,0,0), (500,0),(500,500))
    #pygame.draw.line(screen, (0,0,0), (500,500),(0,500))

#draw

while 1:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == MOUSEBUTTONDOWN:
            pos = pygame.mouse.get_pos()
            x = int(pos[0]//20)
            y = int(pos[1]//20)
            j = int(x*20+20)
            l = int(y*20+20)
            print(x+1,' ',y+1)
        if x > 2 and y > 3 and x < 24 and y < 24:
            pygame.quit()

its not the color. it dose not draw any lines tough i gave the command

 myfont = pygame.font.SysFont("monospace", 20)


    label = myfont.render("Allameh helli 3 stock exchange group",1,0,0,0))
    screen.blit(label, (30, 10))

What are my error(s) here that prevent me from making the screen white and raising that error?

1

1 Answers

0
votes

You need to add this line in your while loop but not in your for loop :

screen.fill([255, 255, 255])

and this line of code outside and after the while loop:

pygame.quit()

You have not defined x nor y yet. You will need to redefine those variables, including pos to prevent the error. The error is that your if statement:

        if x > 2 and y > 3 and x < 24 and y < 24:
            pygame.quit()

was not in line with the one above it. The program then could not see what x or y was, raising the error. Your while loop is fine by the way. I hope this helps you!