Moving around a grid

Advanced game related topics
Haplo
User
User
Posts: 25
Joined: Tue Jun 08, 2004 8:45 pm

Moving around a grid

Post by Haplo »

I want to create a grid 12 x 12 and have a player piece start in the top left hand grid square and then be able to move the player piece a set amount of moves either vertically, horizontally or diagonally around the board (for instance the piece can move 3 places) and then transfer the turn across to a other player in the opposite corner.

I am still learning the basics but I assume that the best way to go would be to create the grid using a array. I am a little unsure on how to code the movement (using the arrow keys) and how to check when all the moves are finished and control is passed to the opposite player. Can anyone offer a little bit of wisdom please (I have searched the forum but nothing seems to point me in the right direction).
-----

If a man is standing in the middle of the forest speaking, and there is no woman around to hear him, is he still wrong?
LarsG
Enthusiast
Enthusiast
Posts: 713
Joined: Mon Jun 02, 2003 1:06 pm
Location: Norway
Contact:

Post by LarsG »

This might help you on your way...
(I got abit carried away)

*edit*
I just noticed I had put the DrawingMode() command
inside the nested for loops.. best put it outside
*edit*

Code: Select all

; make some constants
#width = 800 : #height = 600
#gx = 12 : #gy = 12 : #size = 30

; make a Structure for our players
Structure player
  x.l         ; the x coord
  y.l         ; the y coord
  movesleft.l ; how many moves he has left
EndStructure

; then make a couple of players
Global player1. player
Global player2.player
Global turn ; whose turn is it?

; note that the grid array isn't actually used
; in this example... it will only come useful
; when filled with variables etc..
; (like if you want a wall or obstacle there)
;Dim Grid(#gx-1, #gy-1) ; the grid

Procedure DrawGrid()
  ; this procedure draws the playing area
  If StartDrawing(ScreenOutput())
    DrawingMode(4)
    For x = 0 To #gx*#size Step #size
      For y = 0 To #gy*#size Step #size
        Box(x,y,#size,#size, $bbbbbb)
      Next
    Next
    StopDrawing()
  EndIf
EndProcedure

Procedure DrawPlayers()
  ; this procedure draws the players
  If StartDrawing(ScreenOutput())
    Circle((Player1\x*#size)+(#size/2), (player1\y*#size)+(#size/2),(#size/2.5),$ff0000)
    Circle((Player2\x*#size)+(#size/2), (player2\y*#size)+(#size/2),(#size/2.5),$0000ff)     
    StopDrawing()
  EndIf
EndProcedure

Procedure DrawHUD()
  ; this procedure draws the players
  If StartDrawing(ScreenOutput())
    Locate(600,73)
    DrawText("Player:")
    Locate(600,100)
    DrawText("P1 moves left: " + Str(player1\movesleft))
    Locate(600,120)
    DrawText("P2 moves left: " + Str(player2\movesleft))
    If turn = 0
      Circle(#width - 100,80,(#size/2.5),$ff0000)
    Else
      Circle(#width - 100,80,(#size/2.5),$0000ff)
    EndIf 
    StopDrawing()
  EndIf
EndProcedure

Procedure UpdatePlayers()
  ; this updates the players
  ; check turn
  If turn = 0
    If player1\movesleft = 0
      turn = 1-turn
      player2\movesleft = 3 
    EndIf
  Else
    If player2\movesleft = 0
      turn = 1-turn
      player1\movesleft = 3
    EndIf
  EndIf
  ; get the keys    
  If KeyboardReleased(#PB_Key_Left)
    If turn = 0
      If player1\movesleft > 0
        If player1\x > 0
          player1\x - 1
          player1\movesleft -1
        EndIf
      EndIf
    Else
      If player2\movesleft > 0
        If player2\x > 0
          player2\x - 1
          player2\movesleft -1
        EndIf
      EndIf
    EndIf
  EndIf
  If KeyboardReleased(#PB_Key_Right)
    If turn = 0
      If player1\movesleft > 0
        If player1\x < #gx
          player1\x + 1
          player1\movesleft -1
        EndIf
      EndIf
    Else
      If player2\movesleft > 0
        If player2\x < #gx
          player2\x + 1
          player2\movesleft -1
        EndIf
      EndIf
    EndIf
  EndIf
  If KeyboardReleased(#PB_Key_Up)
    If turn = 0
      If player1\movesleft > 0
        If player1\y > 0
          player1\y - 1
          player1\movesleft -1
        EndIf
      EndIf
    Else
      If player2\movesleft > 0
        If player2\y > 0
          player2\y - 1
          player2\movesleft -1
        EndIf
      EndIf
    EndIf
  EndIf
  If KeyboardReleased(#PB_Key_Down)
    If turn = 0
      If player1\movesleft > 0
        If player1\y < #gy
          player1\y + 1
          player1\movesleft -1
        EndIf
      EndIf
    Else
      If player2\movesleft > 0
        If player2\y < #gy
          player2\y + 1
          player2\movesleft -1
        EndIf
      EndIf
    EndIf
  EndIf
    
EndProcedure

Procedure Init()
  ; setup the players
  player1\x = 0
  player1\y = 0  
  player1\movesleft = 3
  player2\x = #gx
  player2\y = #gy 
  player2\movesleft = 3
  
  ; give the turn to player 1
  turn = 0
EndProcedure




InitKeyboard():InitSprite()
; open screen
If OpenScreen(#width,#height,16,"MyGrid")= 0
  MessageRequester("Darn", "Couldn't open screen!")
  End
EndIf

Init()

; main
Repeat
  ; get keys
  ExamineKeyboard()
  ; update players
  UpdatePlayers()
  ; draw grid
  DrawGrid()
  ; draw players
  DrawPlayers()
  ; draw hud
  DrawHUD()
  FlipBuffers() : ClearScreen(0,0,30)
Until KeyboardReleased(#PB_Key_Escape)

End
Last edited by LarsG on Thu Jun 24, 2004 2:58 pm, edited 1 time in total.

AMD Athlon XP2400, 512 MB RAM, Hercules 3D Prophet 9600 256MB RAM, WinXP
PIII 800MHz, 320 MB RAM, Nvidia Riva Tnt 2 Mach 64 (32MB), WinXP + Linux
17" iMac, 1.8 GHz G5, 512 MB DDR-RAM, 80 GB HD, 64 MB Geforce FX 5200, SuperDrive, OSX
Haplo
User
User
Posts: 25
Joined: Tue Jun 08, 2004 8:45 pm

Post by Haplo »

Wow... thankyou so much for taking all that time. It has been a great help.

:D
-----

If a man is standing in the middle of the forest speaking, and there is no woman around to hear him, is he still wrong?
spongehammer
User
User
Posts: 84
Joined: Sat Jul 19, 2003 6:45 pm
Location: UK

Post by spongehammer »

If a man is standing in the middle of the forest speaking, and there is no woman around to hear him, is he still wrong?
LOL :D
I looked up 'paranoid' in the dictionary this morning.
It said, 'what do you want to know that for?'
Post Reply