Double Snake Game
Dependencies: mbed SPI_TFT_ILI9341 Joystick font_big
main.cpp@0:fada2b2ed18f, 2019-06-16 (annotated)
- Committer:
- 2018US_EminaTahirovi
- Date:
- Sun Jun 16 20:26:36 2019 +0000
- Revision:
- 0:fada2b2ed18f
- Child:
- 1:891702b74827
.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
2018US_EminaTahirovi | 0:fada2b2ed18f | 1 | # include "mbed.h" |
2018US_EminaTahirovi | 0:fada2b2ed18f | 2 | # define dp23 P0_0 |
2018US_EminaTahirovi | 0:fada2b2ed18f | 3 | # include "stdio.h" |
2018US_EminaTahirovi | 0:fada2b2ed18f | 4 | # include "SPI_TFT_ILI9341.h" |
2018US_EminaTahirovi | 0:fada2b2ed18f | 5 | # include "string" |
2018US_EminaTahirovi | 0:fada2b2ed18f | 6 | # include "Arial12x12.h" |
2018US_EminaTahirovi | 0:fada2b2ed18f | 7 | //# include <Joystick.h> |
2018US_EminaTahirovi | 0:fada2b2ed18f | 8 | //# include "Arial24x23.h" |
2018US_EminaTahirovi | 0:fada2b2ed18f | 9 | //# include "Arial28x28.h" |
2018US_EminaTahirovi | 0:fada2b2ed18f | 10 | //# include "font_big.h" |
2018US_EminaTahirovi | 0:fada2b2ed18f | 11 | #include "stdlib.h" |
2018US_EminaTahirovi | 0:fada2b2ed18f | 12 | #include "time.h" |
2018US_EminaTahirovi | 0:fada2b2ed18f | 13 | |
2018US_EminaTahirovi | 0:fada2b2ed18f | 14 | Timer t; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 15 | |
2018US_EminaTahirovi | 0:fada2b2ed18f | 16 | SPI_TFT_ILI9341 TFT ( dp2 , dp1 , dp6 , dp24 , dp23 , dp25 ," TFT "); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 17 | AnalogIn VRx1(dp11); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 18 | AnalogIn VRy1(dp10); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 19 | InterruptIn SW1(dp9); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 20 | |
2018US_EminaTahirovi | 0:fada2b2ed18f | 21 | AnalogIn VRx2(dp4); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 22 | AnalogIn VRy2(dp13); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 23 | InterruptIn SW2(dp17); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 24 | |
2018US_EminaTahirovi | 0:fada2b2ed18f | 25 | enum Pozicija{Centar, Desno, Lijevo, Gore, Dolje}; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 26 | Pozicija pozicija1 = Centar, pozicija2 = Centar, pozicija = Centar; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 27 | Pozicija redSnakePos(Desno), yellowSnakePos(Lijevo); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 28 | //dimenzije prostora za igru; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 29 | int najmanja_x=0, najmanja_y=0, najveca_x=320, najveca_y=240; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 30 | int diff(1); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 31 | int multiply[3] = {10, 20, 50}; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 32 | int delay[3] = {500,300,150}; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 33 | |
2018US_EminaTahirovi | 0:fada2b2ed18f | 34 | void ReadDifficulty(){ |
2018US_EminaTahirovi | 0:fada2b2ed18f | 35 | Pozicija izbor; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 36 | if(VRx1 < 1.0 / 3.0) izbor=Lijevo; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 37 | else if(VRx1 > 2.0 / 3.0) izbor=Desno; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 38 | |
2018US_EminaTahirovi | 0:fada2b2ed18f | 39 | if(izbor != pozicija) pozicija = izbor; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 40 | } |
2018US_EminaTahirovi | 0:fada2b2ed18f | 41 | |
2018US_EminaTahirovi | 0:fada2b2ed18f | 42 | void LarinIzbor(){ |
2018US_EminaTahirovi | 0:fada2b2ed18f | 43 | ReadDifficulty(); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 44 | if(pozicija == Desno && diff < 2) diff++; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 45 | else if(pozicija == Lijevo && diff > 0) diff--; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 46 | } |
2018US_EminaTahirovi | 0:fada2b2ed18f | 47 | |
2018US_EminaTahirovi | 0:fada2b2ed18f | 48 | void ReadPosition1(){ |
2018US_EminaTahirovi | 0:fada2b2ed18f | 49 | Pozicija nova_pozicija1; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 50 | if(VRx1 < 1.0 / 3.0) nova_pozicija1=Lijevo; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 51 | else if(VRx1 > 2.0 / 3.0) nova_pozicija1=Desno; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 52 | else if(VRy1 < 1.0 / 3.0) nova_pozicija1=Gore; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 53 | else if(VRy1 > 2.0 / 3.0) nova_pozicija1=Dolje; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 54 | |
2018US_EminaTahirovi | 0:fada2b2ed18f | 55 | if(nova_pozicija1 != pozicija1){ |
2018US_EminaTahirovi | 0:fada2b2ed18f | 56 | pozicija1 = nova_pozicija1; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 57 | } |
2018US_EminaTahirovi | 0:fada2b2ed18f | 58 | } |
2018US_EminaTahirovi | 0:fada2b2ed18f | 59 | |
2018US_EminaTahirovi | 0:fada2b2ed18f | 60 | void ReadPosition2(){ |
2018US_EminaTahirovi | 0:fada2b2ed18f | 61 | Pozicija nova_pozicija2; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 62 | if(VRx2 < 1.0 / 3.0) nova_pozicija2=Lijevo; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 63 | else if(VRx2 > 2.0 / 3.0) nova_pozicija2=Desno; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 64 | else if(VRy2 < 1.0 / 3.0) nova_pozicija2=Gore; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 65 | else if(VRy2 > 2.0 / 3.0) nova_pozicija2=Dolje; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 66 | |
2018US_EminaTahirovi | 0:fada2b2ed18f | 67 | if(nova_pozicija2 != pozicija2){ |
2018US_EminaTahirovi | 0:fada2b2ed18f | 68 | pozicija2 = nova_pozicija2; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 69 | } |
2018US_EminaTahirovi | 0:fada2b2ed18f | 70 | } |
2018US_EminaTahirovi | 0:fada2b2ed18f | 71 | |
2018US_EminaTahirovi | 0:fada2b2ed18f | 72 | //koordinate jednog dijela zmije ili njene hrane; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 73 | struct gameItem{ |
2018US_EminaTahirovi | 0:fada2b2ed18f | 74 | int X; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 75 | int Y; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 76 | }; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 77 | |
2018US_EminaTahirovi | 0:fada2b2ed18f | 78 | //default velicina zmije; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 79 | unsigned int yellow_snake_velicina=5; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 80 | unsigned int red_snake_velicina=5; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 81 | bool sw1(true); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 82 | |
2018US_EminaTahirovi | 0:fada2b2ed18f | 83 | //sirina i duzina jednog dijela zimje ili njene hrane; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 84 | const unsigned int dimenzija=5; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 85 | |
2018US_EminaTahirovi | 0:fada2b2ed18f | 86 | //niz za dijelove zmije; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 87 | gameItem red_snake[100]; // random broj 100 |
2018US_EminaTahirovi | 0:fada2b2ed18f | 88 | gameItem yellow_snake[100]; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 89 | |
2018US_EminaTahirovi | 0:fada2b2ed18f | 90 | //hrana |
2018US_EminaTahirovi | 0:fada2b2ed18f | 91 | gameItem njamnjam; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 92 | |
2018US_EminaTahirovi | 0:fada2b2ed18f | 93 | //stanja igre; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 94 | enum GameState{Welcome, Begin, Proces, MainScreen, GameOver}; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 95 | |
2018US_EminaTahirovi | 0:fada2b2ed18f | 96 | GameState stanje=Welcome; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 97 | //funkcija za ispis WelcomeScreena |
2018US_EminaTahirovi | 0:fada2b2ed18f | 98 | void statScreen(){ |
2018US_EminaTahirovi | 0:fada2b2ed18f | 99 | LarinIzbor(); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 100 | TFT.fillrect(10,20,310,220,Yellow); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 101 | TFT.locate(70,70); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 102 | TFT.set_font (( unsigned char *) Arial12x12); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 103 | TFT.printf("Double Snake"); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 104 | TFT.locate(70,90); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 105 | TFT.printf("Difficulty:"); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 106 | TFT.locate(70,130); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 107 | TFT.printf("Press the button"); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 108 | TFT.locate(70,150); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 109 | TFT.printf(" to start."); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 110 | } |
2018US_EminaTahirovi | 0:fada2b2ed18f | 111 | void WelcomeScreen(){ |
2018US_EminaTahirovi | 0:fada2b2ed18f | 112 | // statScreen(); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 113 | TFT.set_font (( unsigned char *) Arial12x12); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 114 | TFT.fillrect(70,110,320,122,Yellow); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 115 | TFT.locate(70,110); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 116 | if(diff == 1) TFT.printf("Medium"); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 117 | else if(diff == 0) TFT.printf("Low"); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 118 | else if(diff == 2) TFT.printf("High"); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 119 | } |
2018US_EminaTahirovi | 0:fada2b2ed18f | 120 | //Funkcije za crtanje |
2018US_EminaTahirovi | 0:fada2b2ed18f | 121 | void crtajCrvenuZmiju(){ |
2018US_EminaTahirovi | 0:fada2b2ed18f | 122 | TFT.fillrect(red_snake[0].X, red_snake[0].Y, red_snake[0].X + dimenzija, red_snake[0].Y + dimenzija, Red); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 123 | TFT.fillrect(red_snake[1].X, red_snake[1].Y, red_snake[1].X + dimenzija, red_snake[1].Y + dimenzija, Green); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 124 | TFT.fillrect(red_snake[red_snake_velicina-1].X, red_snake[red_snake_velicina-1].Y, red_snake[red_snake_velicina-1].X + dimenzija, red_snake[red_snake_velicina-1].Y + dimenzija, Green); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 125 | for(int i=1; i<red_snake_velicina; i++){ |
2018US_EminaTahirovi | 0:fada2b2ed18f | 126 | TFT.rect(red_snake[i].X, red_snake[i].Y, red_snake[i].X + dimenzija, red_snake[i].Y + dimenzija, Red); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 127 | } |
2018US_EminaTahirovi | 0:fada2b2ed18f | 128 | } |
2018US_EminaTahirovi | 0:fada2b2ed18f | 129 | void crtajZutuZmiju(){ |
2018US_EminaTahirovi | 0:fada2b2ed18f | 130 | TFT.fillrect(yellow_snake[0].X, yellow_snake[0].Y, yellow_snake[0].X+dimenzija, yellow_snake[0].Y+dimenzija,Yellow); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 131 | TFT.fillrect(yellow_snake[1].X, yellow_snake[1].Y, yellow_snake[1].X+dimenzija, yellow_snake[1].Y+dimenzija, Green); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 132 | TFT.fillrect(yellow_snake[yellow_snake_velicina-1].X, yellow_snake[yellow_snake_velicina-1].Y, yellow_snake[yellow_snake_velicina-1].X+dimenzija, yellow_snake[yellow_snake_velicina-1].Y+dimenzija, Green); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 133 | for(int i=1; i<yellow_snake_velicina; i++){ |
2018US_EminaTahirovi | 0:fada2b2ed18f | 134 | TFT.rect(yellow_snake[i].X, yellow_snake[i].Y, yellow_snake[i].X+dimenzija, yellow_snake[i].Y+dimenzija, Yellow); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 135 | } |
2018US_EminaTahirovi | 0:fada2b2ed18f | 136 | |
2018US_EminaTahirovi | 0:fada2b2ed18f | 137 | } |
2018US_EminaTahirovi | 0:fada2b2ed18f | 138 | void crtajHranu(){ |
2018US_EminaTahirovi | 0:fada2b2ed18f | 139 | TFT.fillrect(njamnjam.X, njamnjam.Y, njamnjam.X + dimenzija, njamnjam.Y + dimenzija, Blue); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 140 | } |
2018US_EminaTahirovi | 0:fada2b2ed18f | 141 | |
2018US_EminaTahirovi | 0:fada2b2ed18f | 142 | |
2018US_EminaTahirovi | 0:fada2b2ed18f | 143 | |
2018US_EminaTahirovi | 0:fada2b2ed18f | 144 | //funkcija za slucajeve koji dovode do GameOver stanja |
2018US_EminaTahirovi | 0:fada2b2ed18f | 145 | void Sudari(){ |
2018US_EminaTahirovi | 0:fada2b2ed18f | 146 | //sudar zmije same sa sobom |
2018US_EminaTahirovi | 0:fada2b2ed18f | 147 | for(int i=1; i<red_snake_velicina; i++){ |
2018US_EminaTahirovi | 0:fada2b2ed18f | 148 | if(red_snake[0].X == red_snake[i].X && red_snake[0].Y == red_snake[i].Y){ |
2018US_EminaTahirovi | 0:fada2b2ed18f | 149 | stanje=GameOver; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 150 | |
2018US_EminaTahirovi | 0:fada2b2ed18f | 151 | } |
2018US_EminaTahirovi | 0:fada2b2ed18f | 152 | } |
2018US_EminaTahirovi | 0:fada2b2ed18f | 153 | for(int i=1; i<yellow_snake_velicina; i++){ |
2018US_EminaTahirovi | 0:fada2b2ed18f | 154 | if(yellow_snake[0].X == yellow_snake[i].X && yellow_snake[0].Y == yellow_snake[i].Y){ |
2018US_EminaTahirovi | 0:fada2b2ed18f | 155 | stanje=GameOver; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 156 | |
2018US_EminaTahirovi | 0:fada2b2ed18f | 157 | } |
2018US_EminaTahirovi | 0:fada2b2ed18f | 158 | |
2018US_EminaTahirovi | 0:fada2b2ed18f | 159 | } |
2018US_EminaTahirovi | 0:fada2b2ed18f | 160 | //sudar zmije sa zidom |
2018US_EminaTahirovi | 0:fada2b2ed18f | 161 | if((red_snake[0].X < najmanja_x || red_snake[0].X > najveca_x || red_snake[0].Y < najmanja_y || red_snake[0].Y > najveca_y )) { |
2018US_EminaTahirovi | 0:fada2b2ed18f | 162 | // prodavnicaGame(); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 163 | stanje=GameOver; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 164 | } |
2018US_EminaTahirovi | 0:fada2b2ed18f | 165 | if((yellow_snake[0].X<najmanja_x || yellow_snake[0].X>najveca_x || yellow_snake[0].Y<najmanja_y || yellow_snake[0].Y>najveca_y )) { |
2018US_EminaTahirovi | 0:fada2b2ed18f | 166 | // prodavnicaGame(); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 167 | stanje=GameOver; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 168 | } |
2018US_EminaTahirovi | 0:fada2b2ed18f | 169 | // sudar dvije zmije |
2018US_EminaTahirovi | 0:fada2b2ed18f | 170 | for(int i=0; i<yellow_snake_velicina; i++){ |
2018US_EminaTahirovi | 0:fada2b2ed18f | 171 | if(red_snake[0].X == yellow_snake[i].X && red_snake[0].Y == yellow_snake[i].Y) |
2018US_EminaTahirovi | 0:fada2b2ed18f | 172 | stanje=GameOver; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 173 | } |
2018US_EminaTahirovi | 0:fada2b2ed18f | 174 | for(int i=0; i<red_snake_velicina; i++){ |
2018US_EminaTahirovi | 0:fada2b2ed18f | 175 | if(yellow_snake[0].X == red_snake[i].X && yellow_snake[0].Y == red_snake[i].Y) |
2018US_EminaTahirovi | 0:fada2b2ed18f | 176 | stanje=GameOver; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 177 | } |
2018US_EminaTahirovi | 0:fada2b2ed18f | 178 | |
2018US_EminaTahirovi | 0:fada2b2ed18f | 179 | } |
2018US_EminaTahirovi | 0:fada2b2ed18f | 180 | |
2018US_EminaTahirovi | 0:fada2b2ed18f | 181 | |
2018US_EminaTahirovi | 0:fada2b2ed18f | 182 | void spawnFood(){ |
2018US_EminaTahirovi | 0:fada2b2ed18f | 183 | njamnjam.X += 50; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 184 | njamnjam.Y += 50; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 185 | if(njamnjam.X > najveca_x) njamnjam.X = 10; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 186 | if(njamnjam.Y > najveca_y) njamnjam.Y = 10; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 187 | } |
2018US_EminaTahirovi | 0:fada2b2ed18f | 188 | |
2018US_EminaTahirovi | 0:fada2b2ed18f | 189 | |
2018US_EminaTahirovi | 0:fada2b2ed18f | 190 | void spawnSnake(){ |
2018US_EminaTahirovi | 0:fada2b2ed18f | 191 | red_snake[0].X = najmanja_x + 4*dimenzija; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 192 | red_snake[0].Y = najmanja_y; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 193 | yellow_snake[0].X = najveca_x - 30; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 194 | yellow_snake[0].Y = najveca_y-10; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 195 | |
2018US_EminaTahirovi | 0:fada2b2ed18f | 196 | int defaultSize = 5; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 197 | for(int i=1; i<defaultSize; i++){ |
2018US_EminaTahirovi | 0:fada2b2ed18f | 198 | red_snake[i].X = red_snake[i-1].X - dimenzija; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 199 | red_snake[i].Y = red_snake[i-1].Y; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 200 | } |
2018US_EminaTahirovi | 0:fada2b2ed18f | 201 | for(int i=1; i<defaultSize; i++){ |
2018US_EminaTahirovi | 0:fada2b2ed18f | 202 | yellow_snake[i].X = yellow_snake[i-1].X + dimenzija; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 203 | yellow_snake[i].Y = yellow_snake[i-1].Y; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 204 | } |
2018US_EminaTahirovi | 0:fada2b2ed18f | 205 | red_snake_velicina = defaultSize; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 206 | yellow_snake_velicina = defaultSize; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 207 | |
2018US_EminaTahirovi | 0:fada2b2ed18f | 208 | } |
2018US_EminaTahirovi | 0:fada2b2ed18f | 209 | void Setup(){ |
2018US_EminaTahirovi | 0:fada2b2ed18f | 210 | redSnakePos=Desno; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 211 | yellowSnakePos=Lijevo; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 212 | spawnSnake(); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 213 | spawnFood(); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 214 | stanje=Proces; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 215 | } |
2018US_EminaTahirovi | 0:fada2b2ed18f | 216 | void prodavnicaGame(){ |
2018US_EminaTahirovi | 0:fada2b2ed18f | 217 | TFT.cls(); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 218 | TFT.locate(100,100); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 219 | TFT.set_font (( unsigned char *) Arial12x12); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 220 | TFT.printf("GAME OVER!"); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 221 | TFT.locate(100,150); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 222 | TFT.printf("Score player 1: %d", (red_snake_velicina-5)*10); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 223 | TFT.locate(100,170); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 224 | TFT.printf("Score player 2: %d", (yellow_snake_velicina-5)*10); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 225 | TFT.locate(120,190); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 226 | if(red_snake_velicina>yellow_snake_velicina) TFT.printf("Pobjednik je crveni igrac"); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 227 | else if(red_snake_velicina<yellow_snake_velicina) TFT.printf("Pobjednik je zuti igrac"); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 228 | else TFT.printf("Nerijeseno"); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 229 | } |
2018US_EminaTahirovi | 0:fada2b2ed18f | 230 | //trenutak kad zmija pojede hranu |
2018US_EminaTahirovi | 0:fada2b2ed18f | 231 | void JediBraco(){ |
2018US_EminaTahirovi | 0:fada2b2ed18f | 232 | if(red_snake[0].X == njamnjam.X && red_snake[0].Y == njamnjam.Y){ |
2018US_EminaTahirovi | 0:fada2b2ed18f | 233 | red_snake_velicina++; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 234 | TFT.fillrect(njamnjam.X,njamnjam.Y, njamnjam.X + dimenzija, njamnjam.Y + dimenzija, Green); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 235 | spawnFood(); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 236 | } |
2018US_EminaTahirovi | 0:fada2b2ed18f | 237 | if(yellow_snake[0].X == njamnjam.X && yellow_snake[0].Y == njamnjam.Y){ |
2018US_EminaTahirovi | 0:fada2b2ed18f | 238 | yellow_snake_velicina++; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 239 | TFT.fillrect(njamnjam.X,njamnjam.Y, njamnjam.X + dimenzija, njamnjam.Y + dimenzija, Green); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 240 | spawnFood(); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 241 | } |
2018US_EminaTahirovi | 0:fada2b2ed18f | 242 | } |
2018US_EminaTahirovi | 0:fada2b2ed18f | 243 | void crtaj(){ |
2018US_EminaTahirovi | 0:fada2b2ed18f | 244 | crtajCrvenuZmiju(); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 245 | crtajZutuZmiju(); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 246 | crtajHranu(); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 247 | Sudari(); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 248 | } |
2018US_EminaTahirovi | 0:fada2b2ed18f | 249 | void redSnakeMovement(){ |
2018US_EminaTahirovi | 0:fada2b2ed18f | 250 | ReadPosition1(); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 251 | if(pozicija1 == Desno && redSnakePos != Lijevo) { |
2018US_EminaTahirovi | 0:fada2b2ed18f | 252 | redSnakePos=Desno; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 253 | } |
2018US_EminaTahirovi | 0:fada2b2ed18f | 254 | else if(pozicija1 == Lijevo && redSnakePos != Desno){ |
2018US_EminaTahirovi | 0:fada2b2ed18f | 255 | redSnakePos = Lijevo; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 256 | } |
2018US_EminaTahirovi | 0:fada2b2ed18f | 257 | else if(pozicija1 == Gore && redSnakePos != Dolje){ |
2018US_EminaTahirovi | 0:fada2b2ed18f | 258 | redSnakePos = Gore; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 259 | } |
2018US_EminaTahirovi | 0:fada2b2ed18f | 260 | else if(pozicija1 == Dolje && redSnakePos != Gore){ |
2018US_EminaTahirovi | 0:fada2b2ed18f | 261 | redSnakePos = Dolje; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 262 | } |
2018US_EminaTahirovi | 0:fada2b2ed18f | 263 | } |
2018US_EminaTahirovi | 0:fada2b2ed18f | 264 | |
2018US_EminaTahirovi | 0:fada2b2ed18f | 265 | void yellowSnakeMovement(){ |
2018US_EminaTahirovi | 0:fada2b2ed18f | 266 | ReadPosition2(); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 267 | if(pozicija2 == Desno && yellowSnakePos != Lijevo) { |
2018US_EminaTahirovi | 0:fada2b2ed18f | 268 | yellowSnakePos=Desno; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 269 | } |
2018US_EminaTahirovi | 0:fada2b2ed18f | 270 | else if(pozicija2 == Lijevo && yellowSnakePos != Desno){ |
2018US_EminaTahirovi | 0:fada2b2ed18f | 271 | yellowSnakePos = Lijevo; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 272 | } |
2018US_EminaTahirovi | 0:fada2b2ed18f | 273 | else if(pozicija2 == Gore && yellowSnakePos != Dolje){ |
2018US_EminaTahirovi | 0:fada2b2ed18f | 274 | yellowSnakePos = Gore; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 275 | } |
2018US_EminaTahirovi | 0:fada2b2ed18f | 276 | else if(pozicija2 == Dolje && yellowSnakePos != Gore){ |
2018US_EminaTahirovi | 0:fada2b2ed18f | 277 | yellowSnakePos = Dolje; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 278 | } |
2018US_EminaTahirovi | 0:fada2b2ed18f | 279 | } |
2018US_EminaTahirovi | 0:fada2b2ed18f | 280 | |
2018US_EminaTahirovi | 0:fada2b2ed18f | 281 | void snakeMovement(){ |
2018US_EminaTahirovi | 0:fada2b2ed18f | 282 | redSnakeMovement(); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 283 | yellowSnakeMovement(); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 284 | // Sudari(); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 285 | stanje=MainScreen; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 286 | } |
2018US_EminaTahirovi | 0:fada2b2ed18f | 287 | |
2018US_EminaTahirovi | 0:fada2b2ed18f | 288 | void updateRedMovement(){ |
2018US_EminaTahirovi | 0:fada2b2ed18f | 289 | TFT.rect(red_snake[red_snake_velicina-1].X, red_snake[red_snake_velicina-1].Y, red_snake[red_snake_velicina-1].X+dimenzija, red_snake[red_snake_velicina-1].Y+dimenzija,Green); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 290 | for(int i=red_snake_velicina - 1; i > 0; i--){ |
2018US_EminaTahirovi | 0:fada2b2ed18f | 291 | red_snake[i].X = red_snake[i-1].X; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 292 | red_snake[i].Y = red_snake[i-1].Y; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 293 | } |
2018US_EminaTahirovi | 0:fada2b2ed18f | 294 | if(redSnakePos == Lijevo) red_snake[0].X -= dimenzija; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 295 | else if(redSnakePos == Desno) red_snake[0].X += dimenzija; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 296 | else if(redSnakePos == Dolje) red_snake[0].Y += dimenzija; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 297 | else if(redSnakePos == Gore) red_snake[0].Y -= dimenzija; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 298 | // TFT.fillrect(red_snake[red_snake_velicina-1].X, red_snake[red_snake_velicina-1].Y, red_snake[red_snake_velicina-1].X+dimenzija, red_snake[red_snake_velicina-1].Y+dimenzija,Green); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 299 | } |
2018US_EminaTahirovi | 0:fada2b2ed18f | 300 | |
2018US_EminaTahirovi | 0:fada2b2ed18f | 301 | void updateYellowMovement(){ |
2018US_EminaTahirovi | 0:fada2b2ed18f | 302 | TFT.rect(yellow_snake[yellow_snake_velicina-1].X, yellow_snake[yellow_snake_velicina-1].Y, yellow_snake[yellow_snake_velicina-1].X+dimenzija, yellow_snake[yellow_snake_velicina-1].Y+dimenzija,Green); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 303 | for(int i=yellow_snake_velicina - 1; i > 0; i--){ |
2018US_EminaTahirovi | 0:fada2b2ed18f | 304 | yellow_snake[i].X = yellow_snake[i-1].X; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 305 | yellow_snake[i].Y = yellow_snake[i-1].Y; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 306 | } |
2018US_EminaTahirovi | 0:fada2b2ed18f | 307 | if(yellowSnakePos == Lijevo) yellow_snake[0].X -= dimenzija; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 308 | else if(yellowSnakePos == Desno) yellow_snake[0].X += dimenzija; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 309 | else if(yellowSnakePos == Dolje) yellow_snake[0].Y += dimenzija; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 310 | else if(yellowSnakePos == Gore) yellow_snake[0].Y -= dimenzija; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 311 | // TFT.fillrect(yellow_snake[yellow_snake_velicina-1].X, yellow_snake[yellow_snake_velicina-1].Y, yellow_snake[yellow_snake_velicina-1].X+dimenzija, yellow_snake[yellow_snake_velicina-1].Y+dimenzija,Green); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 312 | } |
2018US_EminaTahirovi | 0:fada2b2ed18f | 313 | |
2018US_EminaTahirovi | 0:fada2b2ed18f | 314 | void updateMovement() { |
2018US_EminaTahirovi | 0:fada2b2ed18f | 315 | updateRedMovement(); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 316 | updateYellowMovement(); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 317 | JediBraco(); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 318 | //Sudari(); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 319 | stanje=Proces; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 320 | } |
2018US_EminaTahirovi | 0:fada2b2ed18f | 321 | void Igraj(){ |
2018US_EminaTahirovi | 0:fada2b2ed18f | 322 | stanje=Begin; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 323 | } |
2018US_EminaTahirovi | 0:fada2b2ed18f | 324 | |
2018US_EminaTahirovi | 0:fada2b2ed18f | 325 | int main() { |
2018US_EminaTahirovi | 0:fada2b2ed18f | 326 | SW1.mode(PullUp); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 327 | SW2.mode(PullUp); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 328 | TFT.set_orientation(3); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 329 | TFT.background(Green); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 330 | TFT.foreground(Blue ); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 331 | TFT.cls(); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 332 | SW1.fall(&Igraj); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 333 | stanje=Welcome; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 334 | sw1=true; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 335 | t.start(); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 336 | njamnjam.X=10; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 337 | njamnjam.Y=30; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 338 | while(1) { |
2018US_EminaTahirovi | 0:fada2b2ed18f | 339 | switch(stanje){ |
2018US_EminaTahirovi | 0:fada2b2ed18f | 340 | case Welcome: |
2018US_EminaTahirovi | 0:fada2b2ed18f | 341 | LarinIzbor(); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 342 | if(t.read_ms()%3000==0) {statScreen(); WelcomeScreen(); } |
2018US_EminaTahirovi | 0:fada2b2ed18f | 343 | break; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 344 | case Begin: |
2018US_EminaTahirovi | 0:fada2b2ed18f | 345 | TFT.cls(); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 346 | Setup(); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 347 | break; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 348 | case Proces: |
2018US_EminaTahirovi | 0:fada2b2ed18f | 349 | snakeMovement(); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 350 | wait_ms(5); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 351 | break; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 352 | case MainScreen: |
2018US_EminaTahirovi | 0:fada2b2ed18f | 353 | updateMovement(); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 354 | crtaj(); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 355 | wait_ms(delay[diff]); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 356 | break; |
2018US_EminaTahirovi | 0:fada2b2ed18f | 357 | case GameOver: |
2018US_EminaTahirovi | 0:fada2b2ed18f | 358 | prodavnicaGame(); |
2018US_EminaTahirovi | 0:fada2b2ed18f | 359 | } |
2018US_EminaTahirovi | 0:fada2b2ed18f | 360 | } |
2018US_EminaTahirovi | 0:fada2b2ed18f | 361 | } |