@OmarionKeesley/

U5Q1 (Insert Name)

Pygame

No description

fork
loading
Files
  • main.py
  • nohup.out
main.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
"""
Welcome to Unit 5 - Quiz 1: Shooting x Falling Squares

Your task is to alter this code to accomplish several specific coding objectives. 

This quiz is OPEN INTERNET. Use everything you know about coding to accomplish the tasks. 

Max Points: 30

TASKS 
1) Change Player color to any color but red (4 points)
2) Change Falling Block color to any color but blue (4 points)
3) Change background color to any color but white (4 points)
4) Add a caption that says the name of this game: Shooting x Falling Squares (4 points)
5) Increase bullet speed to 5 px/frame (4 points)

6)Program your game to print various things when a certain amount of points are reached (+ 10 points):

* Print "NICE!" when score == 10
* Print "GREAT!" when score == 30
* Print "YOU WIN!" when score == 50


**EXTRA CREDIT**
EC1: Double the score for bullet hits (+4 points)

EC2: Program your code so that the shell prints "GAME OVER" when the player collides into a falling block (+10 points)

EC3: BIG BANG ATTACK!

Part1: Create a special Block class called BIGBANG that creates a BIG BANG ATTACK bullet that is 200 px wide and 75 px high. (+20 points)

Part2: Program your BIG BANK ATTACK to fire when the user presses the SPACEBAR. When the user fires, print "BIG BANG ATTACK" to the screen. (+20 points)

Part3: Program your BIG BANG ATTACK to only be able to fire after the PLAYER reaches 10 or more points. (+60 points)


"""
import pygame
import random
 
# Define some colors
BLACK = (0, 0, 0)
WHITE = (0, 255, 0)
RED = (0, 0, 255)
BLUE = (255, 0, 0)
 
# --- Classes
 
 
class Block(pygame.sprite.Sprite):
    
	def __init__(self, color):
		# Call the parent class (Sprite) constructor
		super().__init__()

		self.image = pygame.Surface([20, 15])
		self.image.fill(color)

		self.rect = self.image.get_rect()

	def reset_pos(self):
		""" Reset position to the top of the screen, at a random x location. 
		Called by update() or the main program loop if there is a collision.
		"""
		self.rect.y = random.randrange(-300, -20)
		self.rect.x = random.randrange(0, screen_width)

	def update(self):
		""" Called each frame. """

		# Move block down one pixel
		self.rect.y += 1

		# If block is too far down, reset to top of screen.
		if self.rect.y > 410:
			self.reset_pos()
 
 
class Player(pygame.sprite.Sprite):
    """ This class represents the Player. """
 
    def __init__(self):
        """ Set up the player on creation. """
        # Call the parent class (Sprite) constructor
        super().__init__()
 
        self.image = pygame.Surface([20, 20])
        self.image.fill(RED)
 
        self.rect = self.image.get_rect()
 
    def update(self):
        """ Update the player's position. """
        # Get the current mouse position. This returns the position
        # as a list of two numbers.
        pos = pygame.mouse.get_pos()
 
        # Set the player x position to the mouse x position
        self.rect.x = pos[0]
 
 
class Bullet(pygame.sprite.Sprite):
    """ This class represents the bullet . """
    def __init__(self):
        # Call the parent class (Sprite) constructor
        super().__init__()
 
        self.image = pygame.Surface([4, 10])
        self.image.fill(BLACK)
 
        self.rect = self.image.get_rect()
 
    def update(self):
        """ Move the bullet. """
        self.rect.y -= 5
 
 
# --- Create the window
 
# Initialize Pygame
pygame.init()
 
# Set the height and width of the screen
screen_width = 700
screen_height = 400
screen = pygame.display.set_mode([screen_width, screen_height])
 
# --- Sprite lists
 
# This is a list of every sprite. All blocks and the player block as well.
all_sprites_list = pygame.sprite.Group()
 
# List of each block in the game
block_list = pygame.sprite.Group()
 
# List of each bullet
bullet_list = pygame.sprite.Group()
 
# --- Create the sprites
 
for i in range(50):
    # This represents a block
    block = Block(BLUE)
 
    # Set a random location for the block
    block.rect.x = random.randrange(screen_width)
    block.rect.y = random.randrange(350)
 
    # Add the block to the list of objects
    block_list.add(block)
    all_sprites_list.add(block)
 
# Create a red player block
player = Player()
all_sprites_list.add(player)
 
# Loop until the user clicks the close button.
done = False
 
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
 
score = 0
player.rect.y = 370
 
# -------- Main Program Loop -----------
while not done:
    # --- Event Processing
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
 
        elif event.type == pygame.MOUSEBUTTONDOWN:
            # Fire a bullet if the user clicks the mouse button
            bullet = Bullet()
            # Set the bullet so it is where the player is
            bullet.rect.x = player.rect.x
            bullet.rect.y = player.rect.y
            # Add the bullet to the lists
            all_sprites_list.add(bullet)
            bullet_list.add(bullet)
 
    # --- Game logic
 
    # Call the update() method on all the sprites
    all_sprites_list.update()
 
    # Calculate mechanics for each bullet
    for bullet in bullet_list:
 
        # See if it hit a block
        block_hit_list = pygame.sprite.spritecollide(bullet, block_list, True)
 
        # For each block hit, remove the bullet and add to the score
        for block in block_hit_list:
            bullet_list.remove(bullet)
            all_sprites_list.remove(bullet)
            score += 1
            print(score)
          
        # Remove the bullet if it flies up off the screen
        if bullet.rect.y < -10:
            bullet_list.remove(bullet)
            all_sprites_list.remove(bullet)
 
    # --- Draw a frame
 
    # Clear the screen
    screen.fill(WHITE)
 
    # Draw all the spites
    all_sprites_list.draw(screen)
 
    # Go ahead and update the screen with what we've drawn.
    pygame.display.flip()
 
    # --- Limit to 20 frames per second
    clock.tick(60)
 
pygame.quit()