-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpygame.py
227 lines (157 loc) · 8.77 KB
/
pygame.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# In this example.py you will learn how to make a very simple game using the pygame library.
# One of the best ways of learning to program is by writing games.
# Pygame is a collection of modules in one package.
# You will need to install pygame.
# To do so:
# 1) open the command line interface on your computer,
# 2) cd to the directory that this task is located in,
# 3) follow the instructions here: https://www.pygame.org/wiki/GettingStarted
# 4) if you need help using pip, see here: https://projects.raspberrypi.org/en/projects/using-pip-on-windows
import pygame # Imports a game library that lets you use specific functions in your program.
import random # Import to generate random numbers.
# Initialize the pygame modules to get everything started.
pygame.init()
# The screen that will be created needs a width and a height.
screen_width = 1040
screen_height = 680
screen = pygame.display.set_mode((screen_width,screen_height)) # This creates the screen and gives it the width and height specified as a 2 item sequence.
# This creates the player and gives it the image found in this folder (similarly with the enemy image).
player = pygame.image.load("image.png")
enemy1 = pygame.image.load("enemy.png")
enemy2 = pygame.image.load("enemy.png")
enemy3 = pygame.image.load("enemy.png")
prize = pygame.image.load("prize.jpg")
# Get the width and height of the images in order to do boundary detection (i.e. make sure the image stays within screen boundaries or know when the image is off the screen).
image_height = player.get_height()
image_width = player.get_width()
enemy_height = enemy1.get_height()
enemy_width = enemy1.get_width()
prize_height = prize.get_height()
prize_width = prize.get_width()
print("This is the height of the player image: " +str(image_height))
print("This is the width of the player image: " +str(image_width))
# Store the positions of the player and enemy as variables so that you can change them later.
playerXPosition = 100
playerYPosition = 50
# Make the enemy start off screen and at a random y position.
enemy1XPosition = screen_width
enemy1YPosition = random.randint(0, screen_height - enemy_height)
enemy2XPosition = screen_width
enemy2YPosition = random.randint(0, screen_height - enemy_height)
enemy3XPosition = screen_width
enemy3YPosition = random.randint(0, screen_height - enemy_height)
prizeXPosition = screen_width
prizeYPosition = random.randint(0,screen_height - prize_height)
# This checks if the up or down key is pressed.
# Right now they are not so make them equal to the boolean value (True or False) of False.
# Boolean values are True or False values that can be used to test conditions and test states that are binary, i.e. either one way or the other.
keyUp= False
keyDown = False #add additional keystrokes
keyRight = False
keyLeft = False
# This is the game loop.
# In games you will need to run the game logic over and over again.
# You need to refresh/update the screen window and apply changes to
# represent real time game play.
while 1: # This is a looping structure that will loop the indented code until you tell it to stop(in the event where you exit the program by quitting). In Python the int 1 has the boolean value of 'true'. In fact numbers greater than 0 also do. 0 on the other hand has a boolean value of false. You can test this out with the bool(...) function to see what boolean value types have. You will learn more about while loop structers later.
screen.fill(0) # Clears the screen.
screen.blit(player, (playerXPosition, playerYPosition))# This draws the player image to the screen at the postion specfied. I.e. (100, 50).
screen.blit(enemy1, (enemyXPosition, enemyYPosition))
screen.blit(enemy2, (enemy2XPosition, enemy2YPosition))
screen.blit(enemy3, (enemy3XPosition, enemy3YPosition))
screen.blit(prize, (prizeXPosition, prizeYPosition)
pygame.display.flip()# This updates the screen.
# This loops through events in the game.
for event in pygame.event.get():
# This event checks if the user quits the program, then if so it exits the program.
if event.type == pygame.QUIT:
pygame.quit()
exit(0)
# This event checks if the user press a key down.
if event.type == pygame.KEYDOWN:
# Test if the key pressed is the one we want.
if event.key == pygame.K_UP: # pygame.K_UP represents a keyboard key constant.
keyUp = True
if event.key == pygame.K_DOWN:
keyDown = True
if event.key == pygame.K_RIGHT:
keyRight = True
if event.key == pygame.K_DOWN:
keyDown = True
# This event checks if the key is up(i.e. not pressed by the user).
if event.type == pygame.KEYUP:
# Test if the key released is the one we want.
if event.key == pygame.K_UP:
keyUp = False
if event.key == pygame.K_DOWN:
keyDown = False
if event.key == pygame.K_RIGHT:
keyRight = False
if event.key == pygame.K_DOWN:
keyDown = False
# After events are checked for in the for loop above and values are set,
# check key pressed values and move player accordingly.
# The coordinate system of the game window(screen) is that the top left corner is (0, 0).
# This means that if you want the player to move down you will have to increase the y position.
if keyUp == True:
if playerYPosition > 0 : # This makes sure that the user does not move the player above the window.
playerYPosition -= 10
if keyDown == True:
if playerYPosition < screen_height - image_height:# This makes sure that the user does not move the player below the window.
playerYPosition += 10
if keyLeft == True:
if playerXPosition > 0 : # This makes sure that the user does not move the player above the window.
playerXPosition -= 10
if keyRight == True:
if playerXPosition < screen_width - image_width:# This makes sure that the user does not move the player below the window.
playerXPosition += 10
# Check for collision of the enemy with the player.
# To do this we need bounding boxes around the images of the player and enemy.
# We the need to test if these boxes intersect. If they do then there is a collision.
# Bounding box for the player:
playerBox = pygame.Rect(player.get_rect())
# The following updates the playerBox position to the player's position,
# in effect making the box stay around the player image.
playerBox.top = playerYPosition
playerBox.left = playerXPosition
# Bounding box for the enemy:
enemy1Box = pygame.Rect(enemy1.get_rect())
enemy1Box.top = enemy1YPosition
enemy1Box.left = enemy1XPosition
enemy2Box = pygame.Rect(enemy2.get_rect())
enemy2Box.top = enemy2YPosition
enemy2Box.left = enemy2XPosition
enemy3Box = pygame.Rect(enemy3.get_rect())
enemy3Box.top = enemy3YPosition
enemy3Box.left = enemy3XPosition
prizeBox = pygame.Rect(prize.get_rect())
prizeBox.top = prizeXPosition
prizeBox.left = prizeYPosition
# Test collision of the boxes:
if playerBox.colliderect(enemyBox):
# Display losing status to the user:
print("You lose!")
# Quit game and exit window:
pygame.quit()
exit(0)
# If the enemy is off the screen the user wins the game:
if enemyXPosition < 0 - enemy_width and enemy2XPosition < 0 - enemy_width and enemy3XPosition < 0 - enemy_width :
# Display wining status to the user:
print("You win!")
# Quite game and exit window:
#pygame.quit()
#exit(0)
enemy1XPosition = screen_width
enemy1YPosition = random.randint(0, screen_height - enemy_height)
enemy2XPosition = screen_width
enemy2YPosition = random.randint(0, screen_height - enemy_height)
enemy3XPosition = screen_width
enemy3YPosition = random.randint(0, screen_height - enemy_height)
prizeXPosition = screen_width
prizeYPosition = random.randint(0,screen_height - prize_height)
# Make enemy approach the player.
enemy1XPosition -= 2
enemy2XPosition -= 4
enemy3XPosition -= 1
prizeXPosition -= 0.5
# ================The game loop logic ends here. =============