-
Notifications
You must be signed in to change notification settings - Fork 0
/
asteroids2_0.py
110 lines (64 loc) · 2.38 KB
/
asteroids2_0.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
#Asteroids 1.0
#Zachary Page
#imports
from superwires import games
import random
#global info
games.init(screen_width = 640, screen_height= 480, fps = 60)
#classes
class Asteroid(games.Sprite):
small = 1
medium = 2
large = 3
images = {small : games.load_image("images2/asteroid3.png"),
medium : games.load_image("images2/asteroid2.png"),
large : games.load_image("images2/asteroid1.png")}
speed = 2
def __init__(self, x , y, size):
super(Asteroid, self).__init__(image = Asteroid.images[size],
x = x,
y = y,
dx = random.choice([1, -1]) * Asteroid.speed * random.random()/size,
dy = random.choice([-1, 1]) * Asteroid.speed * random.random()/size)
self.size = size
def update(self):
if self.left < games.screen.width:
self.right = 0
if self.right < 0:
self.left = games.screen.width
if self.top > games.screen.height:
self.bottom = 0
if self.bottom < 0:
self.top = games.screen.height
class Ship(games.Sprite):
ship_image = games.load_image("images2/ship3.png")
rotation_step = 3
def __init__(self):
super(Ship, self).__init__(image=Ship.ship_image,
x=games.screen.width / 2,
y=games.screen.height / 2)
def update(self):
if games.keyboard.is_pressed(games.K_LEFT) or games.keyboard.is_pressed(games.K_a):
self.angle -= Ship.rotation_step
if games.keyboard.is_pressed(games.K_RIGHT) or games.keyboard.is_pressed(games.K_d):
self.angle += Ship.rotation_step
#main
def main():
#load data
bg_img = games.load_image("images2/Space.jpg")
#Create objects
for i in range(8):
x = random.randrange(games.screen.width)
y = random.randrange(games.screen.height)
size = random.choice([Asteroid.small, Asteroid.medium, Asteroid.large])
new_asteroid = Asteroid(x = x, y = y, size = size)
games.screen.add(new_asteroid)
#create ship
player = Ship()
#draw objects
games.screen.background = bg_img
games.screen.add(player)
#game setup
#start mainloop
games.screen.mainloop()
main()