I am trying to create a 2D shooter and I have created the images and movements seem to be working so far.
I have a monster that I want to move from side to side while a soldier can be controlled by the keyboard. The monster's movements will be predefined and disconnected from the soldier's movements.
I have no idea how to do this, I've tried a few different loops but nothing is working. Please help.
Do you mean you want the monster to scroll left and right like an alien from space invaders?
Or will the monster be more adventurous and move like on the old arcade game Galaxians(up,down,left and right at speed.) If so you might want to consider the monster having four variables for movement, two for the x and y position and two for the rate of change in x and y positions.
Just left and right for now.
It's pretty simple in concept but the purpose of the assignment is collision detection. I haven't gotten to that part of it yet. I can't get two images to move simultaneously and independent of each other.
If you are planning on using more than one monster you could use types to set up the monsters and use a For Each loop to go through the list of monsters to update their location on the screen for each loop of the program.
Something like this:
For monstername=Each monster
monster/x = monster/x+1
monster/y = monster/y+1
Next
Although you'll need to add the other bits yourself, like checking if the monster has hit the limit of how far along the x or y axis it can go or whatever.

Ho ho.. I was inspired.
Here is my code, I have commented the section where the problem lies...
Graphics 800,526
SetBuffer BackBuffer()
Const MoveLeft=203
Const MoveRight=205
Const MoveUp=200
Const MoveDown=208
Global monsterDead = False
Global soldierDead = False
AutoMidHandle True
Global monster=LoadAnimImage("images\monster.bmp", 72, 72, 0, 12)
Global xMonster=460
Global yMonster=99
Global monsterFrame=1
DrawImage monster, xMonster, yMonster, monsterFrame
MaskImage monster, 0, 0, 0
Global egg=LoadImage("images\egg.bmp")
Global xEgg=xMonster
Global yEgg=yMonster
DrawImage egg, xEgg, yEgg
MaskImage egg, 0, 0, 0
Global soldier=LoadAnimImage("images\soldier.bmp", 165, 165, 0, 4)
MidHandle soldier
Global xSoldier=300
Global ySoldier=400
Global frameSoldier=0
DrawImage soldier, xSoldier, ySoldier, frameSoldier
MaskImage soldier, 0, 0, 0
Global background = LoadImage("images\city-scene.bmp")
Function eggThrow()
Cls
DrawImage background, 400, 263
DrawImage monster, xMonster, yMonster, monsterFrame
DrawImage egg, xEgg, yEgg
xEgg = xEgg-30
yEgg = yEgg+45
Delay(30)
Flip
End Function
Function soldierUpdate()
Cls
DrawImage background, 400, 263
DrawImage soldier, xSoldier, ySoldier, frameSoldier
Delay(50)
Flip
End Function
Function monsterUpdate()
Cls
DrawImage background, 400, 263
DrawImage monster, xMonster, yMonster, monsterFrame
Delay(50)
Flip
End Function
Function monsterGrow()
monsterFrame=0
monsterUpdate()
monsterFrame=1
monsterUpdate()
monsterFrame=2
monsterUpdate()
End Function
Function monsterThrow()
monsterFrame=6
monsterUpdate()
monsterFrame=7
monsterUpdate()
monsterFrame=8
monsterUpdate()
For x = 1 To 10 Step 1
eggThrow()
Next
xEgg=xMonster
yEgg=yMonster
End Function
Function monsterDie()
monsterFrame=9
monsterUpdate()
monsterFrame=10
monsterUpdate()
monsterFrame=11
monsterUpdate()
End Function
monsterGrow()
While Not KeyDown(1)
;THE SOLDIER DOES NOT APPEAR UNLESS LEFT OR RIGHT KEY IS PRESSED
;MONSTER DISAPPEARS WHEN SOLDIER APPEARS
While soldierDead = False
If(KeyDown(MoveLeft))
xSoldier=xSoldier-5
frameSoldier=0
soldierUpdate()
xSoldier=xSoldier-5
frameSoldier=1
soldierUpdate()
Else If(KeyDown(MoveRight))
xSoldier=xSoldier+5
frameSoldier=2
soldierUpdate()
xSoldier=xSoldier+5
frameSoldier=3
soldierUpdate()
End If
Wend
If monsterDead = False
For x = 1 To 3 Step 1
xMonster=xMonster+5
monsterFrame=3
monsterUpdate()
xMonster=xMonster+5
monsterFrame=4
monsterUpdate()
xMonster=xMonster+5
monsterFrame=5
monsterUpdate()
Next
For y = 1 To 3 Step 1
xMonster=xMonster-5
monsterFrame=3
monsterUpdate()
xMonster=xMonster-5
monsterFrame=4
monsterUpdate()
xMonster=xMonster-5
monsterFrame=5
monsterUpdate()
Next
monsterThrow()
Else If monsterDead = True
monsterDie()
End If
Wend
End
I think the problem is with calling the functions. Although, it's quite hard to read your code.
Here is one of the first programs, I have done. It's not perfect, but it does pretty much what I think you're after although it moves on both x, and y axis.
Graphics 800,600,16,2
SetBuffer BackBuffer() ;sets the buffer to the backbuffer
AppTitle "Bouncey Ball Thing"
boxsize=2 ;size of the rectangle box
box_x = 20 ;the starting x position
box_y = 100 ;the starting y position
randomx=Rnd(0,8) ;speed to move on the x axis (random)
randomy=Rnd(0,5) ;speed to move on the y axis (random)
While Not KeyHit(1) ;main loop
Cls ;clear screen
Color (255,255,255) ;set colour to white
Rect box_x,box_y,boxsize,boxsize,1 ;draw rectangle
Flip ;flip the buffer (front or back)
If box_x>=800 Then boxright=True ;check if x>=800 pixels
If box_x<=0 Then boxright=False ;check if x<=0 pixels
If box_y>=600 Then boxdown=True ;check if y>=600 pixels
If box_y<=0 Then boxdown=False ;check if y<=0 pixels
If boxright=False Then
box_x=box_x+randomx ;move right
Else
box_x=box_x-randomx ;move left
EndIf
If boxdown=False Then
box_y=box_y+randomx ;move down
Else
box_y=box_y-randomx ;move up
EndIf
Wend ;finish main loop
End
Thanks for your help, unfortunately that's not quite what I'm looking for.
If you run that code with some placeholder images in place of the ones i have defined in the code you will see the problem. The first moving character to be displayed (monster) disappears when the soldier image is introduced to the game.
This bugs the hell out of me and I don't know why it's happening. I just don't know how to keep the monster moving while the soldier is on the screen.
Thank you
A good way to manage this kind of approach would be
Function MonsterMove()
If Mdir=0 then
if Monsterx=>1 then
Monsterx=Monsterx-1
elseif Monsterx=0 then
Monsterx=Monsterx+1
Mdir=1
Return
endif
esleif Mdir=1 then
if Monsterx=<760 then
Monsterx=Monsterx+1
elseif Monsterx=760 then
Monsterx=Monsterx-1
Mdir=0
Return
endif
endif
End Function
[EDIT]
this will cause the monster to move left to right
As for you code you need to change
Function soldierUpdate()
Cls
DrawImage background, 400, 263
DrawImage soldier, xSoldier, ySoldier, frameSoldier
Delay(50)
Flip
End Function
Function monsterUpdate()
Cls
DrawImage background, 400, 263
DrawImage monster, xMonster, yMonster, monsterFrame
Delay(50)
Flip
End Function
to
Function DrawingUpdate()
DrawImage background, 400, 263
DrawImage soldier, xSoldier, ySoldier, frameSoldier
DrawImage monster,xMonster,yMonster,monsterFrame
Delay(50)
Flip
cls
End Function
calls this instead of
soldierUpdate()
monsterUpdate()
Things to note about programming logic
1. The Screen
This is the output of the programme and is the final peace in game logic and is to be show once per game loop
2. Character ie zombie ,soldier
These are definded by a collection of variables which
and can be changed during the game loop
Now when setting the game up you need to do thing in order for the whole drawing process to work
The Main Loop
While not keyhit(1)
UpdateZombie()
UpdateSoldier()
DrawLayout()
DrawZombie()
DrawSoldier()
Delay(50)
Flip
Cls
Wend
So you alter all the possitons and frames of the creatures
Draw the background,layout
Draw the Characters
Flip
Cls
this should now draw correctly to the screen
Its always a good idea to drawout a diagram of your programme flow to help you with the early stages of coding