WAZ MADE A GAME!
Blitz3D Forums/Blitz3D Beginners Area/WAZ MADE A GAME!
| ||
I made a game!! And it was from all my head of what I learnd today and over past few days. I so excited, to you lot its probley the sillyest game ever but to me I almost in tears when I saw it working! I got a buzz so high words can't exaplin. My game basicly called "DICE ROLL" It stats of stating what the computer rolled and its random of 1 to 6. Now it then asks player to press space to roll dice and it gives hima random number. Who ever has highest number wins and its states that HERES MY CODE is it good or what! ;################################## ;Warrens First Game ## ;Dice Roll (Press escape to quit)## ;################################## ; select example AppTitle "Dice Roll" .start computer% = Rand(1,6) Select computer% Case 1 Print "The computer rolled a 1" Case 2 Print "The computer rolled a 2" Case 3 Print "The computer rolled a 3" Case 4 Print "The computer rolled a 4" Case 5 Print "The computer rolled a 5" Case 6 Print "The computer rolled a 6" End Select Print "Please press space to roll the dice!" While Not KeyHit(1) While KeyHit(57) player = Rand(1,6) Print "You rolled a "+player+" If player < computer% Then Print "Computer WINS!" Else If player > computer% Then Print "You WON!" EndIf EndIf Goto start Wend Wend |
| ||
Obvisouly my code is laid out better example the wends line up with the WHILS so I know which is which and the endif line with IFS. I did all that due to the great help i got here. It just makes the code in 1 line as I copy and pasted from blitz, I not sure how to get it from blitz to these forums intact. What u rekon! I am well proud! Now I gotta try add it so it keeps the scores and prints em.. thats gonna be hard LOL |
| ||
I'm impressed... A very simple game... Since I need to install my Blitz on my new PC I cannot test it out yet, but as far as I can read the code the game should work... Way to go, Waz... |
| ||
I gotta yet to make it so the screen can update with the new stuff as at the moment it just basicly goes on and on and its diffcault to keep track with all the txt on screen and no spaces. |
| ||
I just added another line which basicly is another IF, THEN. If the numbers DRAW then it will print "ITS A DRAW!" I upgrading it bit but bit and soon I be selling it. HA HA |
| ||
Sure thing... Putting things in good lay-out is an important part of game developping... I've one kind of criticism, when I look in your code... The computer will always play with the same number rolled from the dice which will only be shown once... Do you see why that is? |
| ||
No sorry.. But u also confused me. Do you mean the computer will roll the same number over and over? if so.. it does not seem to be doing that... Ahh if umean the computer will start with the same number at the very beginning all the time then yea that happens. Some reason when it first starts its always a 3... How I change that to be random it should happen with the rand i already got? |
| ||
No sorry.. But u also confused me. Do you mean the computer will roll the same number over and over? if so.. it does not seem to be doing that... |
| ||
I see what u mean its basicly the same each time. Only just noticed after my excitement LOL. hmm Whats the work around this. |
| ||
Tric is right. That's because computers have difficulty generating real random numbers. Everytime you restart your game (by pressing f5, or RUN), the random numbers generated will be in the same order as the previous time, unless you add this line (only need to do it once at the start): SeedRnd(MilliSecs()) Good first game! Keep going! |
| ||
Congrats! Lots of people never get as far as you did with this game. Don't feel bad about it being simple either, you managed something many people never do - you choose a goal and you completed it. |
| ||
Whats the seedrand(millisecs()) actully do? Just tells computer to generate random proper numbers? |
| ||
4 minor points: 1) you can just use computer rather than computer% 2) Select Case is not as appropriate here, but it is fine too. An easier way is simply: Print "The computer rolled a " + computer% 3) Using a Goto statement is not a good way of getting out of the While Wend loops. 4) You left out an ending " in the line: Print "You rolled a "+player+" |
| ||
Thanks dynaman that means a lot to me! P.S ROB that worked a treat, what did that 1 line actully mean? I read the help on bb but it kinda explains tricky, it basicl sets the random generator or something with system clock..? hehe |
| ||
Thanks dynaman that means a lot to me! P.S ROB that worked a treat, what did that 1 line actully mean? |
| ||
The randomizer was not the point in my post... but I didn't see that "Goto Start"... I'll explain my point later, as I have to go now... |
| ||
>Whats the seedrand(millisecs()) actully do? >Just tells computer to generate random proper numbers? In short, "Yes"! The long explanation is that computers are so logical that they have extreme difficulty generating random numbers. So, pseudo-random numbers are generated instead. These are random numbers that are either predefined, or are mathematically generated, but they depend on an initial "seed". When you run your program, the seed is always the same, unless you tell the computer to get the seed from somewhere else; in this case, the system time (look up the keyword SeedRnd). That's what that line does. Actually, it might need to be SeedRnd rather than SeedRand! |
| ||
Lost. I kinda lost now, What should I move onto now and learn? Read into Types a bit more? urm.. Read more about loops? I lost my brain somewhere LOL I tihnk I deserve a break to go play a game actully. ehehe |
| ||
Learn types as they're the most powerful thing you'll learn, but take a break as it might take a bit of head-scratching... ;) That game is very well laid out -- plenty of people have real trouble getting that far. BTW Post your code between {code} and {/code} tags (but replace { and } with [ and ] so... {code} Blah blah blah... {/code} ... becomes... Blah blah blah... |
| ||
THANKS DUDE! Is this better folks: ; select example AppTitle "Dice Roll" .start SeedRnd(MilliSecs()) ; Sets the random generator to get real random numbers when program is ran computer% = Rand(1,6) Select computer% Case 1 Print "The computer rolled a 1" Case 2 Print "The computer rolled a 2" Case 3 Print "The computer rolled a 3" Case 4 Print "The computer rolled a 4" Case 5 Print "The computer rolled a 5" Case 6 Print "The computer rolled a 6" End Select Print "Please press space to roll the dice!" While Not KeyHit(1) While KeyHit(57) player = Rand(1,6) Print "You rolled a "+player+" If player < computer% Then Print "Computer WINS!" Else If player > computer% Then Print "You WON!" Else If player = computer% Then Print "ITS A DRAW!" EndIf EndIf EndIf Goto start Wend Wend |
| ||
Great Waz - very inspirational! You use good programming technique. Concise and well-laid out. It is clear and easy to understand especially with your ;remarks I still do not order my code 'properly' in this way, and maybe I'll never get around to finishing anything!!! All the best! |
| ||
When you run your program, the seed is always the same, unless you tell the computer to get the seed from somewhere else; in this case, the system time which is always changing (time never stops) so the seed you get is always different. Thought I would better clarify that... |
| ||
Congratulations Waz! Well done. |
| ||
congrats, i would defanately learn types, they are extremely powerfull |
| ||
I'm not sure if this is better than my first game or not. :-) My first game displayed a grid, and you had to choose the square which had an X under it. :-) That was wayyy back when I was using the Ti99/4a, back in the christmas of 1981 or 82 I think. Hey woz, here's a way you can make your code better: You can replace all this: computer% = Rand(1,6) Select computer% Case 1 Print "The computer rolled a 1" Case 2 Print "The computer rolled a 2" Case 3 Print "The computer rolled a 3" Case 4 Print "The computer rolled a 4" Case 5 Print "The computer rolled a 5" Case 6 Print "The computer rolled a 6" End Select With this: computer% = Rand(1,6) Print "The computer rolled a " + Str$(computer%) + "." Also, you should replace: While KeyHit(57) Wend With this: If KeyHit(57) EndIf Your version works just fine, but if you think about what the code is "saying", then the second one makes more sense and is easier to understand. Yours says "While the key has been hit". Mine says "If the key has been hit". A while loop is usually used when the condition check is something that MIGHT remain true after going through the loop once. But you should only check keyhit() for a particular key once per game loop. And your While Not KeyHit(1) loop is your main game loop. Just cause, that's how things are done. :-) Oh, and you should not have a goto in there. You should put EVERYTHING inside your while not keyhit(1) loop. To do that, make a new variable called "ComputerRoll". Set it to TRUE before you enter the game loop, and then, check to see if it is true. If it is, roll the computer die, and print your text. And then set it to false. Then the computer will roll the die once, until you set computerroll to true again. You do not need to seed the random number generator in your main game loop btw, just once when your program starts. And the last thing you need to do to get rid of the goto, is replace goto start with "ComputerRoll = True" to tell the computer to roll it's die again. And that's all the suggestions I have for now. :-) |
| ||
I'll make a few alternings in your code, if you don't mind First, try to prevent the use of the "Goto" command as much as possible... It's a forbidden command... A good programmer never uses it, and in this program it's not really needed... Plus, as I said, the program will loop until you press ESCAPE... However the loop starts after the computer rolls his dice (look good at the code)... You fixed that up with a Goto, but you could fix that up on this way, replacing the "While" commands by Ifs... Like this ;################################## ;Warrens First Game ## ;Dice Roll (Press escape to quit)## ;################################## ; select example AppTitle "Dice Roll" .start computer% = Rand(1,6) Select computer% Case 1 Print "The computer rolled a 1" Case 2 Print "The computer rolled a 2" Case 3 Print "The computer rolled a 3" Case 4 Print "The computer rolled a 4" Case 5 Print "The computer rolled a 5" Case 6 Print "The computer rolled a 6" End Select Print "Please press space to roll the dice!" If Not KeyHit(1) If KeyHit(57) player = Rand(1,6) Print "You rolled a "+player+" If player < computer% Then Print "Computer WINS!" Else If player > computer% Then Print "You WON!" EndIf EndIf Goto start End If ENd If Okay, now I´m going to take you into a simple more profesional way of programming, which is a lot neater... ;################################## ;Warrens First Game ## ;Dice Roll (Press escape to quit)## ;################################## ; select example AppTitle "Dice Roll" Repeat ; Start a repeat loop in stead of start computer% = Rand(1,6) Select computer% Case 1 Print "The computer rolled a 1" Case 2 Print "The computer rolled a 2" Case 3 Print "The computer rolled a 3" Case 4 Print "The computer rolled a 4" Case 5 Print "The computer rolled a 5" Case 6 Print "The computer rolled a 6" End Select Print "Please press space to roll the dice!" Repeat Until KeyHit(1) or KeyHit(57) ; Waits until the desired keys are pressed If KeyHit(57) player = Rand(1,6) Print "You rolled a "+player+" If player < computer% Then Print "Computer WINS!" Else If player > computer% Then Print "You WON!" EndIf EndIf Until KeyHit(1) This code may have required more typing but will serve a lot better when you start to code really complicated things and can prevent turning complex way of coding turning into chaos... Try experimenting with the "Repeat" & "Until" commands... CAn you see what those commands do? |
| ||
his code is fine they both acomplish the same thing, save for the goto. it is NOT good in advanced programing to get stuck in a loop _JUST_ to wait for a keypress. Granted his code isnt perfect while instead of if it is better than your example, whiles are much more profesional than repeat until... |
| ||
It was in his code the only way for me to let things wait, since it uses the Print method... In normal coding I wouldn't have used that loop, trust me... In fact, in this case I wouldn't even use "KeyHit"... But I left it in, in order to make things not too confusing all at once... I'm just noting the forbiddeness of Gotos since they tend to turn a program into spaghetti... |
| ||
Yes, SSS is right. You don't want to halt the program execution just to wait for the user to press a specific key. I wrote up a version of the code that implements all the things I consider hallmarks of professionalyl written code. Hopefully this will give you a jump start on writing clean code and more complex programs. :-) AppTitle "Dice Roll" SeedRnd MilliSecs() NewRoll = True Repeat If NewRoll = True Computer_Die = Rand(1, 6) Print "The computer rolled " + Str$(Computer_Die) + "." Print "Press space to roll your die!" NewRoll = False EndIf If KeyHit(57) Player_Die = Rand(1, 6) Print "You rolled a " + Str$(Player_Die) + "." If Player_Die < Computer_Die Print "Computer WINS!" Else If Player_Die > Computer_Die Print "You WON!" Else Print "It's a DRAW!" EndIf EndIf Print "" NewRoll = True EndIf Until KeyHit(1) |
| ||
sswift, you dont need to do Str$(variable) to get this to work if you remove the str part it will still work, |
| ||
THat's also a way to cover it (I even discovered a few bugs in my code, I normally check all my codes before I post them, but since I still need to re-install my copy of Blitz, I couldn't do it) |
| ||
That first print statement is supposed to be indented with the one below it btw, that's just the forum being naughty. :-) |
| ||
" sswift, you dont need to do Str$(variable) to get this to work if you remove the str part it will still work," I know that. But that is in Blitz. In almost any other language, that will not work, and even if it does, is confusing. So I explicitly convert the integer to a string. There's a reason Mark included the Str$() command. :-) Of course, he didn't include the inverse, Val() which makes no sense to me. I want Val! :-) Inline val, that doesn't close my code down that I can just put in there to feel good about myself. :-) |
| ||
In case you're confused about what the code is doing up there that I wrote... When the main game loop begins, newroll is true. If newroll is true, the computer rolls it's die, and sets newroll to false. As long as newroll is false, the loop loops forever, checking to see if the player has pressed space. As soon as they press space, the two dice are evaluated, the answer returned, and newroll is set to true so that the next tiem through the loop the computer will roll again. |
| ||
wow wow wow. Everythi8ngs way other my head.... I not read up repeat, until etc but I am actully on LOOPS part and repeat and until in my BOOK so I read about em. ALot stuff confusing me now I made a game and ur all jelous so u takemy copyright and edit it. HA HA joke :) Thanks for so much intrest btw its heart warming. I think the diffacult thing is that theres so many ways doing things and knowing which is the best way to do it.. know what I mean? Thanks guys. Luv Waz |
| ||
Waz, there are certainly a lot of ways of doing things, and even the experts differ, so you don't need to worry too much about this. Bottom line is to get the program to work, to do what you wanted it to do. Some ways are faster, some ways are more elegant, some ways might be okay for this game but not for a more complicated game, etc. Take your pick. |
| ||
Well, congratulations are in order. Reading through your numerous other posts it seems like you're making some real progress. |
| ||
First, try to prevent the use of the "Goto" command as much as possible... It's a forbidden command... A good programmer never uses it, and in this program it's not really needed... The first and last of those statements are absolutly correct. The middle two are not ;) Good job Waz! |
| ||
Hmmm.. Why doesn't this program work on my machine at all? It starts, but when I press space, nothing happens.. It does not help to compile it, either.. Also, the window looks like a DOS box - is that right? Any theories? |
| ||
When I added the line Graphics 800,600 the program KIND OF worked for me.. The new graphics window appeared AND the DOS-BOX like window appeared with the text in it. When I activate the DOS-BOX windows (clicking inside it) if doesn't work, but when I activate the graphics window, it works when I press space (the program continues in the DOS-BOX window).. Is that weird, or normal..?? |
| ||
bachfire, what version of blitz are you using? since it wont work properly with blitz+ since it doesn't "print" to the current canvas only a dos box, but you need a screen to get input. |
| ||
Allright, so that's maybe it. I use BlitzPlus. As long as what happens is normal and makes sense, in my end, then I'm happy. |
| ||
kwl tis wuld be good to be used to imtate dice roing in ganes you could have come soon |