Hi,

I am trying to build a rectangle which changes its size with respect to the input given. My problem is that on using the surface.blit function the object start to replicate when the size of the screen is greater than the object. So many rectangle appear instead of one. So please help me to avoid these repetition of object.

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

pygame.init()
screen=pygame.display.set_mode((640,480),0,32)
while 1:
	for event in pygame.event.get():
		if event.type == QUIT:
			exit()

	pygame.draw.rect(screen,(0,255,0),(50,50,100,100))
	screen.blit(screen,(50,50))
	pygame.display.flip()
	pygame.display.update()

This is my code . Here the rectangle start to repeat down continously with a certain indent from the left. Anyways to rectify it.

Mods note:
For Python on Daniweb:
Please use the [code=python] and [/code] tag pair to enclose your python code to preserve the proper indentations.

You haven't re-blitted the entire screen, i.e. refilled every pixel of it in order to fully wipe + refresh it. For this, add a screen.fill((0,0,0)) before you re-blit the rectangle. This will cover the screen in black again before redrawing the new rectangle. Hope that helps!

It works - I get only one rectangle showing with it.

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

pygame.init()
screen=pygame.display.set_mode((640,480),0,32)
while 1:
	for event in pygame.event.get():
		if event.type == QUIT:
			exit()

	screen.fill((0,0,0)) # put it here
	pygame.draw.rect(screen,(0,255,0),(50,50,100,100))
	screen.blit(screen,(50,50))
	pygame.display.flip()
	pygame.display.update()

You need it called before every blit for the rectangle.

Thanks for calling me out on that. I got used to using tabs when I started out with Python, but switched to the 4-space standard. I'm still struggling with this though, as I am too used to being able to hit backspace once to return to the indentation of the outer level, so about a week ago I switched Notepad++ back to use tabs.

Just a note: Folks that use tabs, particularly tabs and spaces mixed will not be able to use the utility program that comes with the new Python3 and converts Python2 code to Python3 code.

This article has been dead for over six months. Start a new discussion instead.