Ooops
November 24, 2020, 07:22:20 AM

Author Topic: [bmx] Tower Defense by Baystep Productions [ 1+ years ago ]  (Read 898 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Tower Defense
Author : Baystep Productions
Posted : 1+ years ago

Description : So here's the simple makings of a tower defense game, I'm sure a lot can be added to make it better. But this is for the code archives, so I don't care.

The map files are composed of 2 image files, the background, and the "collision" image, which is black and white. Then the final dat file containing the image names and a series of x,y coordinates marking way points in the path.

Press SPACE to buy towers, they cost $10.
Press D to upgrade damage on selected tower.
Press R to upgrade range on selected tower.
Press F to upgrade fire rate on selected tower.
Press ENTER to deselect tower.


Code :
Code: BlitzMax
  1. Strict
  2. Import "ScriptReader.bmx"
  3.  
  4. 'WRITEN BY CHRIS PIKUL
  5. '---------------------
  6. 'FREE TO USE/PUBLIC DOMAIN
  7. SetGraphicsDriver D3D7Max2DDriver()
  8. Graphics 1024,768,0,60,GRAPHICS_ALPHABUFFER
  9.  
  10. Global bg_FileName$,bg_Image:TImage
  11. Global brd_FileName$,brd_Image:TPixmap
  12. Global map_FileName$ = "SimpleTest"                                                                                                     '|<-------------- MAP FILE NAME! WILL LOOK IN MAPS DIRECTORY AND WILL ADD .DAT EXTENSION
  13. Global startX%,startY%,endX%,endY%
  14. Global starting:Point
  15. Global ending:Point
  16. Global money%=25,health%=25
  17. Global sinceLastWave%,waveNumber%=1,waveCount%=1,waveMode%,waveCreated%
  18. Global mouseMode%,mouseSel:Tower,permSel:Tower
  19.  
  20. Global tower1:TImage = LoadAnimImage("ResTower1.png",25,25,0,3)                         '|<-------------- YOUR TOWER IMAGE! First frame is the base, 2 is barrel, 3 is barrel firing
  21. MidHandleImage(tower1)
  22.  
  23. Global path:TList = New TList
  24. Type Point
  25.         Field x%,y%
  26.         Field mode%
  27.         Function Create(x%,y%,mode%=0)
  28.                 Local pnt:Point = New Point
  29.                 pnt.x = x%
  30.                 pnt.y = y%
  31.                 pnt.mode% = mode%
  32.                 path.AddLast pnt
  33.         EndFunction
  34. EndType
  35.  
  36. LoadMap()
  37.  
  38. starting:Point = Point(path.First())
  39. ending:Point = Point(path.Last())
  40.  
  41. Global towers:TList = New TList
  42. Type Tower
  43.         Field x%,y%
  44.         Field damage#
  45.         Field range#
  46.         Field fireRate%
  47.         Field lastFire%
  48.         Field target:Enemy
  49.         Field targetDist#
  50.         Field imageMode%
  51.         Field rot#
  52.         Function Create(setX%,setY%)
  53.                 Local newTower:Tower = New Tower
  54.                 newTower.x = setX%
  55.                 newTower.y = setY%
  56.                 newTower.fireRate%=20
  57.                 newTower.damage# = 1
  58.                 newTower.range# = 75
  59.                 towers.AddLast newTower
  60.         EndFunction
  61. EndType
  62.  
  63. Global wave:TList = New TList
  64. Type Enemy
  65.         Field x#,y#
  66.         Field pointIndex%
  67.         Field nextPoint:Point
  68.         Field health#,speed#
  69.         Function Create()
  70.                 Local newEnemy:Enemy = New Enemy
  71.                 newEnemy.x = starting.x
  72.                 newEnemy.y = starting.y - 25
  73.                 newEnemy.health# = 1+(waveNumber%*0.4)
  74.                 newEnemy.speed# = 0.9+(waveNumber%*0.2)
  75.                 newEnemy.pointIndex% = 0
  76.                 newEnemy.nextPoint:Point = starting
  77.                 wave.AddLast newEnemy
  78.         EndFunction
  79. EndType
  80. Enemy.Create()
  81.  
  82. SetBlend ALPHABLEND
  83. While Not KeyHit(KEY_ESCAPE)
  84.         Cls
  85.         DrawImage(bg_Image,0,0)
  86.         For Local enmy:Enemy = EachIn wave
  87.                 DrawOval(enmy.x-5,enmy.y-5,10,10)
  88.                 If enmy.x<enmy.nextPoint.x
  89.                         If (enmy.nextPoint.x-enmy.x)<enmy.speed#
  90.                                 enmy.x = enmy.nextPoint.x
  91.                         Else
  92.                                 enmy.x = enmy.x+enmy.speed#
  93.                         EndIf
  94.                 ElseIf enmy.x>enmy.nextPoint.x
  95.                         If (enmy.x-enmy.nextPoint.x)<enmy.speed#
  96.                                 enmy.x = enmy.nextPoint.x
  97.                         Else
  98.                                 enmy.x = enmy.x-enmy.speed#
  99.                         EndIf
  100.                 EndIf
  101.                 If enmy.y<enmy.nextPoint.y
  102.                         If (enmy.nextPoint.y-enmy.y)<enmy.speed#
  103.                                 enmy.y = enmy.nextPoint.y
  104.                         Else
  105.                                 enmy.y = enmy.y+enmy.speed#
  106.                         EndIf
  107.                 ElseIf enmy.y>enmy.nextPoint.y
  108.                         If (enmy.y-enmy.nextPoint.y)<enmy.speed#
  109.                                 enmy.y = enmy.nextPoint.y
  110.                         Else
  111.                                 enmy.y = enmy.y-enmy.speed#
  112.                         EndIf
  113.                 EndIf
  114.                 If enmy.x<=(enmy.nextPoint.x+enmy.speed) And enmy.x>=(enmy.nextPoint.x-enmy.speed) And enmy.y<=(enmy.nextPoint.y+enmy.speed) And enmy.y>=(enmy.nextPoint.y-enmy.speed)
  115.                         enmy.pointIndex% = enmy.pointIndex%+1
  116.                         If enmy.nextPoint = ending
  117.                                 wave.Remove enmy
  118.                                 waveCount = waveCount-1
  119.                                 health = health - 1
  120.                         Else
  121.                                 enmy.nextPoint = Point(path.ValueAtIndex(enmy.pointIndex%))
  122.                         EndIf
  123.                 EndIf
  124.                 If enmy.health<=0
  125.                         wave.Remove enmy
  126.                         money=money+1
  127.                         wave.Remove enmy
  128.                         waveCount% = waveCount%-1
  129.                 EndIf
  130.         Next
  131.         For Local twr:Tower = EachIn towers
  132.                 twr.lastFire% = twr.lastFire%+1
  133.                 For Local enmy:Enemy = EachIn wave
  134.                         Local dist# = Sqr((enmy.x-twr.x)^2+(enmy.y-twr.y)^2)
  135.                         If twr.target
  136.                                 If dist#<twr.targetDist#
  137.                                         twr.targetDist#=dist#
  138.                                         twr.target = enmy
  139.                                 EndIf
  140.                         Else
  141.                                 If dist#<twr.range
  142.                                         twr.targetDist#=dist#
  143.                                         twr.target=enmy
  144.                                 EndIf
  145.                         EndIf
  146.                 Next
  147.                 If twr.target
  148.                         twr.targetDist# = Sqr((twr.target.x-twr.x)^2+(twr.target.y-twr.y)^2)
  149.                         twr.rot# = ATan2((twr.y-twr.target.y),(twr.x-twr.target.x))
  150.                         If twr.lastFire%>=twr.fireRate%
  151.                                 twr.imageMode%=5
  152.                                 twr.lastFire%=0
  153.                                 twr.target.health = twr.target.health-twr.damage#
  154.                                 If twr.target.health<=0
  155.                                         twr.target = Null
  156.                                 EndIf
  157.                         EndIf
  158.                         If twr.targetDist#>twr.range#
  159.                                 twr.target = Null
  160.                         EndIf
  161.                 EndIf
  162.                 If MouseX()>=(twr.x-10) And MouseX()<=(twr.x+10) And MouseY()>=(twr.y-10) And MouseY()<=(twr.y+10)
  163.                         mouseSel = twr
  164.                 ElseIf mouseSel=twr
  165.                         mouseSel = Null
  166.                 EndIf
  167.                 DrawImage(tower1,twr.x,twr.y)
  168.                 SetRotation(twr.rot#)
  169.                 If twr.imageMode%>0
  170.                         twr.imageMode% = twr.imageMode%-1
  171.                         DrawImage(tower1,twr.x,twr.y,2)
  172.                 Else
  173.                         DrawImage(tower1,twr.x,twr.y,1)
  174.                 EndIf
  175.                 SetRotation(0)
  176.                 Rem
  177.                 SetAlpha(0.1)
  178.                 SetColor(255,0,0)
  179.                 DrawOval(twr.x-twr.range,twr.y-twr.range,twr.range*2,twr.range*2)
  180.                 SetColor(255,255,255)
  181.                 SetAlpha(1)
  182.                 EndRem
  183.         Next
  184.         If KeyHit(KEY_SPACE) And money>=10 Then mouseMode% = 1
  185.                 Select mouseMode%
  186.                         Case 0
  187.                                 If mouseSel
  188.                                         SetAlpha(0.1)
  189.                                         SetColor(255,0,0)
  190.                                         DrawOval(mouseSel.x-mouseSel.range,mouseSel.y-mouseSel.range,mouseSel.range*2,mouseSel.range*2)
  191.                                         SetColor(255,255,255)
  192.                                         SetAlpha(1)
  193.                                         If MouseHit(1)
  194.                                                 mouseMode%=2
  195.                                                 permSel = mouseSel
  196.                                         EndIf
  197.                                 EndIf
  198.                         Case 1 'Building crap
  199.                                 SetAlpha(0.1)
  200.                                 SetColor(255,0,0)
  201.                                 DrawOval(MouseX()-75,MouseY()-75,150,150)
  202.                                 SetColor(255,255,255)
  203.                                 SetAlpha(1)
  204.                                 If MouseHit(1)
  205.                                         Local pix = brd_Image.ReadPixel(MouseX(),MouseY())
  206.                                         Local pixRed = (pix Shr 16) & $FF
  207.                                         Local pixGreen = (pix Shr 8) & $FF
  208.                                         Local pixBlue = pix & $FF
  209.                                         If pixRed=0 And pixGreen=0 And pixBlue=0
  210.                                                 tower.Create(MouseX(),MouseY())
  211.                                         EndIf
  212.                                         money=money-10
  213.                                         mouseMode%=0
  214.                                 EndIf
  215.                         Case 2 'Selecting crap
  216.                                 DrawText("Damage: "+permSel.damage#+" upgrade cost $"+(2*Int(permSel.damage)),705,55)
  217.                                 DrawText("Range: "+permSel.range#+" upgrade cost $"+Int(Int(permSel.range)*0.026),705,65)
  218.                                 DrawText("Fire Rate: "+permSel.fireRate%+" upgrade cost $"+Int(100-(Int(permSel.fireRate%)*4.75)),705,75)
  219.                                 If KeyHit(KEY_D) And money>=(2*Int(permSel.damage))
  220.                                         money = money - (2*Int(permSel.damage))
  221.                                         permSel.damage# = permSel.damage#+(permSel.damage#*0.25)
  222.                                 EndIf
  223.                                 If KeyHit(KEY_R) And money>=Int(Int(permSel.range)*0.026)
  224.                                         money = money - Int(Int(permSel.range)/0.026)
  225.                                         permSel.range# = permSel.range#+(permSel.range#*0.25)
  226.                                 EndIf
  227.                                 If KeyHit(KEY_F) And money>=Int(100-(Int(permSel.fireRate%)*4.75))
  228.                                         money = money - Int(100-(Int(permSel.fireRate%)*4.75))
  229.                                         permSel.fireRate% = permSel.fireRate%-(permSel.fireRate%*0.10)
  230.                                 EndIf
  231.                                 If KeyHit(KEY_RETURN)
  232.                                         mouseMode% = 0
  233.                                         permSel = Null
  234.                                         FlushKeys()
  235.                                         FlushMouse()
  236.                                 EndIf
  237.                 EndSelect
  238.         DrawText("Cash: $"+money,705,5)
  239.         DrawText("Health: "+health,705,15)
  240.         DrawText("Wave: #"+waveNumber+"/"+waveCount,705,25)
  241.         DrawText("Enemy: Health="+(1+(waveNumber%*0.4))+", Speed="+(0.9+(waveNumber%*0.2)),705,35)
  242.         Flip
  243.        
  244.         If waveCount<=0 And waveMode=0
  245.                 waveNumber = waveNumber+1
  246.                 sinceLastWave=MilliSecs()
  247.                 waveMode=1
  248.         EndIf
  249.         Select waveMode
  250.                 Case 1
  251.                         If MilliSecs()>=sinceLastWave+3000
  252.                                 waveMode = 2
  253.                                 waveCreated = 0
  254.                         EndIf
  255.                 Case 2
  256.                         Enemy.Create()
  257.                         waveCount=waveCount+1
  258.                         waveCreated = waveCreated+1
  259.                         sinceLastWave = MilliSecs()
  260.                         waveMode = 3
  261.                 Case 3
  262.                         If MilliSecs()>=sinceLastWave+200
  263.                                 waveMode = 2
  264.                                 If waveCreated>=(waveNumber*2)
  265.                                         waveMode=0
  266.                                 EndIf
  267.                         EndIf
  268.         EndSelect
  269.         If AppSuspended() Or KeyHit(KEY_P)
  270.                 SetAlpha(0.5)
  271.                 SetColor(0,0,0)
  272.                 DrawRect(0,0,1024,768)
  273.                 SetAlpha(1)
  274.                 SetColor(255,255,255)
  275.                 DrawText("[PAUSED]",500,300)
  276.                 Flip
  277.                 While Not KeyHit(KEY_P)
  278.                         DrawText "PAUSED",0,0
  279.                 Wend
  280.                 FlushKeys()
  281.                 FlushMouse()
  282.         EndIf
  283.         If AppTerminate()
  284.                 If Confirm("Are you sure you want to quit?") Then Exit
  285.         EndIf
  286. Wend
  287. End
  288.  
  289. Function LoadMap()
  290.         Local file:TStream = ReadFile("Maps"+map_FileName$+".dat")
  291.         If Not file Then RuntimeError("Could not load map file!")
  292.         bg_FileName$ = file.ReadLine()
  293.         brd_FileName$ = file.ReadLine()
  294.         Local points% = file.ReadByte()
  295.         For Local temp% = 1 To points%
  296.                 Local tempX% = file.ReadInt()
  297.                 Local tempY% = file.ReadInt()
  298.                 Local tempM% = file.ReadByte()
  299.                 Point.Create(tempX%,tempY%,tempM%)
  300.         Next
  301.         CloseFile(file)
  302.         bg_Image:TImage = LoadImage("Maps"+bg_FileName$)
  303.         brd_Image:TPixmap = LoadPixmap("Maps"+brd_FileName$)
  304. EndFunction
  305.  
  306. 'WRITEN BY CHRIS PIKUL
  307. '---------------------
  308. 'FREE TO USE/PUBLIC DOMAIN


Comments :


Baystep Productions(Posted 1+ years ago)

 A simple map editor to go with it!
Code: [Select]
Strict

Global bg_FileName$,bg_Image:TImage
Global brd_FileName$,brd_Image:TPixmap
Global map_FileName$ = "SimpleTest"
Global lastR%,lastG%,lastB%,lastX%,lastY%
Global startX%,startY%,endX%,endY%

Global cursors:TImage = LoadAnimImage("ResMapEditor_Cursors.png",25,25,0,3)
MidHandleImage(cursors)
Global curMode% = 0
Global editorMode% = 0

Global path:TList = New TList
Global vect:Waypoint
Type Waypoint
Field x,y
Field mode%
Function Create(x%,y%,mode%=0)
Local pnt:Waypoint = New Waypoint
pnt.x = x%
pnt.y = y%
pnt.mode% = mode%
path.AddLast pnt
EndFunction
EndType

Global start:Waypoint,ending:Waypoint


Local leaveLoop=False
Repeat
Print "Tower War, Map Builder Utility"
Print "=============================="
Print "[N]ew file, or [L]oad previous? ([E] to quit application)"
Select Lower(Input())
Case "n" 'New File
Print "Note: All image files should be in the Maps directory, therefor, the directory is not needed in the file name."
bg_FileName$ = Input("Background Image File>")
bg_Image:TImage = LoadImage("Maps"+bg_FileName$)
If Not bg_Image Then Print "Could not locate the image file (or unreadable format) ~q"+bg_FileName$+"~q!"
brd_FileName$ = Input("Boundry Image File>")
brd_Image:TPixmap = LoadPixmap("Maps"+brd_FileName$)
If Not brd_Image Then Print "Could not locate the image file (or unreadable format) ~q"+bg_FileName$+"~q!"
leaveLoop=True
Case "l" 'Load File
Print "Note: All map files are in the Maps directory, therefor, the directory is not needed in the file name."
map_FileName$ = Input("File name>")
LoadMap()
leaveLoop=True
Case "e"
End
Default
Print "Unrecongnized command, try again"
EndSelect
Until leaveLoop
LoadMap()

Graphics 1024,768,0
SetBlend ALPHABLEND
While Not KeyHit(KEY_ESCAPE)
Cls
If KeyHit(KEY_S)
If Not map_FileName$ Then map_FileName$=Input("Map file name>")
SaveMap()
EndIf
If KeyHit(KEY_P)
If editorMode%
editorMode% = 0
If start And ending
If start.x>start.y
start.y=0
Else
start.x=0
EndIf
If ending.x>ending.y
ending.x = 700
Else
ending.y = 700
EndIf
EndIf
Else
editorMode% = 1
EndIf
FlushKeys()
EndIf

If MouseX()<700 And MouseY()<700
Local pix% = brd_Image.ReadPixel(MouseX(),MouseY())
lastR% = (pix% Shr 16) & $FF
lastG% = (pix% Shr 8) & $FF
lastB% = pix% & $FF
EndIf

DrawImage(bg_Image,0,0)
DrawImage(cursors,MouseX(),MouseY(),curMode%)
DrawText("Mouse: "+MouseX()+","+MouseY(),705,10)
DrawText("RGB Values: "+lastR+","+lastG+","+lastB,705,20)
lastX = 0
lastY = 0
For vect:Waypoint = EachIn path
DrawImage(cursors,vect.x,vect.y,2)
If (lastX>0) Or (lastY>0)
DrawLine(lastX,lastY,vect.x,vect.y)
Else
startX% = vect.x
startY% = vect.y
start = vect
EndIf
lastX=vect.x
lastY=vect.y
Next
If vect
endX% = vect.x
endY% = vect.y
ending = vect
EndIf

Select editorMode%
Case 1
DrawText("Drawing Path...",705,30)
If lastR% = 255 And lastG% = 255 And lastB% = 255%
curMode% = 2
Else
curMode% = 1
EndIf
If (lastX>0) Or (lastY>0)
DrawLine(lastX,lastY,MouseX(),MouseY())
EndIf
If MouseHit(1) Then Waypoint.Create(MouseX(),MouseY())

Default
If lastR% = 255 And lastG% = 255 And lastB% = 255%
curMode% = 1
Else
curMode% = 0
EndIf
DrawText("Waiting...",705,30)
EndSelect
DrawText("START",startX,startY)
DrawText("END",endX,endY)

If AppSuspended() Then DrawText "PAUSED",0,0
Flip
If AppTerminate()
If Confirm("Are you sure you want to quit?") Then Exit
EndIf
Wend
End

Function SaveMap()
Local file:TStream = WriteFile("Maps"+map_FileName$+".dat")
If Not file Then RuntimeError("Could not write map file!")
file.WriteLine(bg_FileName$)
file.WriteLine(brd_FileName$)
file.WriteByte(path.Count())
For Local pnt:Waypoint = EachIn path
file.WriteInt(pnt.x%)
file.WriteInt(pnt.y%)
file.WriteByte(pnt.mode%)
Next
CloseFile(file)
EndFunction

Function LoadMap()
Local file:TStream = ReadFile("Maps"+map_FileName$+".dat")
If Not file Then RuntimeError("Could not load map file!")
bg_FileName$ = file.ReadLine()
brd_FileName$ = file.ReadLine()
Local points% = file.ReadByte()
For Local temp% = 1 To points%
Local tempX% = file.ReadInt()
Local tempY% = file.ReadInt()
Local tempM% = file.ReadByte()
Waypoint.Create(tempX%,tempY%,tempM%)
Next
CloseFile(file)
bg_Image:TImage = LoadImage("Maps"+bg_FileName$)
brd_Image:TPixmap = LoadPixmap("Maps"+brd_FileName$)
EndFunction


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal