[Tutorial] Making a 'Character' move
Author |
Message |
DaveAngus
|
Posted: Tue Apr 08, 2008 9:31 am Post subject: [Tutorial] Making a 'Character' move |
|
|
HOW TO MAKE A 'CHARACTER' MOVE WITHIN A GAME
At the bottom of this tutorial I have posted a code of the final project. We are going to work together to come out this that program as the final output.
The reasonw hy it is there is because if you are truely suck and need to reference it it is there for you help.
BUT DONT CHEAT!
This will only hurt yourself!
Also, this is my first Tutorial.
Please offer constructive critisisum
It will only help me for future tutorials!
OK so first we need to set our variable. I will give you all of the variables that we will use for the wole tutorial. I am taking it that all of you know how to set a variable so that is why I am not focusing on that.
code: |
var radius : int
var xpos, ypos : int
var xdir, ydir : int
var key : array char of boolean
var col1 : int
var boxx : int
var boxy : int
|
Now we have to initialize all of the variables.
Think, what are the boarders of the screen?
What is the radius of the circle?
And what is the speed of the circle while its moving?
You dont want it to move too fast but you dont want it to drag on.
NOW TRY TO DO THAT CODE WITHOUT LOOKING DOWN!!!!
Also there is a 'black hole' in this code. It is randomly positioned and you have to
run into it and it will change location. THat is what the rand int's are below. Please just add them to your code so that we have identical codes.
code: |
radius := 25
xpos := 50
ypos := 50
xdir := 1
ydir := 1
col1 := 5
randint (boxx, 1, maxx-25)
randint (boxy, 1, maxy-25)
|
Ok so now that we have initialized our variables, we need to start the main program
We need a loop.
To keep it clean why not add a 'cls'?
We also need to add the oval and box (black hole) to the code.
Now draw a box and an oval.
The oval will be our 'character'
and out box will be our 'black hole'
code: |
drawfilloval (xpos, ypos, radius, radius, col1)
drawfillbox (boxx, boxy, boxx + 25, boxy + 25, black)
|
Ok so now that we have our 'character', we need to set our key controles.
What direction is each arrow going to make the character go?
Ok so when we kit the up key we need the program to move the character up.
We need it to check the y position and relate it to the radius.
code: |
if key (KEY_UP_ARROW) then
if ypos > maxy - radius then
ypos := maxy - radius
else
ypos := ypos + ydir
end if
|
Using this will make you able to move the ball up until it hits the top of the screen.
Now this is your turn,
Try this with the down,left and right key.
Try it without scrolling down.
Need help?????
code: |
elsif key (KEY_DOWN_ARROW) then
if ypos < radius then
ypos := radius
else
ypos := ypos - ydir
end if
elsif key (KEY_RIGHT_ARROW) then
if xpos > maxx - radius then
xpos := maxx - radius
else
xpos := xpos + xdir
end if
elsif key (KEY_LEFT_ARROW) then
if xpos < radius then
xpos := radius
else
xpos := xpos - xdir
end if
|
Ok now that the 'character' can be moved, we need to make it so that when you hit the 'black hole' it will recognise that you have hit it and then change location.
We are going to need an equasion that checks if the ball has made contact with the 'black hole'
You need to check if the xpos is > boxx and if the xpos is < boxx + 25.
That will make it check the x position to see if contact has been made.
Now we need to check the y position to see if the y axis has been contacted.
so why not use an almost identical code to what you used for the xpox and boxx?
would ypos > boxy and pos < boxy + 25 work?
Ok so now that we have that done,
we need to make the box change location when the box is hit.
We are going to have to randomize the box x and y position.
we we are also going to have to move the box slightly off the the border so that no clipping will occur.
You will need 4 randints
TRY THIS NOW!!!!!
code: |
if xpos > boxx and xpos < boxx + 25 and ypos > boxy and ypos < boxy + 25 then
randint (boxx, 1, maxx-25)
randint (boxy, 1, maxy-25)
randint (xpos, 1, maxx-25)
randint (ypos, 1, maxy-25)
end if
|
Now that this has need completed
we need to put whats on our buffer to the screen.
We also need to end out loop that we started at the beginning of our code.
code: |
View.Update
end loop
|
CONGRATULATIONS you have now completed the task of making a 'character' move!!!!
If you have any input on how to better my tutorial just let me know because I would always like to better my work!!!
Thank you for taking the time to read this!
DAVE ANGUS
code: |
%Name:Dave Angus
%File: moveball1.t
%Date:March 20th 2008
%
%Description: Game that moves ball and has a black hole.
setscreen ("graphics:m256")
setscreen ("offscreenonly")
%Declaration of variables
var radius : int
var xpos, ypos : int
var xdir, ydir : int
var key : array char of boolean
var col1 : int
var boxx : int
var boxy : int
%Initialization of variables
radius := 25
xpos := 50
ypos := 50
xdir := 1
ydir := 1
col1 := 5
randint (boxx, 1, maxx-25)
randint (boxy, 1, maxy-25)
%Start of main program.
loop
%Clear the screen
cls
%Draw the circle to our buffer
drawfilloval (xpos, ypos, radius, radius, col1)
drawfillbox (boxx, boxy, boxx + 25, boxy + 25, black)
%Key the pressed key from the keyboard
Input.KeyDown (key)
%Moved the ball up until it hit the top of the screen
if key (KEY_UP_ARROW) then
if ypos > maxy - radius then
ypos := maxy - radius
else
ypos := ypos + ydir
end if
%Moves the ball down until it hits the bottom of the screen
elsif key (KEY_DOWN_ARROW) then
if ypos < radius then
ypos := radius
else
ypos := ypos - ydir
end if
%moves the ball right until it hits the right of the screen
elsif key (KEY_RIGHT_ARROW) then
if xpos > maxx - radius then
xpos := maxx - radius
else
xpos := xpos + xdir
end if
%moves the ball left until it hits the left of the screen
elsif key (KEY_LEFT_ARROW) then
if xpos < radius then
xpos := radius
else
xpos := xpos - xdir
end if
end if
if xpos > boxx and xpos < boxx + 25 and ypos > boxy and ypos < boxy + 25 then
randint (boxx, 1, maxx-25)
randint (boxy, 1, maxy-25)
randint (xpos, 1, maxx-25)
randint (ypos, 1, maxy-25)
end if
%Put what's on our buffer to the screen
View.Update
end loop
|
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
BigBear
|
Posted: Tue Apr 08, 2008 10:46 am Post subject: Re: [Tutorial] Making a 'Character' move |
|
|
Good tutorial I enjoyed the effort put into the structure.
This is the constructive criticisms part:
In the beginning you could have used constants or added what the value of the variables are when you declared them but the same effect is achieved just a little longer.
I do not know if you wanted to create a little bouncing effect to stop the circle from leaving the screen but a simpler and shorter way would be to use one if statement to move
Also you never set the screen to offscreenonly, so someone following you tutorial will have to copy your final program because there output will be all white.
And this
can be shortend to
Without the m which I have no idea what it does.
|
|
|
|
|
|
DaveAngus
|
Posted: Tue Apr 08, 2008 11:46 am Post subject: RE:[Tutorial] Making a \'Character\' move |
|
|
THanks a lot big bear for the post!!!
what you are saying is definitly much better.
Im just trying to keep it very simple.
I could edit that in when Im not in a rush! haha Thanks a lot!!!
-Dave
|
|
|
|
|
|
Mackie
|
Posted: Tue Apr 08, 2008 7:26 pm Post subject: RE:[Tutorial] Making a \'Character\' move |
|
|
I think someone has the Submission forum, confused with the Tutorial forum.
|
|
|
|
|
|
riveryu
|
Posted: Tue Apr 08, 2008 9:15 pm Post subject: RE:[Tutorial] Making a \'Character\' move |
|
|
I noticed that if you replace the "elsifs" with "ifs and end ifs" then you can move diagonally.
Since with elsif the program stop checking for an arrow button being pressed down after one button has been pressed. With if and end if for all of them, the programs checks all buttons every loop.
|
|
|
|
|
|
BigBear
|
Posted: Tue Apr 08, 2008 9:27 pm Post subject: Re: [Tutorial] Making a 'Character' move |
|
|
Also collision detection or black hole or black box should be any part o the circle hitting any part of the square.
|
|
|
|
|
|
A.J
|
Posted: Tue Apr 08, 2008 9:42 pm Post subject: Re: [Tutorial] Making a 'Character' move |
|
|
try going a step further and adding cool graphics by allowing the user to increase/decrease the size of the moving character.
here's an example:
(NOTE: 'd' for erasing, arrow keys for moving, 'w'/'q' for increasing /decreasing the size of the circle respectively
'a' for filling in the circle with color and lastly, space bar for clearing the screen. have fun!!)
Turing: |
View.Set ("offscreenonly")
var x := maxx div 2
var y := maxy div 2
var r := 5
var keys : array char of boolean
loop
Input.KeyDown (keys )
if keys (KEY_UP_ARROW) then
y + = 1
end if
if keys (KEY_DOWN_ARROW) then
y - = 1
end if
if keys (KEY_LEFT_ARROW) then
x - = 1
end if
if keys (KEY_RIGHT_ARROW) then
x + = 1
end if
if keys ('w') then
r + = 1
end if
if keys ('q') then
r - = 1
end if
if keys (chr (32)) then
cls
end if
var c := Rand.Int (1, 255)
if keys ('d') then
drawoval (x, y, r, r, white)
elsif keys ('a') then
drawfilloval (x, y, r, r, c )
else
drawoval (x, y, r, r, c )
end if
View.Update
delay (5)
end loop
|
Overall, good structure in the mode of explaining the tutorial, but next time try posting it on the tutorials page
|
|
|
|
|
|
BigBear
|
Posted: Tue Apr 08, 2008 10:13 pm Post subject: Re: [Tutorial] Making a 'Character' move |
|
|
Well another to make tutorials interesting are games. Look how much I learned from this tutorial. Turing: | %Name:Dave Angus
%File: moveball1.t
%Date:March 20th 2008
%
%Description: Game that moves ball and has a black hole.
setscreen ("graphics:m256")
setscreen ("offscreenonly")
%Declaration of variables
var score : int := 0
var radius : int
var xpos, ypos : int
var xdir, ydir : int
var key : array char of boolean
var col1 : int
var boxx : int
var boxy : int
var fn : int
var highscore : int
var CheckScore : int
var again : string (1)
%Initialization of variables
radius := 25
xpos := 50
ypos := 50
xdir := 10
ydir := 10
col1 := 5
randint (boxx, 1, maxx - 25)
randint (boxy, 1, maxy - 25)
%
procedure CheckScoreFile (var fn : int) % write a score if none exists. Error occurs if game terminated.
var contents : string
open : fn, "HighScores.txt", get
if fn <= 0 then
close : fn
open : fn, "HighScores.txt", put
put : fn, 0, " - No Name"
else
if not eof (fn ) then
get : fn, contents : *
if contents = "" then
close : fn
open : fn, "HighScores.txt", put
put : fn, 0, " - No Name"
end if
else
close : fn
open : fn, "HighScores.txt", put
put : fn, 0, " - No Name"
end if
end if
close : fn
end CheckScoreFile
%
procedure ViewHighScore (var fn : int)
open : fn, "HighScores.txt", get
if fn <= 0 then
locate (12, 5)
put "No high score set!"
return
end if
var HighScore : string
get : fn, HighScore : *
cls
put "High Score: ", HighScore
var key : string (1)
put skip, "Press a key to continue"
getch (key )
close : fn
end ViewHighScore
%
procedure PutScore (var fn : int, score : int)
open : fn, "HighScores.txt", put
var name : string
locate (22, 5)
put "Enter name: " ..
View.Update
get name : *
put : fn, score, " - ", name
close : fn
end PutScore
%Start of main program.
CheckScoreFile (fn )
loop
loop
%Clear the screen
cls
%Draw the circle to our buffer
drawfilloval (xpos, ypos, radius, radius, col1 )
drawfillbox (boxx, boxy, boxx + 25, boxy + 25, black)
put "Score : ", score
%Key the pressed key from the keyboard
Input.KeyDown (key )
%Moved the ball up until it hit the top of the screen
if key (KEY_UP_ARROW) and ypos < maxy - radius then
ypos := ypos + ydir
end if
%Moves the ball down until it hits the bottom of the screen
if key (KEY_DOWN_ARROW) and ypos > radius then
ypos := ypos - ydir
end if
%moves the ball right until it hits the right of the screen
if key (KEY_RIGHT_ARROW) and xpos < maxx - radius then
xpos := xpos + xdir
end if
%moves the ball left until it hits the left of the screen
if key (KEY_LEFT_ARROW) and xpos > radius then
xpos := xpos - xdir
end if
if xpos > boxx and xpos < boxx + 25 and ypos > boxy and ypos < boxy + 25 then
randint (boxx, 1, maxx - 25)
randint (boxy, 1, maxy - 25)
randint (xpos, 1, maxx - 25)
randint (ypos, 1, maxy - 25)
score + = 1
end if
%Put what's on our buffer to the screen
View.Update
exit when Time.Elapsed >= 50000
end loop
View.Set ("nooffscreenonly")
put "You got ", score, " blackholes!"
open : fn, "HighScores.txt", get
get : fn, CheckScore
close : fn
if score >= CheckScore then
locate (20, 5)
put "New high score!!!"
PutScore (fn, score )
end if
loop
put "Would you like to play again? (y/n) Press h to view highscores"
getch (again )
if again = "h" or again = "H" then
ViewHighScore (fn )
end if
exit when again = "n" or again = "N" or again = "y" or again = "Y"
end loop
exit when again = "n"
end loop
|
Sorry if I modified your code too much. Enjoy.
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
A.J
|
Posted: Tue Apr 08, 2008 10:28 pm Post subject: Re: [Tutorial] Making a 'Character' move |
|
|
er........................BigBear, shouldn't you increase the delay ?
and try making the game better by adding a character and some effects.........something like this perhaps?
Description: |
|
Download |
Filename: |
Snake.zip |
Filesize: |
480 KB |
Downloaded: |
345 Time(s) |
|
|
|
|
|
|
Sean
|
Posted: Wed Apr 09, 2008 6:19 am Post subject: Re: [Tutorial] Making a 'Character' move |
|
|
There is already a thread that deals with Character Movement. It was created by Tony and I found the way he presented it was more effective then the way you did.
This is Tony's Work
And, I don't feel the need for critizing other people, and telling htem they suck.
Quote:
At the bottom of this tutorial I have posted a code of the final project. We are going to work together to come out this that program as the final output.
The reason why it is there is because if you are truely suck and need to reference it it is there for you help.
BUT DONT CHEAT!
This will only hurt yourself!
|
|
|
|
|
|
DaveAngus
|
Posted: Wed Apr 09, 2008 7:02 am Post subject: Re: [Tutorial] Making a 'Character' move |
|
|
Sean @ Wed Apr 09, 2008 6:19 am wrote: There is already a thread that deals with Character Movement. It was created by Tony and I found the way he presented it was more effective then the way you did.
This is Tony's Work
And, I don't feel the need for critizing other people, and telling htem they suck.
Quote:
At the bottom of this tutorial I have posted a code of the final project. We are going to work together to come out this that program as the final output.
The reason why it is there is because if you are truely suck and need to reference it it is there for you help.
BUT DONT CHEAT!
This will only hurt yourself!
Oh geeze man that was a typo.
Man I feel like a n00b!
haha
I meant to type 'STUCK'
put 'STUCK' instead of 'SUCK' and the sentence will make much more sence!!!!!
|
|
|
|
|
|
Sean
|
Posted: Wed Apr 09, 2008 6:56 pm Post subject: Re: [Tutorial] Making a 'Character' move |
|
|
Ah okay, sorry for thinking you were critizing. Before you post, read it over again
|
|
|
|
|
|
DaveAngus
|
Posted: Wed Apr 09, 2008 8:17 pm Post subject: RE:[Tutorial] Making a \'Character\' move |
|
|
haha yea definitely. Im a terrible spell checker/editor. haha. Big weakness of mine!
|
|
|
|
|
|
|
|