1+ """Code from part 2"""
2+
3+ import sys
4+ import pygame
5+ from pygame .locals import *
6+ from random import randint , random
7+
8+ FPS = 60
9+ WIDTH , HEIGHT = 400 , 400
10+
11+ pygame .init ()
12+ screen = pygame .display .set_mode ((WIDTH , HEIGHT ))
13+ pygame .display .set_caption ("SHMUP!" )
14+ clock = pygame .time .Clock ()
15+
16+ player_img = pygame .image .load ("player.png" ).convert_alpha ()
17+ player_img = pygame .transform .scale (player_img , (36 , 56 ))
18+ cookie_img = pygame .image .load ("cookie.png" ).convert_alpha ()
19+ bullet_spritesheet = pygame .image .load ("bullet.png" ).convert_alpha ()
20+ bullet_imgs = []
21+ for x in range (4 ):
22+ bullet_img = bullet_spritesheet .subsurface (x * 3 , 0 , 3 , 7 )
23+ bullet_img = pygame .transform .scale (bullet_img , (12 , 28 ))
24+ bullet_imgs .append (bullet_img )
25+ bg_img = pygame .image .load ("background.png" ).convert ()
26+
27+ class Player (pygame .sprite .Sprite ):
28+ def __init__ (self ):
29+ super ().__init__ ()
30+
31+ self .image = player_img
32+ self .rect = player_img .get_rect ()
33+
34+ self .x = 180
35+ self .y = 340
36+
37+ def update (self ):
38+ keys = pygame .key .get_pressed ()
39+ if keys [K_LEFT ]:
40+ self .x -= 4
41+ if keys [K_RIGHT ]:
42+ self .x += 4
43+
44+ self .rect .x = self .x
45+ self .rect .y = self .y
46+
47+ if self .rect .left < 0 :
48+ self .rect .left = 0
49+ self .x = self .rect .x
50+ elif self .rect .right > WIDTH :
51+ self .rect .right = WIDTH
52+ self .x = self .rect .x
53+
54+ if pygame .sprite .spritecollide (self , cookies , False , pygame .sprite .collide_rect_ratio (0.8 )):
55+ pygame .quit ()
56+ sys .exit ()
57+
58+ def draw (self , screen ):
59+ screen .blit (self .image , self .rect )
60+
61+ class Cookie (pygame .sprite .Sprite ):
62+ def __init__ (self , x , y , x_vel , y_vel ):
63+ super ().__init__ (cookies )
64+
65+ self .image = cookie_img
66+ self .rect = self .image .get_rect ()
67+
68+ self .x = x
69+ self .y = y
70+ self .x_vel = x_vel
71+ self .y_vel = y_vel
72+
73+ def update (self ):
74+ self .x += self .x_vel
75+ self .y += self .y_vel
76+
77+ self .rect .x = self .x
78+ self .rect .y = self .y
79+
80+ if self .rect .left < 0 or self .rect .right > WIDTH :
81+ self .x_vel = - self .x_vel
82+ if self .rect .top < 0 or self .rect .bottom > HEIGHT :
83+ self .y_vel = - self .y_vel
84+
85+ class Bullet (pygame .sprite .Sprite ):
86+ def __init__ (self , x , y ):
87+ super ().__init__ (bullets )
88+
89+ self .image = bullet_imgs [0 ]
90+ self .index = 0
91+ self .rect = self .image .get_rect ()
92+
93+ self .x = x
94+ self .y = y
95+
96+ def update (self ):
97+ self .image = bullet_imgs [self .index ]
98+ self .index += 1
99+ if self .index == 4 :
100+ self .index = 0
101+
102+ self .y -= 10
103+
104+ self .rect .centerx = self .x
105+ self .rect .centery = self .y
106+
107+ if self .rect .bottom < 0 :
108+ self .kill ()
109+
110+ bullets = pygame .sprite .Group ()
111+ cookies = pygame .sprite .Group ()
112+ player = Player ()
113+
114+ spawn_chance = 1 / 100
115+
116+ running = True
117+ while running :
118+ clock .tick (FPS )
119+
120+ for event in pygame .event .get ():
121+ if event .type == QUIT :
122+ running = False
123+ if event .type == KEYDOWN :
124+ if event .key == K_SPACE :
125+ Bullet (player .rect .centerx , player .y )
126+
127+ spawn_chance += 0.002 / 100
128+ if spawn_chance > 5 / 100 :
129+ spawn_chance = 5 / 100
130+ if random () < spawn_chance :
131+ Cookie (randint (50 , WIDTH - 50 ), randint (50 , 100 ), randint (- 5 , 5 ), randint (- 5 , 5 ))
132+
133+ bullets .update ()
134+ cookies .update ()
135+ player .update ()
136+ pygame .sprite .groupcollide (bullets , cookies , True , True )
137+
138+ screen .blit (bg_img , (0 , 0 ))
139+
140+ bullets .draw (screen )
141+ cookies .draw (screen )
142+ player .draw (screen )
143+
144+ pygame .display .update ()
145+
146+ pygame .quit ()
0 commit comments