Skip to content

Commit f3dc016

Browse files
committed
step 4
1 parent 487272c commit f3dc016

File tree

5 files changed

+189
-4
lines changed

5 files changed

+189
-4
lines changed

main.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,44 @@
1212
pygame.display.set_caption("SHMUP!")
1313
clock = pygame.time.Clock()
1414

15-
player_img = pygame.image.load("player.png").convert_alpha()
16-
player_img = pygame.transform.scale(player_img, (36, 56))
15+
player_spritesheet = pygame.image.load("player.png").convert_alpha()
16+
player_imgs = []
17+
for x in range(4):
18+
player_img = player_spritesheet.subsurface(x * 9, 0, 9, 14)
19+
player_img = pygame.transform.scale(player_img, (36, 56))
20+
player_imgs.append(player_img)
21+
1722
cookie_img = pygame.image.load("cookie.png").convert_alpha()
23+
1824
bullet_spritesheet = pygame.image.load("bullet.png").convert_alpha()
1925
bullet_imgs = []
2026
for x in range(4):
2127
bullet_img = bullet_spritesheet.subsurface(x * 3, 0, 3, 7)
2228
bullet_img = pygame.transform.scale(bullet_img, (12, 28))
2329
bullet_imgs.append(bullet_img)
30+
2431
bg_img = pygame.image.load("background.png").convert()
2532

2633
class Player(pygame.sprite.Sprite):
2734
def __init__(self):
2835
super().__init__()
2936

30-
self.image = player_img
37+
self.image = player_imgs[0]
38+
self.index = 0
39+
self.animation_time = time.time()
3140
self.rect = player_img.get_rect()
3241

3342
self.x = 180
3443
self.y = 340
3544

3645
def update(self):
46+
self.image = player_imgs[self.index]
47+
if time.time() - self.animation_time > 0.1:
48+
self.animation_time = time.time()
49+
self.index += 1
50+
if self.index == 4:
51+
self.index = 0
52+
3753
keys = pygame.key.get_pressed()
3854
if keys[K_LEFT]:
3955
self.x -= 4

part_3_steps/step-2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Code from part 2"""
1+
"""Bullet animation... but it's too fast?"""
22

33
import sys
44
import pygame

part_3_steps/step-3.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""Proper bullet animation"""
2+
13
import sys
24
import time
35
import pygame

part_3_steps/step-4.py

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

player.png

141 Bytes
Loading

0 commit comments

Comments
 (0)