-
Notifications
You must be signed in to change notification settings - Fork 0
/
Timer2.c
171 lines (155 loc) · 4.3 KB
/
Timer2.c
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
160
161
162
163
164
165
166
167
168
169
#include <stdint.h>
#include "../inc/tm4c123gh6pm.h"
#include "dac.h"
#include "sprite.h"
#include "ST7735.h"
#include "print.h"
#include "random.h"
#define TOTAL_ENEMY 10
extern sprite_t cars[];
extern sprite_t player;
extern uint32_t totalScore;
extern const unsigned short bg[];
extern const unsigned short player0img[];
extern const unsigned short gameover[];
extern volatile uint32_t FallingEdges;
void DisableInterrupts(void); // Disable interrupts
void EnableInterrupts(void); // Enable interrupts
void Reset_Handler(void);
uint32_t Random90(void);
uint8_t RandomDir(void);
void gameEngine(void);
int8_t gameCollision(void);
void gameOver(void);
void Timer2_Init(void){
uint32_t volatile delay;
SYSCTL_RCGCTIMER_R |= 0x04; // 0) activate timer2
delay = SYSCTL_RCGCTIMER_R;
TIMER2_CTL_R = 0x00000000; // 1) disable timer2A
TIMER2_CFG_R = 0x00000000; // 2) 32-bit mode
TIMER2_TAMR_R = 0x00000002; // 3) periodic mode
TIMER2_TAILR_R = 1000000; // 4) reload value
TIMER2_TAPR_R = 0; // 5) clock resolution
TIMER2_ICR_R = 0x00000001; // 6) clear timeout flag
TIMER2_IMR_R = 0x00000001; // 7) arm timeout
NVIC_PRI5_R = (NVIC_PRI5_R&0x00FFFFFF)|0x20000000; // 8) priority 4
NVIC_EN0_R = 1<<23; // 9) enable IRQ 23 in
TIMER2_CTL_R = 0x00000001; // 10) enable timer2A
}
void Timer2A_Handler(void) {
TIMER2_ICR_R = 0x00000001; // acknowledge
gameEngine();
if(gameCollision() == -1) {
gameOver();
}
}
void gameEngine(void) {
for(int i = 0; i < TOTAL_ENEMY; i++) {
if(cars[i].direction == RIGHT){
cars[i].x++;
if(cars[i].x + 1 == 110) {
cars[i].direction = LEFT;
}
} else {
cars[i].x--;
if(cars[i].x - 1 <= 0) {
cars[i].direction = RIGHT;
}
}
ST7735_DrawBitmap(cars[i].x, cars[i].y, cars[i].image, cars[i].height, cars[i].width);
}
}
int8_t gameCollision(void) {
for(int i = 0; i < TOTAL_ENEMY; i++) {
if(player.y == cars[i].y) {
if((player.x >= cars[i].x) && (player.x <= (cars[i].x + 20))){
return -1;
}
if(((player.x + 12) >= cars[i].x) && (player.x <= cars[i].x)){
return -1;
}
}
}
return 0;
}
uint32_t RandomSeed = 1;
void gameRestart(void) {
Random_Init(RandomSeed);
FallingEdges = 0;
player.x = 0;
player.y = 160;
player.location = GRASS;
player.image = player0img;
cars[0].x = Random90();
cars[0].direction = RandomDir();
cars[1].x = Random90();
cars[1].direction = RandomDir();
cars[2].x = Random90();
cars[2].direction = RandomDir();
cars[3].x = Random90();
cars[3].direction = RandomDir();
cars[4].x = Random90();
cars[4].direction = RandomDir();
cars[5].x = Random90();
cars[5].direction = RandomDir();
cars[6].x = Random90();
cars[6].direction = RandomDir();
cars[7].x = Random90();
cars[7].direction = RandomDir();
cars[8].x = Random90();
cars[8].direction = RandomDir();
cars[9].x = Random90();
cars[9].direction = RandomDir();
RandomSeed = Random90();
}
void gameOver(void) {
DisableInterrupts();
ST7735_FillScreen(0xFFFF);
ST7735_DrawBitmap(3, 100, gameover, 120, 50);
ST7735_SetCursor(3, 11);
ST7735_OutString("Your Score Is:");
LCD_OutDec(totalScore- 1);
ST7735_SetCursor(3, 12);
ST7735_OutString("Resetting game");
ST7735_SetCursor(3, 14);
ST7735_OutString("Please Wait");
ST7735_SetCursor(3, 15);
ST7735_OutString("To Continue...");
int32_t delay = 50000000;
while(delay > 0) {
delay--;
}
gameRestart();
FallingEdges = 0;
ST7735_DrawBitmap(0, 160, bg, 128, 160);
EnableInterrupts();
totalScore = 0;
}
void gameWon(void) {
DisableInterrupts();
ST7735_FillScreen(0xFFFF);
// Need gamewon image here ==>
// ST7735_DrawBitmap(3, 100, gamewon, 120, 50);
ST7735_SetCursor(0, 11);
ST7735_OutString("WINNING Score:");
LCD_OutDec(totalScore);
ST7735_SetCursor(3, 12);
ST7735_OutString("Resetting game");
ST7735_SetCursor(3, 14);
ST7735_OutString("Please Wait");
ST7735_SetCursor(3, 15);
ST7735_OutString("To Continue...");
int32_t delay = 50000000;
while(delay > 0) {
delay--;
}
gameRestart();
ST7735_DrawBitmap(0, 160, bg, 128, 160);
EnableInterrupts();
}
uint32_t Random90(void){
return ((Random32()>>24)%85)+10; // returns random location
}
uint8_t RandomDir(void){
return ((Random32()>>24)%2); // returns random location
}