-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.py
120 lines (95 loc) · 3.22 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
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
# This is the main code for the test sample game.
__author__ = 'Mus'
import pygame, sys
from pygame.locals import *
# bring in my code from other python files
import ship, bullet, background, asteroid
# set up pygame
pygame.mixer.pre_init(22050, -16, 2, 512) # init sound system
pygame.init()
# get the clock, so we can set the framerate later
mainClock = pygame.time.Clock()
# set up the window
WINDOW_WIDTH = 1440
WINDOW_HEIGHT = 800
windowSurface = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT), 0, 32)
pygame.display.set_caption('Test Game')
print('The height is ' + str(WINDOW_HEIGHT) + '.')
print('The width is ' + str(WINDOW_WIDTH) + '.')
# set up colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
#set up fonts
FONT_SIZE = 48
basicFont = pygame.font.SysFont(None, FONT_SIZE)
# set up movement variables
turnCCW = False
turnCW = False
thruster = False
turbo = False
# set up other objects (sprites)
ship.SetupShip(pygame)
bullet.SetupBullet(pygame)
background.SetupBgnd(pygame)
asteroid.SetupAsteroid(pygame)
# run game loop
while (True):
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
# check input
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
if event.key == K_LEFT or event.key == ord('a'):
# start counter clock wise turn
turnCW = False
turnCCW = True
if event.key == K_RIGHT or event.key == ord('d'):
# start clock wise turn
turnCCW = False
turnCW = True
if event.key == K_UP or event.key == ord('w'):
# start ship moving
thruster = True
ship.engineSound.play(-1)
if event.key == K_LSHIFT or event.key == K_RSHIFT:
turbo = True
if event.key == K_RETURN:
bullet.FireBullet(pygame, ship.shipPosX, ship.shipPosY, ship.shipRotation)
bullet.bulletSound.play()
if event.type == KEYUP:
if event.key == K_LEFT or event.key == ord('a'):
# stop counter clock wise turn
turnCCW = False
if event.key == K_RIGHT or event.key == ord('d'):
# stop clock wise turn
turnCW = False
if event.key == K_UP or event.key == ord('w'):
# stop ship moving
thruster = False
ship.engineSound.stop()
#stop ship turbo mode
if event.key == K_LSHIFT or K_RSHIFT:
turbo = False
# asteroid move
asteroid.Amove(windowSurface)
# send motion variables to ship, so it can turn or move
ship.TransformShip(windowSurface, turnCCW, turnCW, thruster, turbo)
# draw background
background.DrawBgnd(windowSurface)
# draw ship
ship.DrawShip(windowSurface, pygame)
# draw bullets
bullet.DrawBullet(windowSurface)
#draw asteroid
asteroid.DrawAsteroid(windowSurface)
# draw the window onto the screen
pygame.display.update()
#set framerate to 60fps
mainClock.tick(60)