Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

tower defense game in ahk?


  • Please log in to reply
33 replies to this topic
unknow_user
  • Members
  • 64 posts
  • Last active: Sep 22 2014 07:33 PM
  • Joined: 08 Apr 2013

ist es möglich ein tower defense game in ahk zu schreiben?

wenn ja wie?

giebt es so was in die richtung schon? (wenn ja bitte script b.z.w. link posten)

 

danke im vorraus.

 



nnnik
  • Members
  • 1625 posts
  • Last active: Jan 24 2019 02:19 PM
  • Joined: 28 Jul 2012

 

ist es möglich ein tower defense game in ahk zu schreiben?

wenn ja wie?

giebt es so was in die richtung schon? (wenn ja bitte script b.z.w. link posten)

Ja das ist möglich weil man sogar 3D Spiele in AHK machen kann.

An sich musst du dich mit GDI+ oder OpenGL(OpenGL kann auch 2D) beschäftigen.

Ich würde dir GDI+ empfehlen, das es einfacher zu verstehen ist.

 

An sich wie würde ich es anstellen?

Generell musst du bei Spielen mit Objekten arbeiten, weil das ganze sonst zu viel wird.

Hier in der Community hilft man dir sicher weiter.

Der Hintergrund(Wege etc.) ist generell nur ein Bild.

Die "Map" erstellst du dann Seperat. (Wege, wo platz/ Kein Platz ist)

Darauf "zeichnest" du dann deine Waffen und Monster.

 

Links:

http://www.autohotke...ary-145-by-tic/


Visit the new forum ahkscript.org.

http://ahkscript.org


nnnik
  • Members
  • 1625 posts
  • Last active: Jan 24 2019 02:19 PM
  • Joined: 28 Jul 2012

Da ich selber gerne Tower Defense Spiele spiele hast du mich angestachelt es selber mal mit einem eigenen Spiel zu versuchen.


Visit the new forum ahkscript.org.

http://ahkscript.org


fump2000
  • Members
  • 591 posts
  • Last active: Nov 11 2015 07:52 AM
  • Joined: 01 Nov 2012

He he, hab ich mir schon gedacht das du es versuchen wirst :)



jNizM
  • Members
  • 928 posts
  • Last active: Jan 12 2018 09:23 AM
  • Joined: 01 Aug 2012
Is ja schon nix neues das er solche Ideen gleich selbst ausprobieren will.

Am besten wir machen eine Wunschliste@nnnik mit Ideen was wir noch so alles gebrauchen könnten und dann warten bis er sie abgearbeitet hat grin.png
[AHK] 1.1.27.04 x64 Unicode | [WIN] 10 Pro (Version 1709)
My GitHub Profile | Donations are appreciated if I could help you

nnnik
  • Members
  • 1625 posts
  • Last active: Jan 24 2019 02:19 PM
  • Joined: 28 Jul 2012

Jaja...

Es war eigentlich klar als er gefragt hat grin.png

 

Ich bin ehrlich gesagt sogar schon ziemlich weit.

Aber ich bin glaube ich zu dumm um Sachen zu drehen .

Ich glaube es will einfach nicht funktioneren

SetBatchlines,-1
Gui,Add,Picture,x0 y0 w800 h600 hwndpic,File.png
Gui, -caption
pToken := Gdip_Startup()
pBitmap := Gdip_CreateBitmap(800, 600)
G := Gdip_GraphicsFromImage(pBitmap)
pBitmap2 := Gdip_CreateBitmap(800, 600)
G2 := Gdip_GraphicsFromImage(pBitmap2)
pBrush:=Gdip_BrushCreateSolid(0xff0000FF)
Gdip_FillRectangle(G, pBrush, 0, 0, 800, 600)
Gdip_DeleteBrush(pBrush)
pBrush := Gdip_BrushCreateSolid(0xff00ff00)
DC:=GetDC(pic+0)
G3:=Gdip_GraphicsFromHDC(DC)
Gui,Show, w800 h600
msgbox % Gdip_RotateWorldTransform(G, 70)

