Sign in to follow this  
monp

[Python] Redraw blank screen help

Recommended Posts

I am trying to get n to redraw a blank white screen but it will not work.
import pygame
pygame.init()

def main():
	screen = pygame.display.set_mode((640,480))
	pygame.display.set_caption("Draw Lines Program")
	
	bg = pygame.Surface(screen.get_size())
	bg.convert()
	bg.fill((255,255,255))
	
	clock = pygame.time.Clock()
	run = True
	
	while run:
		clock.tick(30)
		for event in pygame.event.get():
			if event.type == pygame.QUIT:
				run = False
			elif event.type == pygame.MOUSEBUTTONDOWN:
				MouseD = pygame.mouse.get_pos()
			elif event.type == pygame.MOUSEBUTTONUP:
				MouseU = pygame.mouse.get_pos()
				pygame.draw.line(bg, (0,0,0), MouseD, MouseU, 3)
			elif event.type == pygame.KEYDOWN:
				if event.key == pygame.K_f:
					pygame.image.save(bg, "pic1.bmp")
				elif event.key == pygame.K_n:
					screen.blit(bg, (0,0))
				
				
		screen.blit(bg, (0,0))
		pygame.display.flip()
		
if __name__ == "__main__":
	main()

Share this post


Link to post
Share on other sites
The code you are using to "draw a blank white screen" for the N key is the same as the code you use unconditionally each time through the loop to refresh the current screen state: screen.blit(bg, (0,0)). It should be no surprise that the N key therefore fails to do anything visible. It also shouldn't have been hard to pick up on this, seeing as those two lines of source code come one right after the other (whitespace and indentation notwithstanding).

Since 'bg' will be blitted to screen regardless, what the N key should do is cause 'bg' to become a blank white screen. The way to do this is the same way that you do it before the loop starts: bg.fill((255,255,255)).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this