-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAsteroids.py
61 lines (56 loc) · 2.25 KB
/
Asteroids.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
#2012 Ross Rydman
from game import Game
from point import Point
from Ship import Ship
from Rock import Rock
from Circle import Circle
from Bullet import Bullet
from Star import Star
import config
import random
import pygame
class Asteroids(Game):
def __init__(self, name, screen_x, screen_y, frames_per_second):
Game.__init__(self, name, screen_x, screen_y)
ship_position = Point(config.SCREEN_X/2, config.SCREEN_Y/2)
self.ship = Ship(ship_position, config.SHIP_INITIAL_DIRECTION, config.SHIP_COLOR)
self.bullet = Bullet(Point(0,0), config.BULLET_RADIUS, 0, config.BULLET_COLOR)
self.stars = []
for i in range(config.STAR_COUNT):
s = Star()
self.stars.append(s)
self.rocks = []
for i in range(config.ROCK_COUNT):
(x,y) = random.randint(0, config.SCREEN_X), random.randint(0, config.SCREEN_Y)
p = Point(x,y)
r = Rock(p, random.uniform(0, 360.0), config.ROCK_COLOR, (random.uniform(0.0, config.ROCK_MAX_ROTATION_SPEED) * random.uniform(-1.0,1.0)), random.randint(0, config.ROCK_MAX_SPEED))
self.rocks.append(r)
def game_logic(self, keys, newkeys):
for s in self.stars:
s.game_logic(keys, newkeys)
self.ship.game_logic(keys, newkeys)
for rock in self.rocks:
rock.game_logic(keys, newkeys)
if rock.isActive() == True:
if self.ship.intersect(rock) == True:
self.ship.set_inactive()
if self.bullet.intersect(rock) == True:
rock.set_inactive()
self.bullet.set_inactive()
if pygame.K_SPACE in newkeys:
if self.ship.isActive() == True:
points = self.ship.getPoints()
self.bullet.fire(points[0], self.ship.rotation)
self.bullet.game_logic(keys,newkeys)
def paint(self, surface):
self.screen.fill(config.BACKGROUND_COLOR)
for s in self.stars:
s.paint(surface)
self.ship.paint(surface)
self.bullet.paint(surface)
for rock in self.rocks:
rock.paint(surface)
def main():
a = Asteroids(config.TITLE, config.SCREEN_X, config.SCREEN_Y, config.FRAMES_PER_SECOND)
a.main_loop()
main()