Loop,497
{
CopyBitmapOnGraphic(G2,pBitmap,800,600)
Gdip_FillPie(G2, pBrush, A_index, A_Index, 100, 100, 0, 360)
CopyBitmapOnGraphic(G3,pBitmap2,800,600)
Sleep,15
}
Gdip_DeleteBrush(pBrush)
Gdip_DeleteGraphics(G)
Gdip_DeleteGraphics(G2)
Gdip_DeleteGraphics(G3)
Gdip_DisposeImage(pBitmap)
Gdip_DisposeImage(pBitmap2)
Gdip_Shutdown(pToken)
ExitApp


Visit the new forum ahkscript.org.

http://ahkscript.org


nnnik
  • Members
  • 1625 posts
  • Last active: Jan 24 2019 02:19 PM
  • Joined: 28 Jul 2012

Ich hab jetzt herausgefunden wie man dreht aber es ist schlecht weil nicht in der Mitte sondern am Rand gedrehrt wird und damit die Funktion eigentlich unbrauchbar ist -.-


Visit the new forum ahkscript.org.

http://ahkscript.org


nnnik
  • Members
  • 1625 posts
  • Last active: Jan 24 2019 02:19 PM
  • Joined: 28 Jul 2012
#Warn,all,off

newLayer:="Layer"
Graphic:=new layeredGraphics()
Graphic.addlayer()
Graphic.layers[1].Background:={"Redraw1":func("DrawBackground")}


class layer
{

__new(gngine)
{
this.pToken := Gdip_Startup()
this.gngine:=gngine
this.pBitmap := Gdip_CreateBitmap(this.gngine.size.x, this.gngine.size.y)
this.G := Gdip_GraphicsFromImage(this.pBitmap)
this.edit:=1
}

Redraw(layernr)
{
l:=this
For each,obj in l
	if isObject(obj)
	{
		If IsFunc(obj["Redraw" layernr])
			obj["Redraw" layernr](this.gngine)
	}
}	
}



class layeredGraphics
{
layers := []
models := {}

__new()
{
this.startup:=1
this.pToken := Gdip_Startup()
this.size:={"x":800,"y":600}
this.pBitmap := Gdip_CreateBitmap(800, 600)
this.G := Gdip_GraphicsFromImage(this.pBitmap)
this.startup:=0
}

Redraw()
{
layers:=this.layers
For each,layer in 	layers
{
	Gdip_GraphicsClear(layer.G)
	If (layer.edit)
		layer.redraw(each)
	If !layer.virtual
	{
		this.CopyBitmapOnFinalLayer(layer.pbitmap)
		layer.edit:=0
	}
}
return this.pBitmap
}

addlayer(virtual="")
{
	If virtual
		this.layers.insert(new VirtualLayer(this))
	else
		this.layers.insert(new Layer(this))
}


CopyBitmapOnFinalLayer(pBitmap)
{
return DllCall("gdiplus\GdipDrawImageRectRect", "uint", this.G, "uint", pBitmap
	, "float", 0, "float", 0, "float", this.size.x, "float", this.size.y
	, "float", 0, "float", 0, "float", this.size.x, "float", this.size.y
	, "int", 2, "uint", 0, "uint", 0, "uint", 0)
}

LayerDraw(targetlayer,drawfunc,p*)
{
layer:=this.layers[targetlayer]
If layer.virtual
	targetgraphics:=this.G
Else
	targetgraphics:=layer.G
return this[drawfunc](targetgraphics,p*)
}



DrawModel(targetgraphics,modelname,x,y)
{
return DllCall("gdiplus\GdipDrawImageRectRect", "uint", this.G, "uint", this.models[modelname,bitmap]
	, "float", x, "float", y, "float", this.models[modelname,w], "float", this.models[modelname,h]
	, "float", 0, "float", 0, "float", this.models[modelname,w], "float", this.models[modelname,h]
	, "int", 2, "uint", 0, "uint", 0, "uint", 0)
}

/*
DrawRotateModel(targetgraphics,modelname,x,y,angle)	
{
Gdip_RotateWorldTransform(this.models[modelname,"G"], Angle)
DllCall("gdiplus\GdipDrawImageRectRect", "uint", this.G, "uint", this.models[modelname,"bitmap"]
	, "float", x, "float", y, "float", this.models[modelname,w], "float", this.models[modelname,h]
	, "float", 0, "float", 0, "float", this.models[modelname,w], "float", this.models[modelname,h]
	, "int", 2, "uint", 0, "uint", 0, "uint", 0)
Gdip_RotateWorldTransform(this.models[modelname,"G"], -Angle)
}
*/

}

