forked from vinothpandian/Asteroids-Pygame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
step_5.py
124 lines (102 loc) · 3.38 KB
/
step_5.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import pygame, sys, os, random, math
from pygame.locals import *
pygame.init()
fps = pygame.time.Clock()
#colors
WHITE = (255,255,255)
RED = (255,0,0)
GREEN = (0,255,0)
BLACK = (0,0,0)
#globals
WIDTH = 800
HEIGHT = 600
time = 0
#canvas declaration
window = pygame.display.set_mode((WIDTH, HEIGHT), 0, 32)
pygame.display.set_caption('Asteroids')
#load images
bg = pygame.image.load(os.path.join('images','bg.jpg'))
debris = pygame.image.load(os.path.join('images','debris2_brown.png'))
ship = pygame.image.load(os.path.join('images','ship.png'))
ship_thrusted = pygame.image.load(os.path.join('images','ship_thrusted.png'))
asteroid = pygame.image.load(os.path.join('images','asteroid.png'))
ship_x = WIDTH/2 - 50
ship_y = HEIGHT/2 - 50
ship_angle = 0
ship_is_rotating = False
ship_is_forward = False
ship_direction = 0
ship_speed = 0
asteroid_x = [] #random.randint(0,WIDTH)
asteroid_y = [] #random.randint(0,HEIGHT)
no_asteroids = 5
for i in range(0,no_asteroids):
asteroid_x.append( random.randint(0,WIDTH) )
asteroid_y.append( random.randint(0,HEIGHT) )
def rot_center(image, angle):
"""rotate a Surface, maintaining position."""
orig_rect = image.get_rect()
rot_image = pygame.transform.rotate(image, angle)
rot_rect = orig_rect.copy()
rot_rect.center = rot_image.get_rect().center
rot_image = rot_image.subsurface(rot_rect).copy()
return rot_image
# draw game function
def draw(canvas):
global time
global ship_is_forward
canvas.fill(BLACK)
canvas.blit(bg,(0,0))
canvas.blit(debris,(time*.3,0))
canvas.blit(debris,(time*.3-WIDTH,0))
time = time + 1
for i in range(0,no_asteroids):
canvas.blit( rot_center(asteroid,time) ,(asteroid_x[i],asteroid_y[i]))
if ship_is_forward:
canvas.blit( rot_center(ship_thrusted,ship_angle) , (ship_x, ship_y))
else:
canvas.blit( rot_center(ship,ship_angle) , (ship_x, ship_y))
# handle input function
def handle_input():
global ship_angle, ship_is_rotating, ship_direction
global ship_x, ship_y, ship_speed, ship_is_forward
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
if event.key == K_RIGHT:
ship_is_rotating = True
ship_direction = 0
elif event.key == K_LEFT:
ship_is_rotating = True
ship_direction = 1
elif event.key == K_UP:
ship_is_forward = True
ship_speed = 10
elif event.type == KEYUP:
if event.key == K_LEFT or event.key == K_RIGHT:
ship_is_rotating = False
else:
ship_is_forward = False
print(ship_angle)
if ship_is_rotating:
if ship_direction == 0:
ship_angle = ship_angle - 10
else:
ship_angle = ship_angle + 10
if ship_is_forward or ship_speed > 0:
ship_x = (ship_x + math.cos(math.radians(ship_angle))*ship_speed )
ship_y = (ship_y + -math.sin(math.radians(ship_angle))*ship_speed )
if ship_is_forward == False:
ship_speed = ship_speed - 0.2
# update the screen
def update_screen():
pygame.display.update()
fps.tick(60)
# asteroids game loop
while True:
draw(window)
handle_input()
# game_logic()
update_screen()