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.

Recommended Answers

All 9 Replies

Mindreading is not my strength, so please post some code!

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!

No even by using this the repetition continues

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.

Why use tabs when you could use 4 spaces like everybody else.

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.

Hey thanks shadwickman. It really worked.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.