-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
83 lines (69 loc) · 2.07 KB
/
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
import pygame
import random
pygame.init()
screen = pygame.display.set_mode((800, 600))
done = False
is_blue = True
clock = pygame.time.Clock()
IMAGE = pygame.image.load('bird.png')
bird = pygame.transform.scale(IMAGE, (50, 50))
class Sprite:
def __init__(self):
self.x = 50
self.y = 300
self.width = 30
self.height = 30
self.color = (0, 128, 255)
def draw(self, x, y):
screen.blit(bird,(x,y))
def falldown(self):
if (self.y < 570):
self.y += 3
def goUp(self):
self.y -= 13
class Obstacle:
def __init__(self):
self.x = 800
self.topy = 0
self.height = random.randrange(0, 400)
self.width = 30
self.bottomy = self.height + 200
self.color = (0, 255, 0)
def draw(self):
pygame.draw.rect(screen, self.color, pygame.Rect(self.x, self.topy, self.width, self.height))
pygame.draw.rect(screen, self.color, pygame.Rect(self.x, self.bottomy, self.width, 600 - self.bottomy))
def move(self):
self.x -= 4
def collide(self,sprite):
return ((sprite.x + 2*sprite.width > self.x) and ((sprite.y <= self.topy + self.height) or (sprite.y > self.bottomy)))
def reset(self):
self.x = 800
self.height = random.randrange(100, 300)
self.bottomy = self.height + 200
sprite = Sprite()
obstacle = Obstacle()
obstacle1 = Obstacle()
obstacle1.x = 1200
while not done and not (obstacle.collide(sprite) or obstacle1.collide(sprite)):
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
pressed = pygame.key.get_pressed()
if pressed[pygame.K_SPACE]:
if (sprite.y > 0):
# y -= 20
sprite.goUp()
sprite.falldown()
screen.fill((0, 0, 0))
if(obstacle1.x < 0):
obstacle1.reset()
if (obstacle.x < 0):
obstacle.reset()
sprite.draw(sprite.x, sprite.y)
obstacle1.move()
obstacle1.draw()
obstacle.move()
obstacle.draw()
pygame.display.flip()
clock.tick(60)
pygame.quit()