Selfcalling Loop

Advanced game related topics
sprenio
User
User
Posts: 25
Joined: Sat Jul 16, 2011 1:06 am

Selfcalling Loop

Post by sprenio »

Hi,
i am trying to create a game like pong, but i ran into a problem that i seem to be unable to fix.

Code: Select all

; Pong game v 0.01

Declare DrawingEngine(x)
Declare MoveEngine()
InitSprite()
  
If OpenWindow(24, 0, 0, 800, 200, "Pong Game", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  OpenWindowedScreen(24, 0, 0, 800, 200, 1, 0, 0)
  InitKeyboard()
  ExamineKeyboard()
  adott1 = Random(255)
  adott2 = Random(255)
  adott3 = Random(255)
  StartDrawing(WindowOutput(24))
  Box(x_pos, 5, 20, 80, RGB(adott1, adott2, adott3))
  StopDrawing()
  
  
  Repeat
    MoveEngine()
    DrawingEngine(x)
    keyin = KeyboardPushed(#PB_Key_Q)
    Until keyin = 1
  
EndIf

Procedure DrawingEngine(x)
  StartDrawing(WindowOutput(24))
  x_pos = x
  Box(x_pos, 5, 20, 80, RGB(adott1, adott2, adott2))
  StopDrawing() 
EndProcedure

Procedure MoveEngine()
  If KeyboardPushed(#PB_Key_Up)
    x = x + 1
  ElseIf KeyboardPushed(#PB_Key_Down)
    x = x - 1
  Else
    DrawingEngine(x)
  EndIf
  ProcedureReturn x
EndProcedure
This is the Code, and as you can see, there is a loop to keep the program running the

Code: Select all

 Repeat
    MoveEngine()
    DrawingEngine(x)
    keyin = KeyboardPushed(#PB_Key_Q)
    Until keyin = 1
part. And it seem to be an infinite loop, at least it doesnt let me to do anyting when the game is run, and still i don't know how to break it...:S
So how could i make it like it lets the player to response.
thanks,
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Selfcalling Loop

Post by netmaestro »

I made a couple of changes to your code. First, you have to process window events when using a windowedscreen. Then, you need to pass the WindowID to OpenWindowedScreen, not the #Window. Then, ExamineKeyboard() has to be in your loop, Then, you miss FlipBuffers():

Code: Select all

; Pong game v 0.01

Declare DrawingEngine(x,y)
Declare MoveEngine()
InitSprite()

If OpenWindow(24, 0, 0, 800, 200, "Pong Game", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  OpenWindowedScreen(WindowID(24), 0, 0, 800, 200, 1, 0, 0)
  InitKeyboard()
  
  Global adott1 = Random(255)
  Global adott2 = Random(255)
  Global adott3 = Random(255)
  StartDrawing(WindowOutput(24))
    Box(x_pos, 5, 20, 80, RGB(adott1, adott2, adott3))
  StopDrawing()
  
  
  Repeat
    EventID = WindowEvent()
    While EventID
      If EventID = #PB_Event_CloseWindow
        End
      EndIf
      EventID = WindowEvent()
    Wend
    ExamineKeyboard()
    *pos.POINT = MoveEngine()
    DrawingEngine(*pos\x,*pos\y)
    FlipBuffers()
  Until KeyboardPushed(#PB_Key_Q)
  
EndIf

Procedure DrawingEngine(x,y)
  ClearScreen(0)
  StartDrawing(ScreenOutput())
    Box(x,y, 20, 80, RGB(adott1, adott2, adott2))
  StopDrawing() 
EndProcedure

Procedure.i MoveEngine()
  Static pos.POINT
  If KeyboardPushed(#PB_Key_Up)
    pos\y - 1
  ElseIf KeyboardPushed(#PB_Key_Down)
    pos\y + 1
  ElseIf KeyboardPushed(#PB_Key_Left)
    pos\x - 1
  ElseIf KeyboardPushed(#PB_Key_Right)
    pos\x+1
  EndIf
  ProcedureReturn @pos
EndProcedure
BERESHEIT
sprenio
User
User
Posts: 25
Joined: Sat Jul 16, 2011 1:06 am

Re: Selfcalling Loop

Post by sprenio »

Thank you!
I have an other problem, the program at the moment i guess is running so fast, that if i press a key, i can only see
a line... How can i fix that? To run at a certain speed? I couldn't find any max fps commands or anything like this.
Oh and another problem, after some digging i found the ClearScreen(000000) command, but if i put it into the code like this:

Code: Select all

Procedure DrawingEngine(y)
  ClearScreen(000000)
  StartDrawing(WindowOutput(24))
    y_pos = y
    Box(5, y_pos, 20, 80, RGB(adott1, adott2, adott2))
  StopDrawing() 
EndProcedure
it still draws just a line and not a rectangle...:S
Any ideas how to fix that?
thanks,
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Selfcalling Loop

Post by netmaestro »

Try my posted code again, I modified it so that it actually does something. Also, your StartDrawing(WindowOutput(24)) is useless as the WindowedScreen covers the whole window. You have to draw to ScreenOutput() and FlipBuffers() to see anything. Again, my posted code covers these issues.
BERESHEIT
sprenio
User
User
Posts: 25
Joined: Sat Jul 16, 2011 1:06 am

Re: Selfcalling Loop

Post by sprenio »

Oh, Sorry i am a bit sleepy right now. Thank you, i see it now works... I may have copy pasted my own code lol..:|
Thank you
sprenio
User
User
Posts: 25
Joined: Sat Jul 16, 2011 1:06 am

Re: Selfcalling Loop

Post by sprenio »

Okay, so i used you code, but if i write a Box(400,100, 10, 10, RGB(FF,00,00)) command into the DrawEngine procedure, it just doesn't show up, but somehow, i must make a ball, there and also any AI controlled enemy... So what's the deal with this? WHy it doesn't show up??
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Selfcalling Loop

Post by netmaestro »

Time to revisit your program structure and make some changes. Firstly, you don't want to be drawing to ScreenOutput for every object and so you need to start using sprites instead. Secondly, you need to visualize the behavior of each individual object and assign a structure to it that will store all the data you need to make that behavior happen. I've modified the code to allow for sprites and structured objects and streamlined the program flow. Study what's here and once you understand it and feel able to extend it, start thinking about uncommenting the y_vector line in the move engine to allow for angled shots and what that might mean in terms of collisions. For example, you aren't going to let the ball just go off the top of the screen before it gets to the user paddle, right? So you need to test a collision and reverse the y_vector, but you don't have anything to test it against yet. So there's more sprites to create (and maybe show, maybe not) and more collisions to test. Anyway, start here:

Code: Select all

; Pong game v 0.01

Macro EventSink()
  ev = WindowEvent():While ev:If ev=#PB_Event_CloseWindow:End:EndIf:ev=WindowEvent():Wend
EndMacro

Structure paddle
  x.i
  y.i
  speed.i
  direction.i
EndStructure

Structure ball
  x.d 
  y.d 
  x_vector.d 
  y_vector.d 
EndStructure 

Enumeration ;Windows and gadgets
  #window
EndEnumeration

Enumeration ;Sprites
  #paddle
  #ball
EndEnumeration

Declare DrawingEngine()
Declare MoveEngine()
Declare NewBall()

InitSprite():InitKeyboard()

OpenWindow(#window, 0, 0, 800, 300, "Pong Game", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(#window), 0, 0, 800, 300, 1, 0, 0)

; Create the object faces to display
CreateSprite(#paddle,6,50)
CreateSprite(#ball,6,6)
StartDrawing(SpriteOutput(#paddle))
  Box(0,0,SpriteWidth(#paddle), SpriteHeight(#paddle), #Yellow)
StopDrawing()
StartDrawing(SpriteOutput(#ball))
  Box(0,0,SpriteWidth(#ball), SpriteHeight(#ball), #White)
StopDrawing()

; Create the objects
Global userpaddle.paddle
With userpaddle
  \x       = 20
  \y       = 125
  \speed   = 3
EndWith

Global aipaddle.paddle
With aipaddle
  \x       = 775
  \y       = 125
  \speed   = 2
  \direction = 0
EndWith

Global ball.ball
NewBall()

Repeat
  EventSink()    
  MoveEngine()
  DrawingEngine()
ForEver ; MoveEngine() or EventSink() will end the program

End 

;=========================================
;               Procedures
;=========================================

Procedure DrawingEngine()
  ClearScreen(0)
  DisplaySprite(#paddle,userpaddle\x,userpaddle\y)
  DisplaySprite(#paddle,aipaddle\x,aipaddle\y)
  DisplaySprite(#ball,ball\x,ball\y)
  
  FlipBuffers()
EndProcedure

Procedure NewBall()
  If Random(1)
    direction = 90  ; Right
  Else
    direction = 270 ; Left
  EndIf
  If Random(1)
    angle.d = direction+5+Random(50) ; Up
  Else
    angle.d = direction-5-Random(50) ; Down
  EndIf 
  
  With ball
    \x=WindowWidth(#window)/2-4 : \y=WindowHeight(#window)/2-4
    \x_vector=0.0 + 2 * Sin(angle* #PI / 180.0) 
    \y_vector=0.0 + 2 * Cos(angle* #PI / 180.0)
  EndWith
EndProcedure

Procedure.i MoveEngine()
  
  ; User Paddle
  ExamineKeyboard()
  If KeyboardPushed(#PB_Key_Up)
    userpaddle\y-userpaddle\speed
  ElseIf KeyboardPushed(#PB_Key_Down)
    userpaddle\y+userpaddle\speed
  ElseIf KeyboardPushed(#PB_Key_Left)
    userpaddle\x-userpaddle\speed
  ElseIf KeyboardPushed(#PB_Key_Right)
    userpaddle\x+userpaddle\speed
  ElseIf KeyboardPushed(#PB_Key_Q)
    End 
  EndIf
  
  ; Computer Paddle
  With aipaddle
    If \direction = 0
      \y- \speed : If \y <0 : \y = 0 : \direction = 1 : EndIf
    ElseIf \direction = 1
      \y+ \speed : If \y >250 : \y = 250 : \direction = 0 : EndIf
    EndIf
    If Abs(\x - ball\x) < 7: \y = ball\y-20 : ball\x_vector = 0-ball\x_vector:EndIf ; Simple AI to make sure the computer doesn't miss (for now)
  EndWith

  
  ; Ball 
  ball\x + ball\x_vector
  ; ball\y + ball\y_vector ; For now we won't worry about angles, just go back and forth
  If SpriteCollision(#ball, ball\x, ball\y, #paddle, userpaddle\x, userpaddle\y)
    ball\x_vector=0-ball\x_vector
  ElseIf SpriteCollision(#ball, ball\x, ball\y, #paddle, aipaddle\x, aipaddle\y)
    ball\x_vector=0-ball\x_vector
  EndIf
  
EndProcedure

BERESHEIT
sprenio
User
User
Posts: 25
Joined: Sat Jul 16, 2011 1:06 am

Re: Selfcalling Loop

Post by sprenio »

Ah thank you. It starts to make sense now... :)
sprenio
User
User
Posts: 25
Joined: Sat Jul 16, 2011 1:06 am

Re: Selfcalling Loop

Post by sprenio »

Okey, finally i got it working, thanks to your code, i used it as a blueprint. Here is how it looks, tell me what you think.

Code: Select all

; Pong game v 0.01

Macro EventSink()
  ev = WindowEvent():While ev:If ev=#PB_Event_CloseWindow:End:EndIf:ev=WindowEvent():Wend
EndMacro

Structure paddle
  x.i
  y.i
  speed.i
  direction.i
EndStructure

Structure ball
  x.d 
  y.d 
  x_vector.d 
  y_vector.d 
EndStructure 

Enumeration ;Windows and gadgets
  #window
EndEnumeration

Enumeration ;Sprites
  #paddle
  #ball
EndEnumeration

Declare DrawingEngine()
Declare MoveEngine()
Declare NewBall()

InitSprite():InitKeyboard()

OpenWindow(#window, 0, 0, 800, 300, "Pong Game", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(#window), 0, 0, 800, 300, 1, 0, 0)

; Create the object faces to display
CreateSprite(#paddle,6,50)
CreateSprite(#ball,6,6)
StartDrawing(SpriteOutput(#paddle))
  Box(0,0,SpriteWidth(#paddle), SpriteHeight(#paddle), #Yellow)
StopDrawing()
StartDrawing(SpriteOutput(#ball))
  Box(0,0,SpriteWidth(#ball), SpriteHeight(#ball), #White)
StopDrawing()

; Create the objects
Global userpaddle.paddle
With userpaddle
  \x       = 20
  \y       = 125
  \speed   = 3
EndWith

Global aipaddle.paddle
With aipaddle
  \x       = 775
  \y       = 125
  \speed   = 2
  \direction = 0
EndWith

Global ball.ball
NewBall()

Repeat
  EventSink()    
  MoveEngine()
  DrawingEngine()
ForEver ; MoveEngine() or EventSink() will end the program

End 

;=========================================
;               Procedures
;=========================================

Procedure DrawingEngine()
  ClearScreen(0)
  DisplaySprite(#paddle,userpaddle\x,userpaddle\y)
  DisplaySprite(#paddle,aipaddle\x,aipaddle\y)
  DisplaySprite(#ball,ball\x,ball\y)
  
  FlipBuffers()
EndProcedure

Procedure NewBall()
  If Random(1)
    direction = 90  ; Right
  Else
    direction = 270 ; Left
  EndIf
  If Random(1)
    angle.d = direction+5+Random(50) ; Up
  Else
    angle.d = direction-5-Random(50) ; Down
  EndIf 
  
  With ball
    \x=WindowWidth(#window)/2-4 : \y=WindowHeight(#window)/2-4
    \x_vector=0.0 + 2 * Sin(angle* #PI / 180.0) 
    \y_vector=0.0 + 2 * Cos(angle* #PI / 180.0)
  EndWith
EndProcedure

Procedure.i MoveEngine()
  
  ; User Paddle
  ExamineKeyboard()
  If KeyboardPushed(#PB_Key_Up)
    userpaddle\y-userpaddle\speed
  ElseIf KeyboardPushed(#PB_Key_Down)
    userpaddle\y+userpaddle\speed
  ElseIf KeyboardPushed(#PB_Key_Left)
    userpaddle\x-userpaddle\speed
  ElseIf KeyboardPushed(#PB_Key_Right)
    userpaddle\x+userpaddle\speed
  ElseIf KeyboardPushed(#PB_Key_Q)
    End 
  EndIf
  
  ; Computer Paddle
  With aipaddle
    If \direction = 0
      \y- \speed : If \y <0 : \y = 0 : \direction = 1 : EndIf
    ElseIf \direction = 1
      \y+ \speed : If \y >250 : \y = 250 : \direction = 0 : EndIf
    EndIf
    If Abs(\x - ball\x) < 7: \y = ball\y-20 : ball\x_vector = 0-ball\x_vector:EndIf ; Simple AI to make sure the computer doesn't miss (for now)
  EndWith

  
  ; Ball 
  ball\x + ball\x_vector
  ball\y + ball\y_vector ; For now we won't worry about angles, just go back and forth
  If SpriteCollision(#ball, ball\x, ball\y, #paddle, userpaddle\x, userpaddle\y)
    ball\x_vector=0-ball\x_vector
    ball\y_vector= ball\y_vector + userpaddle\speed
  ElseIf SpriteCollision(#ball, ball\x, ball\y, #paddle, aipaddle\x, aipaddle\y)
    ball\x_vector=0-ball\x_vector
    ball\y_vector= ball\y_vector + aipaddle\speed
  EndIf
  
  ; Collision!
  If userpaddle\y <= 0
    userpaddle\y = 0
  ElseIf userpaddle\y >= 250
    userpaddle\y = 250
  EndIf
  
  If ball\y <= 0
    ball\y_vector = -(ball\y_vector)
  ElseIf ball\y >= 294
    ball\y_vector = -(ball\y_vector)
  EndIf
  
  If ball\x <= 0
    MessageRequester("Game Over", "You did it wrong...", 0)
    End
  EndIf
  
EndProcedure
Actually i see i made a mistake there with the ball's vectors, so the good version looks like this:

Code: Select all

  ; Ball 
  ball\x + ball\x_vector
  ball\y + ball\y_vector ; For now we won't worry about angles, just go back and forth
  If SpriteCollision(#ball, ball\x, ball\y, #paddle, userpaddle\x, userpaddle\y)
    ball\x_vector=0-ball\x_vector
    ball\y_vector= ball\y_vector + ((userpaddle\speed) * userpaddle\direction)
  ElseIf SpriteCollision(#ball, ball\x, ball\y, #paddle, aipaddle\x, aipaddle\y)
    ball\x_vector=0-ball\x_vector
    ball\y_vector= ball\y_vector + ((aipaddle\speed) * aipaddle\direction)
  EndIf
sprenio
User
User
Posts: 25
Joined: Sat Jul 16, 2011 1:06 am

Re: Selfcalling Loop

Post by sprenio »

I actually can't believe this. I used your code, and got stuck again.... I don't know what i do wrong with these loops lol... :@

Code: Select all

; 2D Top-Down "RPG"
;------------------

Macro EventSink()
  ev = WindowEvent():While ev:If ev=#PB_Event_CloseWindow:End:EndIf:ev=WindowEvent():Wend
EndMacro

Declare gameUp()
Declare moveEngine()
Declare drawEngine()
Declare mapCreate()
Declare spriteCreate()

Enumeration
  #MainWindow
  #Walls
  #Player
  #Monsters
EndEnumeration


gameUp()

Repeat
  EventSink()
  moveEngine()
  drawEngine()
ForEver

Procedure gameUp()
  OpenWindow(#MainWindow, 0, 0, 384, 384, "TechDemo", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  InitSprite()
  InitKeyboard()
  OpenWindowedScreen(WindowID(#MainWindow), 0, 0, 384, 384, 1, 0, 0)
  ExamineKeyboard()
  spriteCreate()
  mapCreate()
EndProcedure

Procedure spriteCreate()
  CreateSprite(#Walls, 16, 16, 0)
  CreateSprite(#Player, 8, 8, 0)
  CreateSprite(#Monsters, 8, 8, 0)
EndProcedure

Procedure mapCreate()
  map_height = 24
  map_width = 24
  ; the actual creation of the map:
  Repeat
    Repeat
      If Random(1)
        StartDrawing(SpriteOutput(#Walls))
          Box(x, y, SpriteWidth(#Walls), SpriteHeight(#Walls), #White)
        StopDrawing()
      EndIf
      y = y + 1
    Until y = 24
    x = x + 1
  Until x = 24
EndProcedure

Procedure moveEngine()
  If KeyboardPushed(#PB_Key_W)
    MessageRequester("ASD","ASDASD")
  EndIf
EndProcedure

Procedure drawEngine()
  FlipBuffers()
EndProcedure
So any suggestions?
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Selfcalling Loop

Post by netmaestro »

You had an endless loop in your mapCreate() procedure. You're checking y=24 for the inside loop but once it gets there and control goes back to the outside loop the variable y isn't being reset to zero. It just keeps getting incremented and it'll never hit 24 again. Here's what you need:

Code: Select all

Procedure mapCreate()
  map_height = 24
  map_width = 24
  ; the actual creation of the map:
  x=0
  Repeat
    y=0
    Repeat
      If Random(1)
        StartDrawing(SpriteOutput(#Walls))
          Box(x, y, SpriteWidth(#Walls), SpriteHeight(#Walls), #White)
        StopDrawing()
      EndIf
      y = y + 1
    Until y = 24
    x = x + 1
  Until x = 24
EndProcedure
BERESHEIT
sprenio
User
User
Posts: 25
Joined: Sat Jul 16, 2011 1:06 am

Re: Selfcalling Loop

Post by sprenio »

Thanks, its working now... I gotta hate these loops lol.
sprenio
User
User
Posts: 25
Joined: Sat Jul 16, 2011 1:06 am

Re: Selfcalling Loop

Post by sprenio »

Okey, got it into working properly, but i ran into an obstacle, if i use the same method you used, for creating the sprite, i loose my randomly generated map...

So how can i make the player not drawing a line, without loosing the generated map? The 4 not randomly generated sprites are there to make sure there is no sprite at the players starting position...

Code: Select all

; 2D Top-Down "RPG"
;------------------

Macro EventSink()
  ev = WindowEvent():While ev:If ev=#PB_Event_CloseWindow:End:EndIf:ev=WindowEvent():Wend
EndMacro

Declare gameUp()
Declare moveEngine()
Declare drawEngine()
Declare mapCreate()
Declare spriteCreate()

Global char_x = 4
Global char_y = 4

Enumeration
  #MainWindow
  #Walls
  #Player
  #Monsters
EndEnumeration


gameUp()

Repeat
  EventSink()
  moveEngine()
  drawEngine()
ForEver

Procedure gameUp()
  OpenWindow(#MainWindow, 0, 0, 384, 384, "TechDemo", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  InitSprite()
  InitKeyboard()
  OpenWindowedScreen(WindowID(#MainWindow), 0, 0, 384, 384, 1, 0, 0)
  ExamineKeyboard()
  spriteCreate()
  mapCreate()
EndProcedure

Procedure spriteCreate()
  CreateSprite(#Walls, 16, 16, 0)
  CreateSprite(#Player, 8, 8, 0)
  CreateSprite(#Monsters, 8, 8, 0)
  
  ; ------------------------------
  
  StartDrawing(SpriteOutput(#Player))
    Box(0, 0, SpriteWidth(#Player), SpriteHeight(#Player), #Yellow)
   StopDrawing()
EndProcedure

Procedure mapCreate()
  ; map_height = 24
  ; map_width = 24
  ; the actual creation of the map:
  x=0
  StartDrawing(SpriteOutput(#Walls))
    Box(x, y, SpriteWidth(#Walls), SpriteHeight(#Walls), #White)
  StopDrawing()
  Repeat
    y=0
    Repeat
      If 0 = Random(5) And x > 15 And y > 15
        DisplaySprite(#Walls, x, y)
      EndIf
      y = y + 16
    Until y = 384
    x = x + 16
  Until x = 384
  DisplaySprite(#Walls, 0, 272)
  DisplaySprite(#Walls, 0, 144)
  DisplaySprite(#Walls, 224, 0)
  DisplaySprite(#Walls, 320, 0)
EndProcedure

Procedure moveEngine()
  ExamineKeyboard()
  If KeyboardPushed(#PB_Key_W)
    char_y = char_y - 1
  ElseIf KeyboardPushed(#PB_Key_S)
    char_y = char_y + 1
  ElseIf KeyboardPushed(#PB_Key_A)
    char_x = char_x - 1
  ElseIf KeyboardPushed(#PB_Key_D)
    char_x = char_x + 1
  EndIf
EndProcedure

Procedure drawEngine()
  DisplaySprite(#Player, char_x, char_y)
  FlipBuffers()
EndProcedure
User avatar
graph100
Enthusiast
Enthusiast
Posts: 115
Joined: Tue Aug 10, 2010 3:17 pm

Re: Selfcalling Loop

Post by graph100 »

You should redraw the map each time.

So I just saved It on a new sprite, and then display this sprite for each drawing loop

You don't have to enumerate all the constants in the same Enumeration / EndEnumeration
Because, the window constant and the sprite constant are not the same.

Code: Select all

; 2D Top-Down "RPG"
;------------------

Macro EventSink()
	ev = WindowEvent():While ev:If ev=#PB_Event_CloseWindow:End:EndIf:ev=WindowEvent():Wend
EndMacro

Declare gameUp()
Declare moveEngine()
Declare drawEngine()
Declare mapCreate()
Declare spriteCreate()

Global char_x = 4
Global char_y = 4

Enumeration
	#MainWindow
	#Walls
	#Player
	#Monsters
	#MAP
EndEnumeration


gameUp()

Repeat
	EventSink()
	moveEngine()
	drawEngine()
ForEver

Procedure gameUp()
	OpenWindow(#MainWindow, 0, 0, 384, 384, "TechDemo", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
	InitSprite()
	InitKeyboard()
	OpenWindowedScreen(WindowID(#MainWindow), 0, 0, 384, 384, 1, 0, 0)
	ExamineKeyboard()
	spriteCreate()
	mapCreate()
EndProcedure

Procedure spriteCreate()
	CreateSprite(#Walls, 16, 16, 0)
	CreateSprite(#Player, 8, 8, 0)
	CreateSprite(#Monsters, 8, 8, 0)
	
	; ------------------------------
	
	StartDrawing(SpriteOutput(#Player))
		Box(0, 0, SpriteWidth(#Player), SpriteHeight(#Player), #Yellow)
		StopDrawing()
EndProcedure

Procedure mapCreate()
	; map_height = 24
	; map_width = 24
	; the actual creation of the map:
	x=0
	StartDrawing(SpriteOutput(#Walls))
		Box(x, y, SpriteWidth(#Walls), SpriteHeight(#Walls), #White)
	StopDrawing()
	Repeat
		y=0
		Repeat
			If 0 = Random(5) And x > 15 And y > 15
				DisplaySprite(#Walls, x, y)
			EndIf
			y = y + 16
		Until y = 384
		x = x + 16
	Until x = 384
	DisplaySprite(#Walls, 0, 272)
	DisplaySprite(#Walls, 0, 144)
	DisplaySprite(#Walls, 224, 0)
	DisplaySprite(#Walls, 320, 0)
	
	GrabSprite(#MAP, 0, 0, WindowWidth(#MainWindow), WindowHeight(#MainWindow))
EndProcedure

Procedure moveEngine()
	ExamineKeyboard()
	If KeyboardPushed(#PB_Key_W)
		char_y = char_y - 1
	ElseIf KeyboardPushed(#PB_Key_S)
		char_y = char_y + 1
	ElseIf KeyboardPushed(#PB_Key_A)
		char_x = char_x - 1
	ElseIf KeyboardPushed(#PB_Key_D)
		char_x = char_x + 1
	EndIf
EndProcedure

Procedure drawEngine()
	DisplaySprite(#MAP, 0, 0)
	DisplaySprite(#Player, char_x, char_y)
	FlipBuffers()
EndProcedure
_________________________________________________
My Website : CeriseCode (Warning : perpetual changes & not completed ;))
sprenio
User
User
Posts: 25
Joined: Sat Jul 16, 2011 1:06 am

Re: Selfcalling Loop

Post by sprenio »

Thank you, now i am onto the collision detection... :D
Post Reply