-
Notifications
You must be signed in to change notification settings - Fork 0
/
bird.py
90 lines (77 loc) · 3.45 KB
/
bird.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
import pygame
from globals import *
from random import randint
class Bird(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.images_bases = [[pygame.image.load('assets/sprites/yellowbird-downflap.png').convert_alpha(),
pygame.image.load('assets/sprites/yellowbird-midflap.png').convert_alpha(),
pygame.image.load('assets/sprites/yellowbird-upflap.png').convert_alpha()],
[pygame.image.load('assets/sprites/redbird-downflap.png').convert_alpha(),
pygame.image.load('assets/sprites/redbird-midflap.png').convert_alpha(),
pygame.image.load('assets/sprites/redbird-upflap.png').convert_alpha()],
[pygame.image.load('assets/sprites/bluebird-downflap.png').convert_alpha(),
pygame.image.load('assets/sprites/bluebird-midflap.png').convert_alpha(),
pygame.image.load('assets/sprites/bluebird-upflap.png').convert_alpha()]]
if ZBIRDS == 0:
self.images_base = self.images_bases[randint(0,2)]
else:
self.images_base = self.images_birds[ZBIRDS -1]
self.angles = range(20, -80, -5)
self.images = [[pygame.transform.rotate(image, angle) for image in self.images_base] for angle in self.angles]
# 3 x 20 matrix with images for all positions
# rows are for different wing poisitons
# coloumns are for different angles
self.masks = [[pygame.mask.from_surface(column) for column in row] for row in self.images]
# precalculated masks
self.rects = [[column.get_rect() for column in row] for row in self.images]
# precalculated rects
self.wing = 0
self.angle = 0
self.image = self.images[self.wing][self.angle]
self.mask = self.masks[self.wing][self.angle]
self.rect = self.rects[self.wing][self.angle]
self.rect.center = (WIDTH // 2, 255)
self.z = 0 #counter
self.w = 0 #counter #2
self.alive = True
def update(self):
if self.alive:
self.flap()
if self.z > TICKRATE / 4:
self.angle -= 1
self.rect.move_ip(0, -180 / TICKRATE)
self.z -= 1
elif self.z > TICKRATE / 6:
self.rect.move_ip(0, -120 / TICKRATE)
self.z -= 1
elif self.z > 0:
self.rect.move_ip(0, 120 / TICKRATE)
self.z -= 1
else:
self.rect.move_ip(0, 300 / TICKRATE)
self.angle += 1
self.z = 0
if self.angle < 0:
self.angle = 0
elif self.angle > len(self.angles) - 1:
self.angle = len(self.angles) - 1
if self.rect.y > 368: # calculation for floor collision, could be replaced with real colision
self.rect.y = 368
if self.alive:
PLAYSOUNDS[0](2)
PLAYSOUNDS[0](3)
GAMESTATE.set(2)
def bounce(self):
PLAYSOUNDS[0](0)
self.z = TICKRATE / 2;
def flap(self):
self.w += 1
if self.w == TICKRATE // WINGSPEED:
self.wing += 1
self.wing %= 3
self.image = self.images[self.angle][self.wing]
self.mask = self.masks[self.angle][self.wing]
self.w = 0
def die(self):
self.alive = False