DrawBackground(this,gngine)
{
G:=Gngine.layers.1.g
pBrush:=Gdip_BrushCreateSolid(0xffFFff00)
Gdip_FillRectangle(G, pBrush, 0, 0, 800, 600)
Gdip_DeleteBrush(pBrush)
}












;;;;;;;;;;;;;;;;;;;;;;;;

Gdip_SaveBitmapToFile(Graphic.redraw(), "File.png")



CopyBitmapOnGraphic(pGraphics,pBitmap,w,h)
{
return DllCall("gdiplus\GdipDrawImageRectRect", "uint", pGraphics, "uint", pBitmap
	, "float", 0, "float", 0, "float", w, "float", h
	, "float", 0, "float", 0, "float", w, "float", h
	, "int", 2, "uint", 0, "uint", 0, "uint", 0)
}

Für einige magdas jetzt schwachsinnig klingen aber ich hab 7h an diesem Code gesessen der eig. nichts anderes macht ausser File.png mit einem Gelben Hintergrund in 800x800 zu erstellen.

Als es endlich geklappt hat hab ich mich gefreut wie ein kleines Kind :D

Es gibt noch sehr viel zu tun.


Visit the new forum ahkscript.org.

http://ahkscript.org


nnnik
  • Members
  • 1625 posts
  • Last active: Jan 24 2019 02:19 PM
  • Joined: 28 Jul 2012

OK ich teil euch Hier einfach mal die Fortschritte und Gedanken mit die Ich habe.

Immerhin ist es eine Topic über das Spiele machen, was würde besser passen als "Berichte" und Ideen von jemandem der selber grade an so einem Spiel bastelt.

 

----------------------------------

Die Grafik:

Ich sitze jetzt nun schon mehrere Tage am Anfang aber merke wie ich Langasam dem Ziel ein bischen näher komme.

Der erste Durchbruch ist mir schon gelungen: Ich hab eine Idee gehabt die die Performance deutlich steigern kann:

 

Layer und Modelle:

 

An sich sind es Wege die Performance deutlich zu steigern:

Es gibt viele Dinge die sich nicht ständig ändern (Hintergrund,Weg) andere ändern sich fast jede Sekunde (Bewegende Gegner,schüsse,bewegende Kanone), Würde ich alles einfach neuzeichnen wäre das ziemlich umständlich, vor allem wenn das dargestellte komplex ist.

So entstand bei mir die Idee mehrere Layer zu verwenden. Man kann auf verschiedene Bilder zeichnen ohne dabei die anderen zu verändern. Alle Bider werden dann übereinander gelegt und das fertige Bild entsteht.

So könnte man z.B. den Hintergrund auf den untersten Layer legen, auf ihn einmal die Karte und ein Paar Terrain sachen Zeichnen und müsste diese nie wieder neu Zeichnen.

Auf dem Layer darüber befinden sich dann Kanonen und Feinde.

Diesen muss man eh ständig neu zeichnen aber man gewinnt geschwindigkeit dadurch, dass man Hintergrund nicht neu zeichnen muss.

Ähnlich ist es bei Modellen:

