This repository has been archived by the owner on Mar 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspacewar.cpp
159 lines (137 loc) · 5.81 KB
/
spacewar.cpp
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Programming 2D Games
// Copyright (c) 2011 by:
// Charles Kelly
// Move spaceship with arrow keys.
// Chapter 5 spacewar.cpp v1.0
// This class is the core of the game
#include "spaceWar.h"
//=============================================================================
// Constructor
//=============================================================================
Spacewar::Spacewar()
{}
//=============================================================================
// Destructor
//=============================================================================
Spacewar::~Spacewar()
{
releaseAll(); // call onLostDevice() for every graphics item
}
//=============================================================================
// Initializes the game
// Throws GameError on error
//=============================================================================
void Spacewar::initialize(HWND hwnd)
{
Game::initialize(hwnd); // throws GameError
// nebula texture
if (!nebulaTexture.initialize(graphics,NEBULA_IMAGE))
throw(GameError(gameErrorNS::FATAL_ERROR, "Error initializing nebula texture"));
// planet texture
if (!planetTexture.initialize(graphics,PLANET_IMAGE))
throw(GameError(gameErrorNS::FATAL_ERROR, "Error initializing planet texture"));
// spaceship texture
if (!shipTexture.initialize(graphics,SHIP_IMAGE))
throw(GameError(gameErrorNS::FATAL_ERROR, "Error initializing ship texture"));
// mino texture
if (!minoTexture.initialize(graphics, MINO_IMAGE))
throw(GameError(gameErrorNS::FATAL_ERROR, "Cannot load mino texture"));
// nebula
if (!nebula.initialize(graphics,0,0,0,&nebulaTexture))
throw(GameError(gameErrorNS::FATAL_ERROR, "Error initializing nebula"));
// planet
if (!planet.initialize(graphics,0,0,0,&planetTexture))
throw(GameError(gameErrorNS::FATAL_ERROR, "Error initializing planet"));
// place planet in center of screen
planet.setX(GAME_WIDTH*0.5f - planet.getWidth()*0.5f);
planet.setY(GAME_HEIGHT*0.5f - planet.getHeight()*0.5f);
// ship
if (!ship.initialize(graphics,SHIP_WIDTH, SHIP_HEIGHT, SHIP_COLS, &shipTexture))
throw(GameError(gameErrorNS::FATAL_ERROR, "Error initializing ship"));
ship.setX(GAME_WIDTH/4); // start above and left of planet
ship.setY(GAME_HEIGHT/4);
ship.setFrames(SHIP_START_FRAME, SHIP_END_FRAME); // animation frames
ship.setCurrentFrame(SHIP_START_FRAME); // starting frame
ship.setFrameDelay(SHIP_ANIMATION_DELAY);
minoFact.makeMino(graphics, &minoTexture);
return;
}
//=============================================================================
// Update all game items
//=============================================================================
void Spacewar::update()
{
if(input->isKeyDown(SHIP_RIGHT_KEY)) // if move right
{
ship.setX(ship.getX() + frameTime * SHIP_SPEED);
if (ship.getX() > GAME_WIDTH) // if off screen right
ship.setX((float)-ship.getWidth()); // position off screen left
}
if(input->isKeyDown(SHIP_LEFT_KEY)) // if move left
{
ship.setX(ship.getX() - frameTime * SHIP_SPEED);
if (ship.getX() < -ship.getWidth()) // if off screen left
ship.setX((float)GAME_WIDTH); // position off screen right
}
if(input->isKeyDown(SHIP_UP_KEY)) // if move up
{
ship.setY(ship.getY() - frameTime * SHIP_SPEED);
if (ship.getY() < -ship.getHeight()) // if off screen top
ship.setY((float)GAME_HEIGHT); // position off screen bottom
}
if(input->isKeyDown(SHIP_DOWN_KEY)) // if move down
{
ship.setY(ship.getY() + frameTime * SHIP_SPEED);
if (ship.getY() > GAME_HEIGHT) // if off screen bottom
ship.setY((float)-ship.getHeight()); // position off screen top
}
ship.update(frameTime);
}
//=============================================================================
// Artificial Intelligence
//=============================================================================
void Spacewar::ai()
{}
//=============================================================================
// Handle collisions
//=============================================================================
void Spacewar::collisions()
{}
//=============================================================================
// Render game items
//=============================================================================
void Spacewar::render()
{
graphics->spriteBegin(); // begin drawing sprites
nebula.draw(); // add the orion nebula to the scene
planet.draw(); // add the planet to the scene
ship.draw(); // add the spaceship to the scene
minoFact.draw();
graphics->spriteEnd(); // end drawing sprites
}
//=============================================================================
// The graphics device was lost.
// Release all reserved video memory so graphics device may be reset.
//=============================================================================
void Spacewar::releaseAll()
{
shipTexture.onLostDevice();
planetTexture.onLostDevice();
nebulaTexture.onLostDevice();
minoTexture.onLostDevice();
Game::releaseAll();
return;
}
//=============================================================================
// The grahics device has been reset.
// Recreate all surfaces.
//=============================================================================
void Spacewar::resetAll()
{
nebulaTexture.onResetDevice();
planetTexture.onResetDevice();
shipTexture.onResetDevice();
minoTexture.onResetDevice();
Game::resetAll();
return;
}