[pbcode]
; *=---------------------------------------------------------------=*
; Example Name: 'Click The Circle'
; *=---------------------------------------------------------------=*
;
; Level: Beginner
;
; By Kevin Picone
;
; 22,July,2005
;
; *=---------------------------------------------------------------=*
; (c) Copyright 2005 Kevin Picone, All Rights Reserved
; *=---------------------------------------------------------------=*
;
; This example/basic tutorial aims to show programmers some basic
; interaction between a computer controlled character, in this case
; a circle and a single player.
;
; The code set out in three main parts. The Main Loop (which keeps
; it all running), The Intro and the Game.
;
; In this game, the character does not move, it simply waits for the
; player to click on top of it. Once clicked it the character reposition
; it self and waits again. The player has a limited amount of time
; to click the character before the player will loose a life.
;
; So the focus here is to get a some understanding of how to use Variables
; to control the circle character and how to make the player interact with it.
;
; *=---------------------------------------------------------------=*
; Set the GAME to run at a maximum of 60 Frames Per Second.
Setfps 60
; Set the Title of the Play Basic screen to the title of our game
TitleScreen "Click The Circle Game"
; ================================
; Our Programs MAIN LOOP
; ================================
; The main loop, keeps the program running the various parts (into/game)
; of our game.
;Start a DO/LOOP
Do
; ========================================
; Call the Games Title Screen
; ========================================
; The title screen will be displayed until the user
; presses the mouse button to start
; ========================================
Game_Title_Screen()
; ========================================
; Play Game
; ========================================
Play_Game()
; Tell PB to LOOP back to the previous DO Statement above
; to keep the game running,
loop
; *=---------------------------------------------------------------=*
; >> Game TITLE SCreen <<
; *=---------------------------------------------------------------=*
; This section of code displays a simple Title screen.
Function Game_Title_Screen()
; STart a Repeat/Until Loop. This Loop will continue
; executing until the user pressed the LEFT MOUSE Button
Repeat
; Clear the Screen to a purple/redy colour.
Cls rgb(20,30,40)
; Get the Width of the Screen in Dots (PIxels) and store
; value in the Variable 'WIDTH'
Width=GetScreenWidth()
; Get the Height of the Screen in Dots (PIxels) and store
; this value in the Variable 'HEIGHT'
Height=GetScreenHeight()
; Draw a Big Circle as the Title Screen Backdrop
Circlec Width/2,Height/2,Width/3,1,rgb(80,20,80)
; Draws the Games TITLE message, 50 pixels above the center
; of the screen. We calculate the center cordinate from the
; screens Width & Height. So the center point will be half way
; across and down the screen.
; Calc the X positon of the title message (divide width by 2).
x=Width/2
; Calc the Y position of the title message (divide height by 2)
y=(Height/2)-50
; Draw the Games Title to the screen
CenterText x,y,"<<---- Click The Cirle ---->>"
; Add 100 to the previous Y position. We do this so we can print the
; PRESS LEFT MOUSE BUTTON Message 100 pixels bellow the Title message.
; otherwise, the we'd be drawing this message over the title message
y=y+100
; Draw the press mouse button message to the screen.
CenterText x,y,"Press Left Mouse Button To Start"
; Display the Screen to the user
Sync
; Repeat/Until then Left Mouse Button is pressed (='s TRUE)
until LeftMouseButton()=True
; End This function and return to where it was called from.
EndFunction
; *=---------------------------------------------------------------=*
; *=---------------------------------------------------------------=*
; *=---------------------------------------------------------------=*
; *=---------------------------------------------------------------=*
; >> PLay Game <<
; *=---------------------------------------------------------------=*
; *=---------------------------------------------------------------=*
; *=---------------------------------------------------------------=*
; *=---------------------------------------------------------------=*
; The Play Game function runs the actually game part of this example.
;
; First, it creates/initializes the various variables that hold information
; about our player. Then it and the character.
Function PLay_Game()
; First, we'll get some information about the screens dimensions
; Get the current width of the Screen, and place this value in the
; variable ScreenWidth.
ScreenWidth=GetScreenWidth()
; Get the Screen Height and place this value in the ScreenHeight variable
; for later use.
ScreenHeight=GetScreenHeight()
; ==================================
; Init Our Players Variables
; ==================================
; The Player_Lives variable holds the number of lives then player has
; Each time the player looses a life, the program subtracts one away
; from this variable. The game runs using a Repeat/Until loop. Which
; will make the game continue to run, until the number of Lives hits
; zero. Then it's game over
;
Player_Lives =3
; Init the Players Current Source variable.
Player_Score =0
; ==================================
; Init The Alien/Characters Variables (the circle in this example)
; ==================================
; Init the Aliens max standign still time.
Alien_Reset_Move_Timer=200
; Init the Aliens variable to generate(update) it's starting position
; the first time the Repeat/until loop bellow runs.
Alien_Calc_New_position=On
Repeat
Cls 0
; ========================================
; Handle the ALien (circle) character
; ========================================
; Check If the Aliens position/colour and size should be updated.
If Alien_Calc_New_position=On
; Turn the update flag to OFF, so it doesn't constntly
; randomly reposition the alien all the time.
Alien_Calc_New_position=Off
; Reset the Aliens Counter (which counts down) to the
; next move.
Alien_Updates_Left_Till_Move=Alien_Reset_Move_Timer
; Randomly Set the starting X and Y positions for this alien
Alien_Xpos=Rnd(ScreenWidth)
Alien_Ypos=Rnd(ScreenHeight)
; Use the RndRange function to calc a random number between 10 and 50
; This value is then stored in our Alien_radius variable. Which will be
; use for the size of this alien
Alien_Radius=RndRange(10,50)
; Use then RndRGB function to calc a random colour value and store
; it in the Alien_colour variable
ALien_Colour=Rndrgb()
endif
; DRaw a circle to represent our Alien Character
Circlec Alien_Xpos,ALien_Ypos,Alien_Radius,1,Alien_Colour
; Draw the Aliens Updates Left till
CenterText Alien_Xpos,Alien_ypos-5,Alien_Updates_Left_Till_Move
; ================================================================
; Subtract 1 from the "Alien_Updates_Left_Till_Move" variable.
; This variable stores the number of times the alien has
; been standing still. When this counter runs out (gets to zero),
; the player was too slow to click on the alien, so we kill
; then player and display a message
; ==============================================================
dec Alien_Updates_Left_Till_Move
if Alien_Updates_Left_Till_Move<=0
; Reset the Aliens Timer
Alien_Updates_Left_Till_Move=Alien_Reset_Move_Timer
; Draw the OUT OF TIME message in the center of the screen
CenterTExt ScreenWidth/2,ScreenHeight/2,"Out Of Time"
Sync
; Wait two seconds (so the player can see the out of time message
Wait 1000
Wait 1000
; Subtract a Life from the player_Live variable
Dec PLayer_Lives
endif
; ========================================
; Handle the PLayer
; ========================================
; Check the Left Mouse Button is Being Pressed
if LeftMouseButton()=true
if ButtonDown=False
ButtonDown=True
; The player is Clicking, So we now have to
; check if the player clicked on the alien or not.
; First, we the mouses current X and Y position
Mx=Mousex()
My=Mousey()
; Next, we check if this position is inside the Aliens Radius
If POintIntersectCircle(mx,my,Alien_xpos,Alien_ypos,Alien_radius)=true
; Yes the User clicked within the Circle, so we'll Give the player
; score, and set the alien to generate a new position
Player_Score=PLayer_Score+100
; Add the remaining aliens update counter as some extra bonus
; score
Player_Score=PLayer_Score+Alien_Updates_Left_Till_Move
Alien_Calc_New_position=True
; Each time the player clicks the alien, we'll randomly
; decrease it's waiting counter. So the player has less
; time between
Alien_Reset_Move_Timer=Alien_Reset_Move_Timer-rnd(5)
else
; NO, the player clicked, but Missed the alien. So we'll
; display a message and then subtract 10 from the
CenterTExt ScreenWidth/2,ScreenHeight/2,"Missed It"
Player_Score=PLayer_Score-10
endif
endif
else
ButtonDown=False
endif
; Display the PLayers Score in the Top Left corner of the
; screen. To do this we'll use the TEXT command to draw
; a charcters on the screen.
;
Print "Score:"+Str$(PLayer_Score)
Print "Lives:"+Str$(PLayer_Lives)
; Display the Screen
Sync
; Repeat/Until the Number of PLayers Lives is equal to 0. If the PLayers
; Lives variables is not equal to zero, the UNTIL statement will jump
; back to the previous REPEAT statement and continue looping.
; But, If PLayer_Lives variable equals zero, then PB will exit this loop and
; continue on with the lines following it.
Until PLayer_Lives=0
; ============================================================
; Show the Game Over Message and wait for a key to be pressed
; ============================================================
; Clear the Screen to BLACK
cls rgb(0,0,0)
; Center The game Over Message in the middle of the screen
CenterText ScreenWidth/2,(ScreenHeight/2)-50,"Game Over - Press Any Key"
; Display the players final score
CenterText ScreenWidth/2,(ScreenHeight/2)+50,"Your Score:"+str$(player_Score)
; display the screen
Sync
; wait for a key to be pressed.
Waitkey
EndFunction
[/pbcode]
Return To PlayBasic Tutorial Index (http://www.underwaredesign.com/forums/index.php?topic=2552.0)