Ask coding questions

← Back to all posts
Drawing a rectangle
MercyMuchangi (4)

How can I draw a rectangle using a for loop

Comments
hotnewtop
PattanAhmed (1412)

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

Hope this helps
Please mark my answer if this helps

JosephSanthosh (992)

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.

[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)

[deleted]

@pythoder Please mark as answered if this answered you questions

[deleted]

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

[deleted]
MercyMuchangi (4)

@pythoder why do we need to use random?

Coder100 (18902)

to make it generate any size @MercyMuchangi