4) Drawing into Screen

import pygame

class Simulator(object):
	def main(self , screen):
		robot = pygame.image.load("car60_40.png")
		
		while 1:
			for event in pygame.event.get():
				if event.type == pygame.QUIT:
					return
				if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
					return
			screen.blit(robot,(320,240))
			pygame.display.flip()


if __name__ == '__main__':
	pygame.init()
	screen = pygame.display.set_mode((640,480))
	Simulator().main(screen)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

The next thing that is new is that we call pygame.display.flip. Drawing directly to the screen is usually a very bad idea. Instead, we have a invisible buffer that we draw onto. When we have finished drawing we make the buffer visible. That way we get flicker-free animations

(adsbygoogle = window.adsbygoogle || []).push({});

5) Set Background Color

red = (200, 0, 0)
blue = (0, 0, 255)
green = (0, 155, 0)
yellow = (155, 155, 0)
white = (255, 255, 255)
black = (0, 0, 0) 

screen.fill(white)
1
2
3
4
5
6
7
8

We have passed in a sequence of three values: red, green and blue. Each one can be a value between 0 and 255. Since we set all to zero, we get a black screen. You should experiment with different values for red, green and blue.

6) Using Less CPU

clock = pygame.time.Clock()
clock.tick(30)
1
2

Final Code

(adsbygoogle = window.adsbygoogle || []).push({});
import pygame

"""Define color"""
red = (200, 0, 0)
blue = (0, 0, 255)
green = (0, 155, 0)
yellow = (155, 155, 0)
white = (255, 255, 255)
black = (0, 0, 0)

class Simulator(object):
	def main(self , screen):
		clock = pygame.time.Clock()
		robot = pygame.image.load("car60_40.png")
		while 1:
			clock.tick(30)
			for event in pygame.event.get():
				if event.type == pygame.QUIT:
					return
				if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
					return
			screen.fill(white)
			screen.blit(robot,(320,240))
			pygame.display.flip()






if __name__ == '__main__':
	pygame.init()
	pygame.display.set_caption("Draw")
	screen = pygame.display.set_mode((640,480))
	Simulator().main(screen)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

EXECRISE:

  1. Create a window with a white background color.
  2. Create a window with a red background color.
  3. Experiment with setting different background colors. If you are not familiar with RGB values then spend a little extra time to figure out how to get colors like yellow, brown, cyan etc.
  4. Create a program that asks the user to specify the values for red, green and blue. Check that the values are in the valid range (0-255) and then use these for the background color. 5.Create a program that upon start-up checks the time of the day and sets the brightness of the background color accordingly. Use a blue scale. If it is midnight, the screen should be black. If it is midday, the screen should be bright blue. Any other time the background should be something in between corresponding to the brightness outside.