Skip to content
← Back to Community
Drawing a rectangle
Profile icon
MercyMuchangi

How can I draw a rectangle using a for loop

Voters
Profile icon
NoraTubridy
Profile icon
LEILAPORTUGAL
Profile icon
ArabellaWalters
Profile icon
SelenaGilley
Profile icon
DetectiveBoy1
Profile icon
MercyMuchangi
Comments
hotnewtop
Profile icon
PattanAhmed

@MercyMuchangi Hi,
Check this Picture in which it shows you how to make a rectangle using a For Loop

for loop turtle

Hope this helps
Please mark my answer if this helps

Profile icon
gibbsfreenergy

import pygame
from pygame.locals import *
from sys import exit
from random import *

pygame.init()

screen = pygame.display.set_mode((640, 480), 0,32)
while True:
for event in pygame.event.get():
if event.type == QUIT:
exit()
screen.lock()
for count in range(10):
random_color = (randint(0,255), randint(0,255), randint(0,255))
random_pos = (randint(0,639), randint(0,479))
random_size = (639-randint(random_pos[0], 639), 479-randint(random_pos[1],479))
pygame.draw.rect(screen, random_color, Rect(random_pos, random_size))
screen.unlock()
pygame.display.update()


THIS SHOULD HELP. IF IT HELPS THEN PLEASE SELECT THIS AS THE CORRECT ANSWER.

Profile icon
[deleted]

@pythoder
import turtle
import random
t = turtle.Turtle()
x = random.randint(10, 200)
y = random.randint(20, 100)
for i in range(2):
t.forward(x)
t.left(90)
t.forward(y)

Profile icon
[deleted]

@pythoder Please mark as answered if this answered you questions

Profile icon
[deleted]

@pythoder also, when you want nested loops, you usually want a different variable

Profile icon
[deleted]
Profile icon
MercyMuchangi

@pythoder why do we need to use random?

Profile icon
Coder100

to make it generate any size @MercyMuchangi