-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpygame_tutorial_3.py
83 lines (53 loc) · 1.84 KB
/
pygame_tutorial_3.py
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
'''
Change the rectangle's color when we click on the moving rectangle
'''
# IMPORTS
import sys
import pygame
import random
# INITIALIZE PYGAME
pygame.init()
# CREATE THE SCREEN -------------------------------
''' ADD ME! '''
screen_width=700
screen_height=500
''' CHANGE ME '''
screen = pygame.display.set_mode((screen_width, screen_height))
# red, green, blue
screen.fill( (100, 100, 200) )
# ADD A RECTANGLE ---------------------------------
# width, height
my_shape = pygame.Surface((100, 50))
# red, green, blue
my_shape.fill( (0, 0, 255) )
my_shape_rect = my_shape.get_rect()
# x, y
my_shape_rect.topleft = (200,150)
# DISPLAY IT ALL ----------------------------------
screen.blit(my_shape, my_shape_rect)
pygame.display.update()
# RUN THE GAME ------------------------------------
''' ADD ME '''
amt_to_move = 2
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
mouse_position = pygame.mouse.get_pos()
if my_shape_rect.collidepoint(mouse_position):
red = random.randint(0, 255)
blue = random.randint(0, 255)
green = random.randint(0, 255)
my_shape.fill((red, green, blue))
''' ADD ME! '''
# Move Rectangles's Position
my_shape_rect = my_shape_rect.move((amt_to_move, 0)) # <-- Add me!
# Check If Went Off Edge
if my_shape_rect.right > screen_width or my_shape_rect.left < 0: # <-- Add me!
amt_to_move *= -1 # <-- Add me!
# Display It All To The Screen
screen.fill((100,100,200))
screen.blit(my_shape, my_shape_rect)
pygame.display.update()