More Enemy??
Blitz3D Forums/Blitz3D Beginners Area/More Enemy??
| ||
Ok this is just a little test game. There is 1 player and 1 enemy on screen. The player is moved via the mouse(Point and click). The enemy just sits there How do i add more enemy to the screen in random places( i know its a simple problem im just not thinking LOL)Say 10 enemies. Graphics3D 800,600,16,2 ;-Types----------------------------------------------------- Type bullet Field x#,y# Field ang#,speed# Field img Field life# End Type Type player Field x#,y# Field ang#,speed# Field img End Type Type enemy Field enx#,eny# Field life Field img End Type Type blood Field x#,y#,dx#,dy# ;location and speed Field timer Field size# Field r,g,b End Type Global me.player=New player Global en.enemy=New enemy Global b.bullet=New bullet Global clickedx,clickedy me\x=400 me\y=300 me\img=CreateImage(9,9) SetBuffer ImageBuffer(me\img) Color 100,255,100 Oval 0,0,8,8 ;player gfx MidHandle me\img en\enx=Rnd(0,800) en\eny=Rnd(0,600) en\img=CreateImage(9,9) SetBuffer ImageBuffer(en\img) Color 255,100,100 Oval 0,0,8,8 ;enemy gfx MidHandle en\img mover# = 1 SetBuffer BackBuffer() ;-Main loop------------------------------------------------- While Not KeyHit(1) Cls If MouseHit(2) clickedx=MouseX() clickedy=MouseY() me\ang=ATan2(clickedx-me\x,clickedy-me\y) me\speed=3 EndIf If Sqr((me\x-clickedx)^2+(me\y-clickedy)^2)<3 me\speed=0 EndIf If MouseHit(1) b.bullet = New bullet ;Create bullet b\x = me\x+4 : b\y = me\y+2 b\ang= ATan2(MouseX()-b\x,MouseY()-b\y)+Rnd(-3,6) b\speed=4 b\life=40 End If For b.bullet = Each bullet movebullet(b) Next ;Move blood For bl.blood = Each blood bl\x = bl\x + bl\dx bl\y = bl\y + bl\dy bl\timer=bl\timer+1 If bl\timer>15 Then Delete bl ;15 frames until destroy Next me\y=me\y+(Cos(me\ang)*me\speed) me\x=me\x+(Sin(me\ang)*me\speed) DrawImage me\img,me\x,me\y ;draw player Color 150,150,150 Oval MouseX(),MouseY(),4,4 ;mouse gfx DrawImage en\img,en\enx,en\eny ;draw enemy ;Collision check ;Collision check If Sqr((me\x-en\enx)^2+(me\y-en\eny)^2)<8 me\speed=0 If ImagesCollide (me\img, me\x, me\y,0, en\img, en\enx, en\eny,0) angle# = ATan2(me\y - en\eny,en\enx - me\x) en\enx = en\enx + (mover * (Cos (angle))) en\eny = en\eny + (mover * (Sin (angle))) Color 255,255,255 Text 350,500," COLLISION " End If End If ;On screen info Color 255,255,255 Text 0,0,"LMB = Shoot / RMB = Move" ;Draw particles For bl.blood = Each blood Color 200,50,50 Oval bl\x-bl\size/2,bl\y-bl\size/2,bl\size,bl\size Next Flip Wend End ;-Functions------------------------------------------------- Function MoveBullet(b.bullet) b\y=b\y+(Cos(b\ang)*b\speed) b\x=b\x+(Sin(b\ang)*b\speed) Color 200,200,100 Rect b\x,b\y,2,2 b\life=b\life-1 ;Collision check If Sqr((b\x-en\enx)^2+(b\y-en\eny)^2)<8 Color 255,55,55 Text 350,500," HIT " b\life=-1 For i=1 To 5 bl.blood = New blood bl\x = b\x bl\y = b\y bl\dx = Rnd(-2,2):bl\dy=Rnd(-2,2) bl\size = 2 Next End If If b\life<0 ;Then Delete b.bullet Delete b.bullet End If End Function Thanks |
| ||
Mostly just put your enemy creation code inside a loop. So likeFor i=1 To 10 en\enx=Rnd(0,800) en\eny=Rnd(0,600) en\img=CreateImage(9,9) SetBuffer ImageBuffer(en\img) Color 255,100,100 Oval 0,0,8,8 ;enemy gfx MidHandle en\img Next You probably want to also have a checking step in the loop so that enemies aren't placed on top of each other. Also you'll want to change your collision detection code to have a For/Each loop to check collisions against all the enemies, not just one. (note: I may have written the For line wrong. I don't exactly remember the syntax off the top of my head.) |
| ||
Thanks JHocking. will try out your code Thanks DESTROYER |
| ||
Ok still no joy at making more enemies :( Im thinking maybe a function to create a set amount of enemies on screen??? Any ideas. Thanks DESTROYER |
| ||
Sooooo, can you be any more specific than "no joy?" Like, what happened when you tried what I suggested? Oh, and I forgot to mention that not only will you need to put the enemy creation code inside a loop, you will also need to put the enemy drawing code in a loop, something like: For en.enemy = Each enemy DrawImage en\img,en\enx,en\eny Next |
| ||
Graphics3D 800,600,16,2 ;-Types----------------------------------------------------- Type bullet Field x#,y# Field ang#,speed# Field img Field life# End Type Type player Field x#,y# Field ang#,speed# Field img End Type Type particle Field x#,y#,dx#,dy# ;location and speed Field timer Field size# Field r,g,b End Type Type enemy Field enx#,eny# Field life Field img End Type Type blood Field x#,y#,dx#,dy# ;location and speed Field timer Field size# Field r,g,b End Type Global me.player=New player Global e.enemy=New enemy Global b.bullet=New bullet Global clickedx,clickedy Global nme col=0 oldx=0 oldy=0 me\x=400 me\y=300 me\img=CreateImage(9,9) SetBuffer ImageBuffer(me\img) Color 100,255,100 Oval 0,0,8,8 ;player gfx MidHandle me\img Global engfx=CreateImage(9,9) SetBuffer ImageBuffer(engfx) Color 255,100,100 Oval 0,0,8,8 ;enemy gfx MidHandle engfx mover# = 1 SetBuffer BackBuffer() ;-Main loop------------------------------------------------- While Not KeyHit(1) Cls ClsColor 50,100,150 If MouseHit(2) clickedx=MouseX() clickedy=MouseY() me\ang=ATan2(clickedx-me\x,clickedy-me\y) me\speed=2 EndIf If Sqr((me\x-clickedx)^2+(me\y-clickedy)^2)<3 me\speed=0 EndIf If MouseHit(1) b.bullet = New bullet ;Create bullet b\x = me\x : b\y = me\y b\ang= ATan2(MouseX()-b\x,MouseY()-b\y)+Rnd(-3,6) b\speed=6 b\life=40 End If For b.bullet = Each bullet movebullet(b) Next ;Move particles For pt.particle = Each particle pt\x = pt\x + pt\dx pt\y = pt\y + pt\dy pt\timer=pt\timer+1 If pt\timer>15 Then Delete pt ;15 frames until destroy Next ;Move blood For bl.blood = Each blood bl\x = bl\x + bl\dx bl\y = bl\y + bl\dy bl\timer=bl\timer+1 If bl\timer>15 Then Delete bl ;15 frames until destroy Next me\y=me\y+(Cos(me\ang)*me\speed) me\x=me\x+(Sin(me\ang)*me\speed) DrawImage me\img,me\x,me\y ;draw player Color 150,150,150 Oval MouseX(),MouseY(),4,4 ;mouse gfx ;Collision check If Sqr((me\x-e\enx)^2+(me\y-e\eny)^2)<8 me\speed=0 If ImagesCollide (me\img, me\x, me\y,0, engfx, e\enx, e\eny,0) angle# = ATan2(me\y - e\eny,e\enx - me\x) e\enx = e\enx + (mover * (Cos (angle))) e\eny = e\eny + (mover * (Sin (angle))) Color 255,255,255 Text 350,500," COLLISION " End If End If ;On screen info Color 255,255,255 Text 0,0,"LMB = Shoot / RMB = Move" ;Draw particles For pt.particle = Each particle Color 150,150,150 Oval pt\x-pt\size/2,pt\y-pt\size/2,pt\size,pt\size Next For bl.blood = Each blood Color 200,50,50 Oval bl\x-bl\size/2,bl\y-bl\size/2,bl\size,bl\size Next createNME() Flip Wend End ;-Functions------------------------------------------------- Function MoveBullet(b.bullet) b\y=b\y+(Cos(b\ang)*b\speed) b\x=b\x+(Sin(b\ang)*b\speed) Color 200,200,100 Rect b\x,b\y,2,2 b\life=b\life-1 ;Collision check If Sqr((b\x-e\enx)^2+(b\y-e\eny)^2)<8 Color 255,55,55 Text 350,500," HIT " b\life=-1 For i=1 To 5 bl.blood = New blood bl\x = b\x bl\y = b\y bl\dx = Rnd(-2,2):bl\dy=Rnd(-2,2) bl\size = 2 Next End If If b\life<0 ;Then Delete b.bullet Delete b.bullet End If End Function Function createNME() en.enemy = New enemy en\enx = Rnd(0,800) en\eny = Rnd(0,600) For en.enemy = Each enemy DrawImage engfx,en\enx,en\eny Next End Function Hi JHocking. Your code would only place 1 enemy onto the screen unless i placed the code in the wrong area LOL The above code now has a function to create enemies but places way too many onto the screen im close but still missing something. I think im doing it the right way?? Thanks DESTROYER |
| ||
Um, no. First off, the creation and drawing bits have to be in separate functions. You only create the enemies once, but you draw them over and over and over. So, both bits of code I posted are meant to overwrite the equivalent code in your original program. Or rather, since I only added a For line at the beginning and Next to the end of each piece, just add the necessary four lines to your original code. No new functions, just type in those lines right in place. |
| ||
Ok i added your code like you said. But now i get a runtime error at this first line of this code:;Collision check ;Collision check If Sqr((me\x-en\enx)^2+(me\y-en\eny)^2)<8 me\speed=0 If ImagesCollide (me\img, me\x, me\y,0, en\img, en\enx, en\eny,0) angle# = ATan2(me\y - en\eny,en\enx - me\x) en\enx = en\enx + (mover * (Cos (angle))) en\eny = en\eny + (mover * (Sin (angle))) Color 255,255,255 Text 350,500," COLLISION " End If End If Thanks DESTROYER |
| ||
It's because en is no longer defined. So you're going to need that same For/Each loop every time en is referred to in your code. In this case for example, put For en.enemy = Each enemy before that block of code, and Next at the end. ADDITION: Also, can you tell us what the error is? Like, instead of just "a runtime error," what is the error message? "Memory Access Violation" I would guess, but knowing for sure would help. Don't forget to try running in debug mode to see if that yields any more information. ADDITION: Now that I think about it, this explanation applies to your original code. Did that code run? |
| ||
Ok the runtime error was object does not exist. I put the for next loop around that bit of code like you said and it gives me an error on the bullet function. Same error object does not exist, But it still only displays 1 enemy on screen Yes i applied your code to my original code. Thanks for all the help :) Thanks DESTROYER |
| ||
I meant to put those lines around all chunks of code that refer to en. So you'll need to do it in the bullet function too. As for why it still only displays 1 enemy on the screen, I just noticed yet another bugaboo. You only create one type object; you need a new one for each enemy. So inside the enemy creation loop, immediately after the For line, you need en.enemy=New enemy |
| ||
Ok i added en.enemy = new enemy after the For line, and now it comes up with an error on DrawImage en\img,en\enx,en\eny line of code, Saying image does not exist Populating a screen with 10 enemy cant be this hard LOL Thanks DESTROYER |
| ||
okaaaay, now try commenting out the Global en.enemy=New enemy I think that's creating a useless empty object, resulting in the error. |
| ||
Ok i can see more than 1 enemy on screen now :) But it is still giving image does not exist error on the DrawImage en\img,en\enx,en\eny line of code. Thanks DESTROYER |
| ||
a) please be more specific: "more than 1 enemy"? How many do you see? And what are you seeing period, considering the program is crashing. Does it show one quick render before crashing? In that case, WaitKey is a very useful command for debugging; start putting pauses in strategic places in your code to see what exactly is happening. For example, I'd put one immediately after the flip. b) post your code again |
| ||
Thanks for taking the time to help me Joe. I no im been a pain lol Im running in debug mode and as i run the program the screen pops up i can see 10 enemy in random places on screen the the error pops up straight away Graphics3D 800,600,16,2 ;-Types----------------------------------------------------- Type bullet Field x#,y# Field ang#,speed# Field img Field life# End Type Type player Field x#,y# Field ang#,speed# Field img End Type Type enemy Field enx#,eny# Field life Field img End Type Type blood Field x#,y#,dx#,dy# ;location and speed Field timer Field size# Field r,g,b End Type Global me.player=New player ;Global en.enemy=New enemy Global b.bullet=New bullet Global clickedx,clickedy me\x=400 me\y=300 me\img=CreateImage(9,9) SetBuffer ImageBuffer(me\img) Color 100,255,100 Oval 0,0,8,8 ;player gfx MidHandle me\img For i=0 To 9 en.enemy=New enemy en\enx=Rnd(0,800) en\eny=Rnd(0,600) en\img=CreateImage(9,9) SetBuffer ImageBuffer(en\img) Color 255,100,100 Oval 0,0,8,8 ;enemy gfx MidHandle en\img Next mover# = 1 SetBuffer BackBuffer() ;-Main loop------------------------------------------------- While Not KeyHit(1) Cls If MouseHit(2) clickedx=MouseX() clickedy=MouseY() me\ang=ATan2(clickedx-me\x,clickedy-me\y) me\speed=3 EndIf If Sqr((me\x-clickedx)^2+(me\y-clickedy)^2)<3 me\speed=0 EndIf If MouseHit(1) b.bullet = New bullet ;Create bullet b\x = me\x+4 : b\y = me\y+2 b\ang= ATan2(MouseX()-b\x,MouseY()-b\y)+Rnd(-3,6) b\speed=4 b\life=40 End If For b.bullet = Each bullet movebullet(b) Next ;Move blood For bl.blood = Each blood bl\x = bl\x + bl\dx bl\y = bl\y + bl\dy bl\timer=bl\timer+1 If bl\timer>15 Then Delete bl ;15 frames until destroy Next me\y=me\y+(Cos(me\ang)*me\speed) me\x=me\x+(Sin(me\ang)*me\speed) DrawImage me\img,me\x,me\y ;draw player Color 150,150,150 Oval MouseX(),MouseY(),4,4 ;mouse gfx For en.enemy = Each enemy DrawImage en\img,en\enx,en\eny ;Collision check ;Collision check If Sqr((me\x-en\enx)^2+(me\y-en\eny)^2)<8 me\speed=0 If ImagesCollide (me\img, me\x, me\y,0, en\img, en\enx, en\eny,0) angle# = ATan2(me\y - en\eny,en\enx - me\x) en\enx = en\enx + (mover * (Cos (angle))) en\eny = en\eny + (mover * (Sin (angle))) Color 255,255,255 Text 350,500," COLLISION " End If End If Next ;On screen info Color 255,255,255 Text 0,0,"LMB = Shoot / RMB = Move" ;Draw particles For bl.blood = Each blood Color 200,50,50 Oval bl\x-bl\size/2,bl\y-bl\size/2,bl\size,bl\size Next Flip Wend End ;-Functions------------------------------------------------- Function MoveBullet(b.bullet) en.enemy=New enemy b\y=b\y+(Cos(b\ang)*b\speed) b\x=b\x+(Sin(b\ang)*b\speed) Color 200,200,100 Rect b\x,b\y,2,2 b\life=b\life-1 ;Collision check If Sqr((b\x-en\enx)^2+(b\y-en\eny)^2)<8 Color 255,55,55 Text 350,500," HIT " b\life=-1 For i=1 To 5 bl.blood = New blood bl\x = b\x bl\y = b\y bl\dx = Rnd(-2,2):bl\dy=Rnd(-2,2) bl\size = 2 Next End If If b\life<0 ;Then Delete b.bullet Delete b.bullet End If End Function Thanks DESTROYER |
| ||
Ive edited out the bullet routine and it works :) yayFor b.bullet = Each bullet ;movebullet(b) Next so i just need to play about with the bullet routine Thanks DESTROYER |
| ||
First off, you do not want to be creating new enemies in your MoveBullet function, so remove the "New enemy" line. Second, you need to be looping through the enemies here just as you do with the drawing and collision detection in your main loop. |
| ||
OK thanks Joe you have been a great help to me. Im going to look through my code and tidy it up a bit for now and try and learn from what you have shown me :) Thanks for the waitkey() tip its handy for checking code I'll let you get back on with what ever you are doing and leave the pestering for another night ;) lol Once again thanks for the help M8 :) Thanks DESTROYER |
| ||
I was like "Night? It's the middle of the afternoon." Then it occurred to me you're probably in the UK. No prob on the help. Do study the code to learn. |
| ||
Stop is even more useful than WaitKey for debugging ;) |
| ||
Thanks for the tip octothorpe. Ok here is my new code(still 1 problem tho) Graphics3D 800,600,16,1 Type player Field x#,y# Field ang#,speed# Field img End Type Type enemy Field enx#,eny# Field life Field img End Type Type bullet Field x#,y# Field ang#,speed# Field img Field life# End Type Const Gunsize = 8 Global me.player = New player Global clickedx = MouseX() Global clickedy = MouseY() Global mover = 1 ;player gfx---------- me\x=400 me\y=300 me\img=CreateImage(9,9) SetBuffer ImageBuffer(me\img) Color 100,255,100 Oval 0,0,8,8 MidHandle me\img ;-------------------- ;enemy gfx----------- For i=0 To 9 en.enemy=New enemy en\enx=Rnd(0,800) en\eny=Rnd(0,600) en\img=CreateImage(9,9) SetBuffer ImageBuffer(en\img) Color 255,100,100 Oval 0,0,8,8 MidHandle en\img Next ;-------------------- HidePointer SetBuffer BackBuffer() SeedRnd MilliSecs() ;**MAIN LOOP**-------------------------------------- While Not KeyDown(1) Cls Color 150,150,150 Oval MouseX(),MouseY(),4,4 ;mouse gfx DrawPlayer() DrawEnemy() UpdatePlayer() Flip Wend FreeImage me\img FreeImage en\img End ;-------------------------------------------------- ;-------------------------------------------------- ;Functions----------------------------------------- ;-------------------------------------------------- ;-------------------------------------------------- Function DrawPlayer() DrawImage me\img,me\x,me\y ;draw player End Function ;-------------------------------------------------- Function UpdatePlayer() If MouseHit(1) clickedx=MouseX() clickedy=MouseY() me\ang=ATan2(clickedx-me\x,clickedy-me\y) me\speed=2 EndIf If Sqr((me\x-clickedx)^2+(me\y-clickedy)^2)<3 me\speed=0 EndIf me\y=me\y+(Cos(me\ang)*me\speed) me\x=me\x+(Sin(me\ang)*me\speed) dX = MouseX() - me\x dY = MouseY() - me\y Gunang = ATan2(dY,dX) Gundx# = Cos(Gunang) Gundy# = Sin(Gunang) GunendX = me\x + gundx*Gunsize GunendY = me\y + gundy*Gunsize Color 100,255,100 Line(me\x,me\y,gunendx,gunendy) ;draw gun If MouseHit(2) b.bullet = New bullet ;Create bullet b\x = gunendx : b\y = gunendy b\ang= ATan2(MouseX()-b\x,MouseY()-b\y)+Rnd(-3,6) b\speed=6 b\life=40 End If For b.bullet = Each bullet movebullet(b) Next End Function ;-------------------------------------------------- Function DrawEnemy() For en.enemy = Each enemy DrawImage en\img,en\enx,en\eny If ImagesCollide (me\img, me\x, me\y,0, en\img, en\enx, en\eny,0) angle# = ATan2(me\y - en\eny,en\enx - me\x) en\enx = en\enx + (mover * (Cos (angle))) en\eny = en\eny + (mover * (Sin (angle))) Color 255,255,255 Text 350,500," COLLISION " me\speed=0 End If Next End Function ;-------------------------------------------------- Function MoveBullet(b.bullet) b\y=b\y+(Cos(b\ang)*b\speed) b\x=b\x+(Sin(b\ang)*b\speed) Color 200,200,100 Rect b\x,b\y,2,2 ;Collision check If Sqr((b\x-en\enx)^2+(b\y-en\eny)^2)<8 Color 255,55,55 Text 350,500," HIT " b\life=-1 End If b\life=b\life-1 If b\life<0 ;Then Delete b.bullet Delete b.bullet End If End Function ;-------------------------------------------------- Ok now i get an error on this line inside the MoveBullet Function. ;Collision check If Sqr((b\x-en\enx)^2+(b\y-en\eny)^2)<8 "Variable must be a type" is the error. Any help?? Thanks DESTROYER |
| ||
The variable 'e' isn't defined or passed into the MoveBullet function so defaults to being an integer. |
| ||
oops my bad it should of been ;Collision check If Sqr((b\x-en\enx)^2+(b\y-en\eny)^2)<8 Ive re-updated the above code :) Thanks big10p. So how do i go about passing it into the MoveBullet() function?? My problem is that i never know where code should be placed and executed. Thanks DESTROYER :) |
| ||
Change the collision code in MoveBullet to this:;Collision check For en.enemy = Each enemy If Sqr((b\x-en\enx)^2+(b\y-en\eny)^2)<8 Color 255,55,55 Text 350,500," HIT " b\life=-1 End If Next [edit] I posted my replies without reading this whole thread but now I have I see that jhocking has already said how to remedy the problems I posted about. Destroyer, please read all replies carefully - you were already given the solutions making my posts a waste of time. ;) |
| ||
I did explain what to do, but I didn't post the exact code. Destroyer, you can't expect other people to write all your code for you; it is imperative that you study the things we've told you in order to understand why we are telling you these things. If you actually understood the stuff I told you, you would have no trouble extrapolating that knowledge to other areas of your code. |
| ||
Thanks Joe, big10p and any body else that has helped It was my fault for leaving a simple For Next loop out of the MoveBullet function LOL. I do try tho to work things out myself and im greatfull when people do help me. I also like to try and code things myself rather then keep asking for code, But when it comes to simple routines my mind goes blank lolz. Ill stick to the harder routines in future LOL Thanks DESTROYER |