Bei Towerdefense Games ist es häufig so, dass sich viele Objekte oft wiederholen (Kanone,Feind,Kanonenkugel...) ,  meistens nur unterschieden durch Position und Ausrichtung.

Wenn man komplexere Gebilde hat, die viele Zeichenschritte erfordern, kostet das sehr viel Performance.

Anstatt alle einzeln zu zeichnen lasse ich jetzt die Objekte einfach einmal in ein Bild zeichnen welches dann gedreht und gewendet werden kann wie man will.

Ich hab auch noch ein Beispiel welches Modelle Verdeutlicht:

SetBatchlines,-1
If !FileExist("File.png")
pBitmap2 := Gdip_CreateBitmap(800, 800)
Gdip_SaveBitmapToFile(pBitmap2, "File.png")
pToken := Gdip_Startup()
Gui,Add,Picture,x0 y0 w800 h800 hwndpic,File.png
Gui, -caption
pToken := Gdip_Startup()
pBitmap := Gdip_CreateBitmap(400, 400)
G := Gdip_GraphicsFromImage(pBitmap)
G2 := Gdip_GraphicsFromImage(pBitmap2)
pBrush:=Gdip_BrushCreateSolid(0xff0000FF)
Gdip_FillRectangle(G, pBrush, 0, 0, 400, 400)
Gdip_DeleteBrush(pBrush)
Loop,50
{
	Random,x,0,400
	random,y,0,400
	Random,col,0xFF000000,0xFFFFFFFF
	Random,size,20,100
	pBrush:=Gdip_BrushCreateSolid(col)
	Gdip_FillPie(G, pBrush, x, y, size, size, 0, 360)
	Gdip_DeleteBrush(pBrush)
}
pBrush := Gdip_BrushCreateSolid(0xff00ff00)
DC:=GetDC(pic+0)
G3:=Gdip_GraphicsFromHDC(DC)
Gui,Show, w800 h800
Start:=A_Tickcount
Loop,497
{
t:=A_Tickcount+15
Gdip_GraphicsClear(G2,0xFFFFFFFF)
DrawRotatePictureOnGraphics(G2,pBitmap,400,400,400,A_Index-1)
;CopyBitmapOnGraphic(G2,pBitmap,800,800)
Gdip_FillPie(G2, pBrush, A_index, A_Index, 100, 100, 0, 360)
CopyBitmapOnGraphic(G3,pBitmap2,800,800)
Sleep,t-A_Tickcount
}
Msgbox % A_Tickcount-Start
Gdip_DeleteBrush(pBrush)
Gdip_DeleteGraphics(G)
Gdip_DeleteGraphics(G2)
Gdip_DeleteGraphics(G3)
Gdip_DisposeImage(pBitmap)
Gdip_DisposeImage(pBitmap2)
Gdip_Shutdown(pToken)
ExitApp


DrawRotatePictureOnGraphics(G,pBitmap,x,y,size,angle)	;X and Y describe the new center of the model 
{
dist:=(((size/2)**2)*2)**0.5
VarSetCapacity(Points,24,0)
numput(round(sin((45+angle)*0.01745329252)*dist+x),Points,0,"float")
numput(round(cos((45+angle)*0.01745329252)*dist+y),Points,4,"float")
numput(round(sin((135+angle)*0.01745329252)*dist+x),Points,8,"float")
numput(round(cos((135+angle)*0.01745329252)*dist+y),Points,12,"float")
numput(round(sin((315+angle)*0.01745329252)*dist+x),Points,16,"float")
numput(round(cos((315+angle)*0.01745329252)*dist+y),Points,20,"float")
;Msgbox %  "x1:" numget(Points,0,"float") "y1:" numget(Points,4,"float") "x2:" numget(Points,8,"float")"y2:" numget(Points,12,"float") "x3:" numget(Points,16,"float") "y3:" numget(Points,20,"float")
DllCall("gdiplus\GdipDrawImagePointsRect", "uint", G, "uint", pBitmap
	, "ptr", &Points, "int", 3, "float", 0, "float", 0
	, "float", size, "float", size
	, "int", 2, "uint", 0, "uint", 0, "uint", 0)
}


