python space invaders code

Solutions on MaxInterview for python space invaders code by the best coders in the world

showing results for - "python space invaders code"
Agustina
03 Feb 2018
1# python space invaders (made by yasin)
2import pygame
3
4
5class Game:
6    screen = None
7    aliens = []
8    rockets = []
9    lost = False
10
11    def __init__(self, width, height):
12        pygame.init()
13        self.width = width
14        self.height = height
15        self.screen = pygame.display.set_mode((width, height))
16        self.clock = pygame.time.Clock()
17        done = False
18
19        hero = Hero(self, width / 2, height - 20)
20        generator = Generator(self)
21        rocket = None
22
23        while not done:
24            if len(self.aliens) == 0:
25                self.displayText("VICTORY ACHIEVED")
26
27            pressed = pygame.key.get_pressed()
28            if pressed[pygame.K_LEFT]:  # sipka doleva
29                hero.x -= 2 if hero.x > 20 else 0  # leva hranice plochy
30            elif pressed[pygame.K_RIGHT]:  # sipka doprava
31                hero.x += 2 if hero.x < width - 20 else 0  # prava hranice
32
33            for event in pygame.event.get():
34                if event.type == pygame.QUIT:
35                    done = True
36                if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE and not self.lost:
37                    self.rockets.append(Rocket(self, hero.x, hero.y))
38
39            pygame.display.flip()
40            self.clock.tick(60)
41            self.screen.fill((0, 0, 0))
42
43            for alien in self.aliens:
44                alien.draw()
45                alien.checkCollision(self)
46                if (alien.y > height):
47                    self.lost = True
48                    self.displayText("YOU DIED")
49
50            for rocket in self.rockets:
51                rocket.draw()
52
53            if not self.lost: hero.draw()
54
55    def displayText(self, text):
56        pygame.font.init()
57        font = pygame.font.SysFont('Arial', 50)
58        textsurface = font.render(text, False, (44, 0, 62))
59        self.screen.blit(textsurface, (110, 160))
60
61
62class Alien:
63    def __init__(self, game, x, y):
64        self.x = x
65        self.game = game
66        self.y = y
67        self.size = 30
68
69    def draw(self):
70        pygame.draw.rect(self.game.screen,  # renderovací plocha
71                         (81, 43, 88),  # barva objektu
72                         pygame.Rect(self.x, self.y, self.size, self.size))
73        self.y += 0.05
74
75    def checkCollision(self, game):
76        for rocket in game.rockets:
77            if (rocket.x < self.x + self.size and
78                    rocket.x > self.x - self.size and
79                    rocket.y < self.y + self.size and
80                    rocket.y > self.y - self.size):
81                game.rockets.remove(rocket)
82                game.aliens.remove(self)
83
84
85class Hero:
86    def __init__(self, game, x, y):
87        self.x = x
88        self.game = game
89        self.y = y
90
91    def draw(self):
92        pygame.draw.rect(self.game.screen,
93                         (210, 250, 251),
94                         pygame.Rect(self.x, self.y, 8, 5))
95
96
97class Generator:
98    def __init__(self, game):
99        margin = 30  # mezera od okraju obrazovky
100        width = 50  # mezera mezi alieny
101        for x in range(margin, game.width - margin, width):
102            for y in range(margin, int(game.height / 2), width):
103                game.aliens.append(Alien(game, x, y))
104
105        # game.aliens.append(Alien(game, 280, 50))
106
107
108class Rocket:
109    def __init__(self, game, x, y):
110        self.x = x
111        self.y = y
112        self.game = game
113
114    def draw(self):
115        pygame.draw.rect(self.game.screen,  # renderovací plocha
116                         (254, 52, 110),  # barva objektu
117                         pygame.Rect(self.x, self.y, 2, 4))
118        self.y -= 2  # poletí po herní ploše nahoru 2px/snímek
119
120
121if __name__ == '__main__':
122    game = Game(600, 400)