Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Why is this code not running?

+3 votes
asked Sep 5, 2020 by Rohan (280 points)
import pygame
pygame.init()
ds1 = pygame.display.set_mode((500,500))
pygame.display.set_caption("Cosmopolitan Mini Game")
re1x = 100
re1y = 150
re2x = 300
re2y = 300
re1 = 10
re2 = 30
bl=(0,0,0)
rmain = True
def winf() :
    ds1.fill(bl)
    pygame.draw.rect(ds1, (255,5,5), (re1x, re1y,20,20))
    pygame.draw.rect(ds1, (5,5,255), (re2x, re2y,30,30))
    pygame.display.update
while rmain :
    pygame.time.delay(200)
    if re2x < re1x-10 :
        re2x = re2x + re2
        winf()
    elif re2x > re1x+10 :
        winf()
        re2x = re2x - re2
    elif re2y < re1y-10 :
        re2y = re2y + re2
    elif re2y > re1y+10 :
        re2y = re2y - re2
    else :
        rmain = False
    for event in pygame.event.get() :
        if event.type == pygame.QUIT :
            rmain = False
    km1 = pygame.key.get_pressed()
    if km1[pygame.K_LEFT]:
        re1x += re1
    if km1[pygame.K_RIGHT]:
        re1x -= re1
    if km1[pygame.K_UP]:
        re1y -= re1
    if km1[pygame.K_DOWN]:
        re1y += re1
    winf()
pygame.quit()

2 Answers

0 votes
answered Sep 7, 2020 by Gregory Terrell (140 points)

Replacing 

ds1 = pygame.display.set_mode((500,500))
 

with 

ds1 = pygame.display.set_mode([500, 500])
(removing the inner parentheses with brackets) should at least get your window to appear. 
And changing the pygame.display.update to pygame.display.flip() allows your program to execute properly, or at least it does when testing on my end. Here is what the modified code I got to run looks like in full:
import pygame

pygame.init()
ds1 = pygame.display.set_mode([500, 500])
pygame.display.set_caption("Cosmopolitan Mini Game")
re1x = 100
re1y = 150
re2x = 300
re2y = 300
re1 = 10
re2 = 30
bl = (0, 0, 0)
rmain = True


def winf():
    ds1.fill((255, 255, 255))
    pygame.draw.rect(ds1, (255, 5, 5), (re1x, re1y, 20, 20))
    pygame.draw.rect(ds1, (5, 5, 255), (re2x, re2y, 30, 30))

    # Flip the display
    pygame.display.flip()


while rmain:
    pygame.time.delay(200)
    if re2x < re1x - 10:
        re2x = re2x + re2
        winf()
    elif re2x > re1x + 10:
        winf()
        re2x = re2x - re2
    elif re2y < re1y - 10:
        re2y = re2y + re2
    elif re2y > re1y + 10:
        re2y = re2y - re2
    else:
        rmain = False
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            rmain = False
    km1 = pygame.key.get_pressed()
    if km1[pygame.K_LEFT]:
        re1x += re1
    if km1[pygame.K_RIGHT]:
        re1x -= re1
    if km1[pygame.K_UP]:
        re1y -= re1
    if km1[pygame.K_DOWN]:
        re1y += re1
    winf()
pygame.quit()
–1 vote
answered Sep 19, 2020 by Arti Ahirwar (180 points) 1 flag
import pygame

pygame.init()
ds1 = pygame.display.set_mode([500, 500])
pygame.display.set_caption("Cosmopolitan Mini Game")
re1x = 100
re1y = 150
re2x = 300
re2y = 300
re1 = 10
re2 = 30
bl = (0, 0, 0)
rmain = True


def winf():
    ds1.fill((255, 255, 255))
    pygame.draw.rect(ds1, (255, 5, 5), (re1x, re1y, 20, 20))
    pygame.draw.rect(ds1, (5, 5, 255), (re2x, re2y, 30, 30))

    # Flip the display
    pygame.display.flip()


while rmain:
    pygame.time.delay(200)
    if re2x < re1x - 10:
        re2x = re2x + re2
        winf()
    elif re2x > re1x + 10:
        winf()
        re2x = re2x - re2
    elif re2y < re1y - 10:
        re2y = re2y + re2
    elif re2y > re1y + 10:
        re2y = re2y - re2
    else:
        rmain = False
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            rmain = False
    km1 = pygame.key.get_pressed()
    if km1[pygame.K_LEFT]:
        re1x += re1
    if km1[pygame.K_RIGHT]:
        re1x -= re1
    if km1[pygame.K_UP]:
        re1y -= re1
    if km1[pygame.K_DOWN]:
        re1y += re1
    winf()
pygame.quit()
commented Sep 19, 2020 by xDELLx (10,500 points)
is  Arti  trying to improve copy paste skills ?!
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...