Code: Select all
;
; Simple blocks in Hollywood
;
@DISPLAY {Width = 350, Height = 360, Title = "Simple Blocks"}
Const #BLOCKSIZE = 16
; Include game framework
@INCLUDE "game.hws"
; Setup
Function game.load()
move_delay = 0
fall_delay = 0
game:setUpdateRate(60) ; set update rate
map:init()
shapes:init()
EndFunction
; Draw
Function game.draw()
Cls()
map:draw()
piece:draw()
EndFunction
; Update
Function game.update(dt)
move_delay = move_delay + dt
If move_delay >= 0.1
piece:move()
move_delay = 0
EndIf
fall_delay = fall_delay + dt
If fall_delay >= 0.4
piece:fall()
fall_delay = 0
EndIf
If IsKeyDown("ESC") Then End
EndFunction
;------------------------------------------------------------------------------------------
map = {}
Function map:init()
self.map = {}
map[-5] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
map[-4] = { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }
map[-3] = { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }
map[-2] = { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }
map[-1] = { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }
map[0] = { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }
map[1] = { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }
map[2] = { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }
map[3] = { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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, 0, 0, 0, 0, 0, 0, 1 }
map[18] = { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }
map[19] = { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }
map[20] = { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }
map[21] = { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }
map[22] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
EndFunction
Function map:draw()
SetFillStyle(#FILLCOLOR)
For Local i = 1 To 21
For Local j = 0 To 21
Box(j * #BLOCKSIZE, i * #BLOCKSIZE, #BLOCKSIZE, #BLOCKSIZE, map[i][j])
Next
Next
SetFillStyle(#FILLNONE)
Box( #BLOCKSIZE-1, 0, 20 * #BLOCKSIZE + 2, 22 * #BLOCKSIZE + 1, #WHITE)
EndFunction
shapes = {}
shapes.shapes = {}
Function shapes:init()
Local shape = { coords = { { { x = 0, y = 1 }, { x = 1, y = 1 }, { x = 2, y = 1 }, { x = 3, y = 1 } },
{ { x = 1, y = 0 }, { x = 1, y = 1 }, { x = 1, y = 2 }, { x = 1, y = 3 } } },
color = #RED }
InsertItem(self.shapes, shape)
Local shape = { coords = { { { x = 0, y = 0 }, { x = 0, y = 1 }, { x = 1, y = 0 }, { x = 1, y = 1 } } },
color = #OLIVE }
InsertItem(self.shapes, shape)
Local shape = { coords = { { { x = 1, y = 0 }, { x = 0, y = 1 }, { x = 1, y = 1 }, { x = 2, y = 1 } },
{ { x = 1, y = 0 }, { x = 1, y = 1 }, { x = 2, y = 1 }, { x = 1, y = 2 } },
{ { x = 0, y = 1 }, { x = 1, y = 1 }, { x = 2, y = 1 }, { x = 1, y = 2 } },
{ { x = 1, y = 0 }, { x = 0, y = 1 }, { x = 1, y = 1 }, { x = 1, y = 2 } } },
color = #TEAL }
InsertItem(self.shapes, shape)
Local shape = { coords = { { { x = 0, y = 0 }, { x = 1, y = 0 }, { x = 1, y = 1 }, { x = 2, y = 1 } },
{ { x = 1, y = 0 }, { x = 0, y = 1 }, { x = 1, y = 1 }, { x = 0, y = 2 } } },
color = #PURPLE }
InsertItem(self.shapes, shape)
Local shape = { coords = { { { x = 1, y = 0 }, { x = 2, y = 0 }, { x = 0, y = 1 }, { x = 1, y = 1 } },
{ { x = 0, y = 0 }, { x = 0, y = 1 }, { x = 1, y = 1 }, { x = 1, y = 2 } } },
color = #BLUE }
InsertItem(self.shapes, shape)
Local shape = { coords = { { { x = 2, y = 0 }, { x = 0, y = 1 }, { x = 1, y = 1 }, { x = 2, y = 1 } },
{ { x = 0, y = 0 }, { x = 0, y = 1 }, { x = 0, y = 2 }, { x = 1, y = 2 } },
{ { x = 0, y = 0 }, { x = 1, y = 0 }, { x = 2, y = 0 }, { x = 0, y = 1 } },
{ { x = 0, y = 0 }, { x = 1, y = 0 }, { x = 1, y = 1 }, { x = 1, y = 2 } } },
color = #NAVY }
InsertItem(self.shapes, shape)
Local shape = { coords = { { { x = 0, y = 0 }, { x = 1, y = 0 }, { x = 2, y = 0 }, { x = 2, y = 1 } },
{ { x = 1, y = 0 }, { x = 1, y = 1 }, { x = 0, y = 2 }, { x = 1, y = 2 } },
{ { x = 0, y = 0 }, { x = 0, y = 1 }, { x = 1, y = 1 }, { x = 2, y = 1 } },
{ { x = 0, y = 0 }, { x = 1, y = 0 }, { x = 0, y = 1 }, { x = 0, y = 2 } } },
color = #AQUA }
InsertItem(self.shapes, shape)
EndFunction
piece = {}
piece.id = 0
piece.x = 1
piece.y = -4
piece.rotation = 0
Function piece:fall()
Local ny = self.y + 1
Local i, v = NextItem(shapes.shapes[self.id].coords[self.rotation])
While GetType(i) <> #NIL
If map[ny + shapes.shapes[self.id].coords[self.rotation][i].y][self.x + shapes.shapes[self.id].coords[self.rotation][i].x] <> 0
Local ii, vv = NextItem(shapes.shapes[self.id].coords[self.rotation])
While GetType(ii) <> #NIL
map[self.y + shapes.shapes[self.id].coords[self.rotation][ii].y][self.x + shapes.shapes[self.id].coords[self.rotation][ii].x] = shapes.shapes[self.id].color
ii, vv = NextItem(shapes.shapes[self.id].coords[self.rotation], ii)
Wend
self.id = Rnd(ListItems(shapes.shapes))
self.rotation = Rnd(ListItems(shapes.shapes[self.id].coords))
self.y = -4
self.x = Rnd(20) + 1
ny = self.y
Break
EndIf
i, v = NextItem(shapes.shapes[self.id].coords[self.rotation], i)
Wend
self.y = ny
EndFunction
Function piece:rotate()
Local nr = self.rotation + 1
nr = Wrap(nr, 0, ListItems(shapes.shapes[self.id].coords))
Local i, v = NextItem(shapes.shapes[self.id].coords[nr])
While GetType(i) <> #NIL
If map[self.y + shapes.shapes[self.id].coords[nr][i].y][self.x + shapes.shapes[self.id].coords[nr][i].x] <> 0
nr = self.rotation
Break
EndIf
i, v = NextItem(shapes.shapes[self.id].coords[nr], i)
Wend
self.rotation = nr
EndFunction
Function piece:move()
Local nx
If IsKeyDown("UP") Then piece:rotate()
If IsKeyDown("LEFT")
nx = self.x - 1
Local i, v = NextItem(shapes.shapes[self.id].coords[self.rotation])
While GetType(i) <> #NIL
If map[self.y + shapes.shapes[self.id].coords[self.rotation][i].y][nx + shapes.shapes[self.id].coords[self.rotation][i].x] <> 0
nx = self.x
Break
EndIf
i, v = NextItem(shapes.shapes[self.id].coords[self.rotation], i)
Wend
self.x = nx
ElseIf IsKeyDown("RIGHT")
nx = self.x + 1
Local i, v = NextItem(shapes.shapes[self.id].coords[self.rotation])
While GetType(i) <> #NIL
If map[self.y + shapes.shapes[self.id].coords[self.rotation][i].y][nx + shapes.shapes[self.id].coords[self.rotation][i].x] <> 0
nx = self.x
Break
EndIf
i, v = NextItem(shapes.shapes[self.id].coords[self.rotation], i)
Wend
self.x = nx
EndIf
EndFunction
Function piece:draw()
SetFillStyle(#FILLCOLOR)
Local i, v = NextItem(shapes.shapes[self.id].coords[self.rotation])
While GetType(i) <> #NIL
Box((self.x + shapes.shapes[self.id].coords[self.rotation][i].x) * #BLOCKSIZE, (self.y + shapes.shapes[self.id].coords[self.rotation][i].y) * #BLOCKSIZE, #BLOCKSIZE, #BLOCKSIZE, shapes.shapes[self.id].color)
i, v = NextItem(shapes.shapes[self.id].coords[self.rotation], i)
Wend
EndFunction
; Go!
game:go()