droping aliens down a notch
BlitzPlus Forums/BlitzPlus Beginners Area/droping aliens down a notch
| ||
I got my aliens in my space invader game going back and for the screen but how can i drop them down a few lines when they hit the side. I tried a simple if y= 2 the x=x-5 but only the end aliens drop down. Here is the ever expanding code [code];************************* ;************************* ;*** PC Invaders *** ;*** *** ;*** By Preston Thomas *** ;************************* ;************************* ;graphics mode Graphics 800,600,32,1 ;set frames per second timer = CreateTimer(30) ;Centre Images AutoMidHandle True ;load ship img_ship = LoadImage("ship.png") ;load ship explode img_shipexp = LoadImage("exp.png") ;load laser img_bullet = LoadImage("fire.png") ;load alien img_alien = LoadImage("alien.png") ;load alienfire img_afire = LoadImage("afire.png") ;load gameover img_gameover = LoadImage("gameover.jpg") ;load title screen img_title = LoadImage("title.png") ;load game background img_earth = LoadImage("earth.png") ;load ship laser snd_laser1 = LoadSound("laser.wav") ;load alien explode snd_aexplode = LoadSound("alien exp.wav") ;load alien laugh snd_laugh = LoadSound("speccy laugh.mp3") ;load ship explode sound snd_shipex = LoadSound ("shipexp.mp3") ;load shield sound snd_shield = LoadSound ("shield removed.wav") ;load alien fire snd_afire = LoadSound ("afire.wav") ;start of menu .menu ;load hiscore Cls If ReadFile("score.dat") = 0 Then highscore = 100 Else file_score = ReadFile("score.dat") highscore = ReadInt (file_score) CloseFile (file_score) EndIf ;draw menu and play music title=PlayMusic ("piano fusion.mp3") While Not KeyDown (1) If KeyDown(57) Then StopChannel (title) : Goto game DrawImage img_title,405,300 Flip Wend ;start of game .game ;setup backbuffer SetBuffer BackBuffer() timer = CreateTimer(30) ;frame value alienframe = 1 ;setup number of shots shot = 0 ;type bullet Type bullet Field x Field y End Type ;type alien Type alien Field x Field y End Type ;type bomb Type bomb Field x Field y End Type ;set up where ship has to be x=400 y=550 ;sets up lives lives = 3 ;sets up score and number of shots score = 0 shot = 1 ;sets alien speed and direction aspeed = 2 amx = aspeed chdir = False ;number of aliens numaliens=0 ;setup layers and rows of aliens layer=2 row = 1 ;setup level level = 0 ;start main loop While Not KeyDown(1) If numaliens = 0 Then ;add level level = level + 1 layer = layer + 1 row = row + 1 If row>16 Then row = 16 If layer>10 Then layer = 10 timer = CreateTimer(level) ;generate aliens For z = 1 To row For w = 1 To layer a.alien = New alien a\x = 50 + 40*z a\y = 10 + 50*w Next Next EndIf ;clear screen and draw background Cls DrawImage img_earth,405,300 ;draw player DrawImage img_ship,x,y If KeyDown(205) Then x=x+3 If KeyDown(203) Then x=x-3 If KeyDown(1) Then End If x<20 Then x=20 If x>780 Then x=780 ;fire bullet If KeyHit(57) And shot = 1 Then PlaySound(snd_laser1) b.bullet = New bullet b\x = x b\y = y shot=0 EndIf ;update and draw For b.bullet = Each bullet b\y = b\y - 5 DrawImage img_bullet,b\x,b\y If b\y=0 Then shot = 1 Next ;movement for aliens If chdir = True Then amx = -amx EndIf chdir = False numaliens = 0 ;update and draw aliens For a.alien = Each alien ;count aliens numaliens = numaliens +1 ;move alien a\x = a\x + amx If a\x > 780 Then chdir = True If a\x < 20 Then chdir = True ;generate bombs If Rand(250) = 25 Then bombs.bomb = New bomb bombs\x = a\x bombs\y = a\y PlaySound (snd_afire) EndIf ;draw aliens DrawImage img_alien,a\x,a\y ;Make Alien Explode For b.bullet = Each bullet If ImagesCollide(img_afire,b\x,b\y,0,img_alien,a\x,a\y,0) score=score+25 PlaySound (snd_aexplode) shot = 1 Delete b Delete a If score > highscore Then highscore = score Exit EndIf Next Next ;update and draw bombs For bombs.bomb = Each bomb bombs\y = bombs\y+4 DrawImage img_afire,bombs\x,bombs\y If ImagesCollide(img_afire,bombs\x,bombs\y,0,img_ship,x,y,0) Delete bombs lives = lives - 1 : PlaySound (snd_shield) ElseIf bombs\y > 560 Then Delete bombs EndIf Next ;endgame screen If lives=0 Then DrawImage img_shipexp,x,y Flip PlaySound (snd_shipex) Delay 3500 ;save hiscore file_score = WriteFile("score.dat") WriteInt(file_score,highscore) CloseFile file_score Cls Delete Each alien PlaySound (snd_laugh) DrawImage img_gameover,405,300 Flip Delay 5000 Cls Flip Goto menu EndIf ;display lives,shields,score,level and high score Text 40,10,"Shields "+lives,1,1 Text 400,10,"Score "+score+" Level "+level,1,1 Text 730,10,"High Score "+highscore,1,1 Flip |
| ||
You should use a rectangular area that has the aliens inside it. When the rectangular area hits the left or right side of the screen, you move the whole bunch down a notch, not just one alien. Alien coordinates should start from 0,0. If alienRectX + alien\x > GraphicsWidth() Then.... and so on... :) |
| ||
What misth said, or a simple quick fix with what you have: if y= 2 for a.alien=each alien a\x=a\x-5 next endif In other words, if one alien hits the edge, move all of them down. |
| ||
thank you so much that's really appreciated |
| ||
Morcar, would you like me to send you the source code for my space invaders game I wrote? |