1+ /*
2+ First time? Check out the tutorial game:
3+ https://sprig.hackclub.com/gallery/getting_started
4+
5+ @title : Sebastian's Game of Death
6+ @author :
7+ @tags : []
8+ @addedOn : 2024-00-00
9+ */
10+
11+ const cursor = "c"
12+ const player1 = "p"
13+ const player2 = "e"
14+
15+ setLegend (
16+ [ cursor , bitmap `
17+ 0..............0
18+ .0............0.
19+ ..0..........0..
20+ ...0........0...
21+ ....0......0....
22+ .....0....0.....
23+ ......0..0......
24+ .......00.......
25+ .......00.......
26+ ......0..0......
27+ .....0....0.....
28+ ....0......0....
29+ ...0........0...
30+ ..0..........0..
31+ .0............0.
32+ 0..............0` ] ,
33+ [ player1 , bitmap `
34+ 5555555555555555
35+ 5555555555555555
36+ 5555555555555555
37+ 5555555555555555
38+ 5555555555555555
39+ 5555555555555555
40+ 5555555555555555
41+ 5555555555555555
42+ 5555555555555555
43+ 5555555555555555
44+ 5555555555555555
45+ 5555555555555555
46+ 5555555555555555
47+ 5555555555555555
48+ 5555555555555555
49+ 5555555555555555` ] ,
50+ [ player2 , bitmap `
51+ 3333333333333333
52+ 3333333333333333
53+ 3333333333333333
54+ 3333333333333333
55+ 3333333333333333
56+ 3333333333333333
57+ 3333333333333333
58+ 3333333333333333
59+ 3333333333333333
60+ 3333333333333333
61+ 3333333333333333
62+ 3333333333333333
63+ 3333333333333333
64+ 3333333333333333
65+ 3333333333333333
66+ 3333333333333333` ]
67+
68+ )
69+
70+ setSolids ( [ player1 , player2 ] )
71+
72+ let level = 0
73+ const levels = [
74+ map `
75+ ...................
76+ ...................
77+ ...................
78+ ...................
79+ ...................
80+ ...................
81+ ...................
82+ ...................
83+ ...................
84+ .........c.........
85+ ...................
86+ ...................
87+ ...................
88+ ...................
89+ ...................
90+ ...................
91+ ...................
92+ ...................
93+ ...................`
94+ ]
95+
96+ setMap ( levels [ level ] )
97+
98+ const countNeighbors = ( x , y ) => {
99+ let p1count = 0 ;
100+ let p2count = 0 ;
101+ let sum = 0 ;
102+
103+ // Check all neighboring tiles
104+ for ( let Y = y - 1 ; Y <= y + 1 ; Y ++ ) {
105+ for ( let X = x - 1 ; X <= x + 1 ; X ++ ) {
106+ if ( X != x || Y != y ) { // Exclude the current tile
107+ let sprites = getTile ( X , Y ) ;
108+ sprites . forEach ( sprite => {
109+ if ( sprite . type === "p" ) {
110+ p1count ++ ;
111+ sum ++ ;
112+ } else if ( sprite . type === "e" ) {
113+ p2count ++ ;
114+ sum ++ ;
115+ }
116+ } ) ;
117+ }
118+ }
119+ }
120+ return [ p1count , p2count ] ;
121+ } ;
122+ let p1placeableCells = 10
123+ let p2placeableCells = 10
124+ addText ( p1placeableCells . toString ( ) , { x : 5 , y : 2 , color : color `5` } )
125+ addText ( p2placeableCells . toString ( ) , { x : 15 , y : 2 , color : color `3` } )
126+
127+
128+ onInput ( "s" , ( ) => {
129+ getFirst ( cursor ) . y += 1
130+ } )
131+
132+ onInput ( "w" , ( ) => {
133+ getFirst ( cursor ) . y -= 1
134+ } )
135+
136+ onInput ( "a" , ( ) => {
137+ getFirst ( cursor ) . x -= 1
138+ } )
139+
140+ onInput ( "d" , ( ) => {
141+ getFirst ( cursor ) . x += 1
142+ } )
143+
144+ onInput ( "i" , ( ) => {
145+ if ( p1placeableCells > 0 ) {
146+ let c = getFirst ( cursor )
147+ clearText ( )
148+ addSprite ( c . x , c . y , player1 )
149+ p1placeableCells -= 1
150+ addText ( p2placeableCells . toString ( ) , { x : 15 , y : 2 , color : color `3` } )
151+ addText ( p1placeableCells . toString ( ) , { x : 5 , y : 2 , color : color `5` } )
152+ } else {
153+ if ( p2placeableCells > 0 ) {
154+ let c = getFirst ( cursor )
155+ clearText ( )
156+ addSprite ( c . x , c . y , player2 )
157+ p2placeableCells -= 1
158+ addText ( p2placeableCells . toString ( ) , { x : 15 , y : 2 , color : color `3` } )
159+ addText ( p1placeableCells . toString ( ) , { x : 5 , y : 2 , color : color `5` } )
160+ }
161+ }
162+ } )
163+
164+ onInput ( "k" , ( ) => {
165+
166+ console . log ( "Game has been quit" ) ;
167+ } ) ;
168+
169+ let timer ;
170+ afterInput ( ( ) => {
171+ let seconds = 15 ; // Initialize seconds inside the afterInput function
172+ if ( p1placeableCells == 0 && p2placeableCells == 0 ) {
173+ let del = getFirst ( cursor )
174+ if ( del ) {
175+ del . remove ( ) ;
176+ clearText ( ) ;
177+ addText ( seconds . toString ( ) , { x : 10 , y : 2 , color : color `6` } ) ;
178+ timer = setInterval ( ( ) => {
179+ updateCells ( seconds ) ;
180+ seconds -- ;
181+ clearText ( ) ;
182+ addText ( seconds . toString ( ) , { x : 10 , y : 2 , color : color `6` } ) ;
183+ if ( seconds === 0 ) {
184+ let player1Count = 0 ;
185+ let player2Count = 0 ;
186+
187+ for ( let y = 0 ; y < height ( ) ; y ++ ) {
188+ for ( let x = 0 ; x < width ( ) ; x ++ ) {
189+ let sprites = getTile ( x , y ) ;
190+ sprites . forEach ( sprite => {
191+ if ( sprite . type === player1 ) {
192+ player1Count ++ ;
193+ } else if ( sprite . type === player2 ) {
194+ player2Count ++ ;
195+ }
196+ } ) ;
197+ }
198+ clearText ( )
199+ if ( player1Count > player2Count ) {
200+ addText ( "Player 1 Wins" , { x : 3 , y : 2 , color : color `6` } ) ;
201+ } else if ( player1Count < player2Count ) {
202+ addText ( "Player 2 Wins" , { x : 3 , y : 2 , color : color `6` } ) ;
203+ } else {
204+ addText ( "Tie" , { x : 10 , y : 2 , color : color `6` } ) ;
205+ }
206+ }
207+ }
208+ } , 1000 ) ;
209+
210+ setTimeout ( ( ) => {
211+ clearInterval ( timer ) ;
212+ } , 15000 ) ;
213+ }
214+ }
215+
216+ } ) ;
217+
218+ function updateCells ( seconds ) {
219+ for ( let y = 0 ; y < height ( ) ; y ++ ) {
220+ for ( let x = 0 ; x < width ( ) ; x ++ ) {
221+ let sprites = getTile ( x , y ) ;
222+ let type = "" ;
223+ let neighbors = countNeighbors ( x , y ) ;
224+ let neighborcount = neighbors [ 2 ] ;
225+
226+ if ( neighbors [ 0 ] > neighbors [ 1 ] ) {
227+ neighborcount = neighbors [ 0 ]
228+ type = "p" ;
229+ } else if ( neighbors [ 1 ] >= neighbors [ 0 ] ) {
230+ type = "e" ;
231+ neighborcount = neighbors [ 1 ]
232+ }
233+
234+ if ( sprites . length > 0 ) {
235+ sprites . forEach ( sprite => {
236+ if ( sprite . type === type ) {
237+ if ( neighborcount < 2 || neighborcount > 3 ) {
238+ sprite . remove ( ) ;
239+ } else if ( neighborcount === 2 || neighborcount === 3 ) {
240+ console . log ( "survived!" ) ;
241+ }
242+ }
243+ } ) ;
244+ } else {
245+
246+ if ( neighborcount === 3 ) {
247+ if ( type === "p" ) {
248+ addSprite ( x , y , player1 ) ;
249+ } else if ( type === "e" ) {
250+ addSprite ( x , y , player2 ) ;
251+ }
252+ }
253+ }
254+ }
255+ }
256+ }
0 commit comments