-
Notifications
You must be signed in to change notification settings - Fork 12
/
pipe.py
104 lines (87 loc) · 3.39 KB
/
pipe.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pygame
import random
head = pygame.image.load('data/images/head_pipe.png')
body = pygame.image.load('data/images/pipe.png')
class Pipe_I(pygame.sprite.Sprite):
def __init__(self, parent, x, height, factor):
pygame.sprite.Sprite.__init__(self)
self.flag = True
self.points = True
self.parent = parent
self.mPos = [x, 500]
self.factor = factor
self.mVel = -5 * self.factor
h = height - 42
self.p = h // 40
self.height = self.p * 40 + 50
self.mPos[1] = self.parent.floor_y - self.height
self.image = pygame.surface.Surface((91, self.height), 0)
self.mask = pygame.mask.from_surface(self.image)
self.image.fill((255, 255, 255))
self.image.blit(head, (0, 0))
for i in range(self.p):
self.image.blit(body, (4, 42 + i * 40))
self.image.set_colorkey((255, 255, 255))
self.rect = self.image.get_rect()
self.rect.x = self.mPos[0]
self.rect.y = self.mPos[1]
self.mask = pygame.mask.from_surface(self.image)
def update(self):
self.mPos[0] = self.mPos[0] + self.mVel
self.rect.x = self.mPos[0]
self.rect.y = self.mPos[1]
if self.mPos[0] < -91:
self.parent.sprites.remove(self)
self.parent.tubes.remove(self)
elif self.mPos[0] < (self.parent.bird_x - self.parent.pipe_w / 2):
if self.points:
self.points = False
self.parent.increment_score()
elif self.mPos[0] < (self.parent.game_p):
if self.flag:
self.flag = False
mi = self.height - 160
ma = self.height + 160
if mi < 82:
mi = 82
if ma > self.parent.max_s:
ma = self.parent.max_s
h = random.randrange(mi, ma)
p = Pipe_I(self.parent, self.parent.game_w, h, self.factor)
self.parent.sprites.add(p, layer=1)
self.parent.tubes.add(p)
s = self.parent.floor_y - p.height - 160
p = Pipe_S(self.parent, self.parent.game_w, s, self.factor)
self.parent.sprites.add(p, layer=1)
self.parent.tubes.add(p)
class Pipe_S(pygame.sprite.Sprite):
def __init__(self, parent, x, height, factor):
pygame.sprite.Sprite.__init__(self)
self.flag = True
self.parent = parent
self.mPos = [x, 0]
self.mVel = -5 * factor
h = height - 42
self.p = h // 40
self.height = self.p * 40 + 42
if self.height > height:
dx = self.height - height
self.mPos[1] = -dx
self.image = pygame.surface.Surface((91, self.height), 0)
self.image.fill((255, 255, 255))
for i in range(self.p):
self.image.blit(body, (4, i * 40))
self.image.blit(head, (0, self.p * 40))
self.image.set_colorkey((255, 255, 255))
self.rect = self.image.get_rect()
self.rect.x = self.mPos[0]
self.rect.y = self.mPos[1]
self.mask = pygame.mask.from_surface(self.image)
def update(self):
self.mPos[0] = self.mPos[0] + self.mVel
self.rect.x = self.mPos[0]
if self.mPos[0] < -91:
self.parent.sprites.remove(self)
self.parent.tubes.remove(self)