CopyBitmapOnGraphic(pGraphics,pBitmap,w,h)
{
return DllCall("gdiplus\GdipDrawImageRectRect", "uint", pGraphics, "uint", pBitmap
	, "float", 0, "float", 0, "float", w, "float", h
	, "float", 0, "float", 0, "float", w, "float", h
	, "int", 2, "uint", 0, "uint", 0, "uint", 0)
}

Das Viereck, welches sich in der Mitte dreht ist so etwas ähnliches wie ein Objekt.

Es enthält 50 zufällig positionierte Kreise.

Es wäre schwierig die Alle bei jedem neuen Bild neu zu zeichnen also speichere ich sie einmal und kopiere dann das Modell in den vorletzten Layer.


Visit the new forum ahkscript.org.

http://ahkscript.org


nnnik
  • Members
  • 1625 posts
  • Last active: Jan 24 2019 02:19 PM
  • Joined: 28 Jul 2012

Keine Kommentare?


Visit the new forum ahkscript.org.

http://ahkscript.org


nnnik
  • Members
  • 1625 posts
  • Last active: Jan 24 2019 02:19 PM
  • Joined: 28 Jul 2012

Oh mir fällt grade auf das ich beim rüberkopieren einen Fehler gemacht habe:

Hier jetzt das richtige Beispielskript:

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
SetBatchlines,-1
pToken := Gdip_Startup()
pBitmap2 := Gdip_CreateBitmap(800, 800)
If !FileExist("File.png")
Gdip_SaveBitmapToFile(pBitmap2, "File.png")
pToken := Gdip_Startup()
Gui,Add,Picture,x0 y0 w800 h800 hwndpic,File.png
Gui, -caption
pBitmap := Gdip_CreateBitmap(200, 200)
G := Gdip_GraphicsFromImage(pBitmap)
G2 := Gdip_GraphicsFromImage(pBitmap2)
Gdip_DeleteBrush(pBrush)
Loop,50
{
	Random,x,0,200
	random,y,0,200
	Random,col,0xFF000000,0xFFFFFFFF
	Random,size,20,100
	pBrush:=Gdip_BrushCreateSolid(col)
	Gdip_FillPie(G, pBrush, x, y, size, size, 0, 360)
	Gdip_DeleteBrush(pBrush)
}
pBrush := Gdip_BrushCreateSolid(0xff00ff00)
DC:=GetDC(pic+0)
G3:=Gdip_GraphicsFromHDC(DC)
Gui,Show, w800 h800
Start:=A_Tickcount
Loop,497
{
t:=A_Tickcount+15
Gdip_GraphicsClear(G2,0xFFFFFFFF)
DrawRotatePictureOnGraphics(G2,pBitmap,300,300,200,A_Index-1)
DrawRotatePictureOnGraphics(G2,pBitmap,500,500,200,181-A_Index)
DrawRotatePictureOnGraphics(G2,pBitmap,500,300,200,A_Index-1)
DrawRotatePictureOnGraphics(G2,pBitmap,300,500,200,181-A_Index)
;CopyBitmapOnGraphic(G2,pBitmap,800,800)
Gdip_FillPie(G2, pBrush, A_index, A_Index, 100, 100, 0, 360)
CopyBitmapOnGraphic(G3,pBitmap2,800,800)
Sleep,t-A_Tickcount
}
Msgbox % A_Tickcount-Start
Gdip_DeleteBrush(pBrush)
Gdip_DeleteGraphics(G)
Gdip_DeleteGraphics(G2)
Gdip_DeleteGraphics(G3)
Gdip_DisposeImage(pBitmap)
Gdip_DisposeImage(pBitmap2)
Gdip_Shutdown(pToken)
ExitApp
 
 
DrawRotatePictureOnGraphics(G,pBitmap,x,y,size,angle)	;X and Y describe the new center of the model 
{
dist:=(((size/2)**2)*2)**0.5
VarSetCapacity(Points,24,0)
numput(round(sin((45+angle)*0.01745329252)*dist+x),Points,0,"float")
numput(round(cos((45+angle)*0.01745329252)*dist+y),Points,4,"float")
numput(round(sin((135+angle)*0.01745329252)*dist+x),Points,8,"float")
numput(round(cos((135+angle)*0.01745329252)*dist+y),Points,12,"float")
numput(round(sin((315+angle)*0.01745329252)*dist+x),Points,16,"float")
numput(round(cos((315+angle)*0.01745329252)*dist+y),Points,20,"float")
;Msgbox %  "x1:" numget(Points,0,"float") "y1:" numget(Points,4,"float") "x2:" numget(Points,8,"float")"y2:" numget(Points,12,"float") "x3:" numget(Points,16,"float") "y3:" numget(Points,20,"float")
DllCall("gdiplus\GdipDrawImagePointsRect", "uint", G, "uint", pBitmap
	, "ptr", &Points, "int", 3, "float", 0, "float", 0
	, "float", size, "float", size
	, "int", 2, "uint", 0, "uint", 0, "uint", 0)
}
 
 
CopyBitmapOnGraphic(pGraphics,pBitmap,w,h)
{
return DllCall("gdiplus\GdipDrawImageRectRect", "uint", pGraphics, "uint", pBitmap
	, "float", 0, "float", 0, "float", w, "float", h
	, "float", 0, "float", 0, "float", w, "float", h
	, "int", 2, "uint", 0, "uint", 0, "uint", 0)
}

Visit the new forum ahkscript.org.

http://ahkscript.org


nnnik
  • Members
  • 1625 posts
  • Last active: Jan 24 2019 02:19 PM
  • Joined: 28 Jul 2012

Da die mir die Geschwindigkeit bei GDI+ zu Langsam ist (Wurde ja auch nicht dafür entwickelt), werde ich, glaube ich einen Mix aus GDI+ und OpenGL verwenden.


Visit the new forum ahkscript.org.

http://ahkscript.org


Alibaba
  • Members
  • 435 posts
  • Last active: Nov 19 2014 04:21 PM
  • Joined: 01 Nov 2012

So, ich meld mich auch mal wieder im Forum zurück, hab jetzt auch endlich mal wieder Zeit...

Beim durchstöbern durch die ganzen ungelesenen Threads bin ich auf dein kleines Projekt hier gestoßen...

 

Ich verstehe nicht, wieso sich keiner mehr gemeldet hat... Also ich finde das ganze höchst interessant! ;)

Aber es sieht ganz danach aus, als ob das Projekt vorerst beendet wurde, richtig..?


Find the recent autohotkey version here: ahkscript.org


nnnik
  • Members
  • 1625 posts
  • Last active: Jan 24 2019 02:19 PM
  • Joined: 28 Jul 2012

Spoiler
Nein ich hänge (Blending).
Ich rede schon mit Bentschi darüber.
Aber ich kann ja mal den Quellcode zeigen der benutzt wird um ein Bild in den RAM der Grafikkarte zu laden :


Visit the new forum ahkscript.org.

http://ahkscript.org


Alibaba
  • Members
  • 435 posts
  • Last active: Nov 19 2014 04:21 PM
  • Joined: 01 Nov 2012

Wow, sehr beeindruckend!

Für mich leider nicht sehr durchsichtig aufgrund von OpenGL, das ist für mich (noch) ziemlich fremdes Terrain. wink.png

 

Trotzdem hoffe ich mal, dass du das Problem bald in den Griff bekommst und ich bin echt gespannt, was sich aus dem Projekt noch entwickelt!


Find the recent autohotkey version here: ahkscript.org