Quote: "Why did you set Screen Height() and Width() to variables? Just a preference?"
No, it's just optimization.
The main menu snippet was written with DBP which is quite nippy, but after years of using the slower DBC (and many, many years before that writing software on much slower 8-bit machines), you get into the habit of squeezing the last bit of speed out of them.
Screen Width() and Screen Height() are functions which query the system every time it's used. It's quicker to access variables, so you use the function once and use the variable from then on. Admittedly, the speed gain isn't as much as pre-calculating often used calculations, but the principle is the same.
Using SW and SH also shortens the lines and makes them easier to read.
Quote: "I don't get how you used the scancode()"
ScanCode() simply returns a number when a key is pressed - otherwise it returns 0. Used in this way, the loop does nothing until a key is actually pressed. Without it, the program is continually printing things to the screen. With it, the screen is only updated if a key is pressed, so no flicker.
Normally, you use the format
If condition = something for which the result is
always binary True or False. The result can never be 'nearly' or 'maybe'.
If you leave the '= something' off the end, a true result is implied - DB assumes you are testing for true - in other words, any value other than zero.
So,
If Scancode() just means
If Scancode() = True - or the same as using
If Scancode <> 0.
Quote: "Why do you stack some commands as opposed to 1 per line?"
Personal preference - and a touch of laziness!
Don't you think it's easier to see the whole of a function on the screen at the same time without having to scroll up and down to see the beginning and end?
Quote: "Can you explain what the 'Case' and 'EndCase' do"
Select and Case are a convenient alternative to lots of If..Then statements. In plain English, read it as 'SELECT a variable. In the CASE of it being X, then do X. In the CASE of it being Y then do Y' - and so on. An example:
Say that A can be a number between 1 and 5 and you want to print the current value of A:
A=3
If A=1
Print "A Equals One"
Endif
If A=2
Print "A Equals Two"
Endif
If A=3
Print "A Equals Three"
Endif
If A=4
Print "A Equals Four"
Endif
If A=5
Print "A Equals Five"
Endif
Using Select..Case:
A=3
Select A
Case 1
Print "A Equals One"
EndCase
Case 2
Print "A Equals Two"
EndCase
Case 3
Print "A Equals Three"
EndCase
Case 4
Print "A Equals Four"
EndCase
Case 5
Print "A Equals Five"
EndCase
EndSelect
Not exactly an example which demonstrates Select/Case is any better than If..Endif but shows how it works.
Quote: "You like to multiply the screen height/width to adjust the word positions instead of add/subtract them. Does it matter?"
I don't remember changing anything from your version. To be honest, as they don't change I would rather have used fixed X and Y screen positions for the Text command, but not knowing if you were going to have user-selectable screen resolutions in the future this wasn't possible.
Quote: "The text didn't show up at first because it wasn't in the loop!"
It shouldn't be! Don't change anything as it works fine as it is. If it doesn't for you then the reasons are elsewhere. To start with, are you using the latest upgrade of DBPro?
As I don't have the media, I had to write the main menu procedure as a stand-alone piece of code and it worked perfectly for me. It might be something somewhere else in your program that is causing the problem. I've added another Sync which should work.
Inside the Repeat..Until loop, everything is repeated - including printing to the screen - whether it's needed or not.
My version printed the options to the screen because inside the loop updates are only done when a key is pressed. In your version, your changes simply negate the reason for having the If ScanCode() block.
I did however forget to include the Player Load section at the end of main_menu. That's now done.
Quote: "In the same lines of this problem, did you intend the code to flash white for each button when selected, because it does. How would I make it stay white?"
It only does this because you've moved some of the Text statements inside the loop!
Quote: "Lastly, why is this better than my old one"
Because it works!
Here's your whole program so far. I can't however run it. If you have any further problems, you might want to attach the media to your next post...
` Space Created 4/22/07
` Version 1.02 4/29/07
gosub Setup
gosub Main_Menu
Remstart
Rem No longer required as is now at the end of the Main_Menu procedure
if NumPlayers=1
gosub Single_Player_Load
else
gosub Multiplayer_Load
endif
remend
` Main Loop
Do
GameTime#=timer()-Time
TimerSpeed#=GameTime#*(PlayerSpeed/1000.0)
gosub Player_1_Controls
if NumPlayers = 1
gosub Enemy_Controls
text 30,100,"Score: "+str$(Score)
else
gosub Player_2_Controls
endif
gosub Collisions : if Death=1 then gosub Main_Menu
text 30,20,"FPS: "+str$(screen fps())
Time=timer()
sync
Loop
```````````````````````````````````````
Setup:
` Rem Set the Screem Resolution
set display mode 1024,768,32
` Escape Key Won't Work
disable escapekey
` Setup the Sync + Hide the Mouse
sync on
sync rate 0
hide mouse
` Make Sure the Camera Gets a Backdrop to Prevent HOM Effect
backdrop on
` Make the Background Black
color backdrop rgb(0,0,0)
` Print 'Loading' Text to Center of Screen
set text size 30
ink rgb(255,255,255) , rgb(0,0,0)
sync
center text screen width()/2, screen height()/2, "Loading..."
sync
` Make White Transparent on the Images
set image colorkey 255,255,255
` Load the Images
load image "Player1.bmp" , 1
load image "Player1Bullet.bmp" , 2
load image "Enemy1.bmp" , 3
load image "Enemy1Bullet.bmp" , 4
load image "Enemy2.bmp" , 5
load image "Enemy2Bullet.bmp" , 6
load image "Player2.bmp" , 7
load image "Player2Bullet.bmp" , 8
` CREATE/RESET ALL VARIABLES
` Create Timer Variables for a Reacharge Effect(P1 + P2)
T1=timer()
T2=timer()
` Base Speed, Use for Timer Movement
PlayerSpeed=300
` Set/Reset the Score to 0
Score=0
` Create an Effect Where the Player has to Press Space to Shoot(P1)
SpaceRelease=1
SpacePressed=0
` P2
SRelease=1
SPressed=0
` Reset the Collision Flag
Death=0
` Set Up Max Bullet # and Multiple Bullet Array (P1)
Maxbullets=19
Dim Bullets(MaxBullets)
for T=10 to MaxBullets
Bullets(T)=T
next T
` Set Up Max Bullet # and Multiple Bullet Array (P2)
MaxBullets2=29
Dim Bullets2(MaxBullets2)
for T=20 to MaxBullets2
Bullets2(T)=T
next T
Return
Main_Menu:
Set Text Font "Verdana"
` Goto Game Over if the Game is Over
if Death=1
Gosub Game_Over
endif
` Flag to Exit the Main Menu
ExitMenu=0
` Highlight = 1-3, the Location on the Menu
Highlight=1
SW=Screen Width(): SH=Screen Height()
set text size 72
ink rgb(155,0,0),0: center text SW/2,SH/9,"Space 1.02"
ink rgb(255,0,0),0: center text SW/2-3,SH/9-3,"Space 1.02"
set text size 30
ink rgb(0,0,255),0
center text SW/2,SH*.4,"Single Player"
center text SW/2,SH*.55,"Multiplayer"
center text SW/2,SH*.7,"Exit"
ink rgb(255,255,255),0: center text SW/2-2,SH*.4-2,"Single Player"
Sync: Rem <<<<<< Added this. Hopefully will fix the problem for you
Repeat
If Returnkey()
If Highlight=3
End
Else
ExitMenu=1
Endif
Endif
If ScanCode()
Ink 0,0: Box 0,170,SW,SH
set text size 30: ink rgb(0,0,255),0
center text SW/2,SH*.4,"Single Player"
center text SW/2,SH*.55,"Multiplayer"
center text SW/2,SH*.7,"Exit"
if downkey()=1 Then Inc Highlight: If Highlight=4 Then Highlight=1
if upkey()=1 Then Dec Highlight: If Highlight=0 Then Highlight=3
ink rgb(255,255,255),0
Select Highlight
Case 1
center text SW/2-2,SH*.4-2,"Single Player"
NumPlayers=1
EndCase
Case 2
center text SW/2-2,SH*.55-2,"Multiplayer"
NumPlayers=2
EndCase
Case 3
center text SW/2-2,SH*.7-2,"Exit"
EndCase
EndSelect
Repeat
Until ScanCode()=0
Endif
Sync
Until ExitMenu=1
if NumPlayers=1
gosub Single_Player_Load
else
gosub Multiplayer_Load
endif
Return
Single_Player_Load:
` Position, Scale, and Rotate the Player 1 to the Bottom Center
sprite 1,screen width()/2+30,screen height()-70,1
scale sprite 1,10
rotate sprite 1,-270
` Create Time Variable
Time=timer()
if Death=1
` Create Timer Variables for a Reacharge Effect(P1 + P2)
T1=timer()
T2=timer()
` Base Speed, Use for Timer Movement
PlayerSpeed=300
` Set/Reset the Score to 0
Score=0
` Create an Effect Where the Player has to Press Space to Shoot(P1)
SpaceRelease=1
SpacePressed=0
` P2
SRelease=1
SPressed=0
` Reset the Collision Flag
Death=0
` Set Up Max Bullet # and Multiple Bullet Array (P1)
Maxbullets=19
Dim Bullets(MaxBullets)
for T=10 to MaxBullets
Bullets(T)=T
next T
` Set Up Max Bullet # and Multiple Bullet Array (P2)
MaxBullets2=29
Dim Bullets2(MaxBullets2)
for T=20 to MaxBullets2
Bullets2(T)=T
next T
endif
Return
Multiplayer_Load:
` Position, Scale, and Rotate Player 1 to the Bottom Center
sprite 1,screen width()/2+30,screen height()-70,1
scale sprite 1,10
rotate sprite 1,-270
` Position, Scale, and Rotate Player 2 to the Top Center
sprite 7,screen width()/2-30,70,7
scale sprite 7,10
rotate sprite 7,-90
` Create Time Variable
Time=timer()
if Death=1
` Create Timer Variables for a Reacharge Effect(P1 + P2)
T1=timer()
T2=timer()
` Base Speed, Use for Timer Movement
PlayerSpeed=300
` Set/Reset the Score to 0
Score=0
` Create an Effect Where the Player has to Press Space to Shoot(P1)
SpaceRelease=1
SpacePressed=0
` P2
SRelease=1
SPressed=0
` Reset the Collision Flag
Death=0
` Set Up Max Bullet # and Multiple Bullet Array (P1)
Maxbullets=19
Dim Bullets(MaxBullets)
for T=10 to MaxBullets
Bullets(T)=T
next T
` Set Up Max Bullet # and Multiple Bullet Array (P2)
MaxBullets2=29
Dim Bullets2(MaxBullets2)
for T=20 to MaxBullets2
Bullets2(T)=T
next T
endif
Return
Player_1_Controls:
` Move Left and Right
if leftkey()=1 and sprite x(1)>60
move sprite 1,-TimerSpeed#
endif
if rightkey()=1 and sprite x(1)<screen width()
move sprite 1,TimerSpeed#
endif
` Shoot, Produce Bullet + Set a Charge Time
if SpaceRelease=1
for T=10 to MaxBullets
if spacekey()=1 and (timer()-T1)>0
If sprite exist(T)=0
sprite Bullets(T),sprite x(1)-32,sprite y(1)-25,2
T1=timer()+500
SpacePressed=1
SpaceRelease=0
endif
endif
next T
endif
` Make the Player Press the Space Key
if SpacePressed=1
if spacekey()=0
SpacePressed=0
SpaceRelease=1
endif
endif
` Player 1 Bullet Controls
` Move Bullet(s) Up
for T=10 to MaxBullets
if sprite exist(Bullets(T))=1
move sprite Bullets(T),TimerSpeed#*1.5
endif
next T
` Get Rid of Bullet(s)
for T=10 to MaxBullets
if sprite exist(Bullets(T))=1
if sprite y(Bullets(T))<0-sprite height(Bullets(T))
delete sprite Bullets(T)
endif
endif
next T
Return
Player_2_Controls:
` Move Left and Right
if keystate(30)=1 and sprite x(7)>0
move sprite 7,TimerSpeed#
endif
if keystate(32)=1 and sprite x(7)<screen width()-60
move sprite 7,-TimerSpeed#
endif
` Shoot, Produce Bullet + Set a Charge Time
if SRelease=1
for T=20 to MaxBullets2
if keystate(31)=1 and (timer()-T2)>0
If sprite exist(T)=0
sprite Bullets2(T),sprite x(7)+32,sprite y(7)+10,8
T2=timer()+500
SPressed=1
SRelease=0
endif
endif
next T
endif
` Make the Player Press the Space Key
if SPressed=1
if keystate(31)=0
SPressed=0
SRelease=1
endif
endif
` Player 2 Bullet Controls
` Move Bullet(s) Up
for T=20 to MaxBullets2
if sprite exist(Bullets2(T))=1
move sprite Bullets2(T),-TimerSpeed#*1.5
endif
next T
` Get Rid of Bullet(s)
for T=20 to MaxBullets2
if sprite exist(Bullets2(T))=1
if sprite y(Bullets2(T))>screen height()
delete sprite Bullets2(T)
endif
endif
next T
Return
Enemy_Controls:
` Enemy 1 Controls
` Create Enemy Randomly
if sprite exist(3)=0
Randomize timer()
if rnd(1000)=1
sprite 3,0,0,3
rotate sprite 3,-270
scale sprite 3,20
endif
endif
` Move Enemy Across the Screen
if sprite exist(3)=1
move sprite 3,TimerSpeed#/2
endif
` Get Rid of Enemy
if sprite exist(3)=1
if sprite x(3)>screen width()+sprite height(3)
delete sprite 3
endif
endif
` Enemy Shoot Randomly
if sprite exist(3)=1 and sprite exist(4)=0
if rnd(100)=1
sprite 4,sprite x(3)-sprite width(3),sprite y(3)+sprite height(3)/2,4
endif
endif
` Enemy 1 Bullet Controls
` Move Enemy Bullet(s)
if sprite exist(4)
move sprite 4,-TimerSpeed#*1.5
endif
` Get Rid of Enemy Bullet(s)
if sprite exist(4)=1
if sprite y(4)>screen height()
delete sprite 4
endif
endif
` Enemy 2 Controls
` Create Enemy Randomly
if sprite exist(5)=0 and Score>=300
if rnd(500)=1
sprite 5,screen width()+379,rnd(400)+100,5
rotate sprite 5,-270
scale sprite 5,20
endif
endif
` Move Enemy Across the Screen
if sprite exist(5)=1
move sprite 5,-TimerSpeed#
endif
` Get Rid of Enemy
if sprite exist(5)=1
if sprite x(5)<0
delete sprite 5
endif
endif
` Enemy Shoot Randomly
if sprite exist(5)=1 and sprite exist(6)=0
if rnd(75)=1
sprite 6,sprite x(5)-sprite width(5)/2,sprite y(5)+sprite height(5)/1.5,6
scale sprite 6,20
rotate sprite 6,Rnd(100)+135
endif
endif
` Enemy 2 Bullet Controls
` Move Enemy Bullet(s)
if sprite exist(6)
move sprite 6,TimerSpeed#*2.5
endif
` Get Rid of Enemy Bullet(s)
if sprite exist(6)=1
if sprite y(6)>screen height()
delete sprite 6
endif
endif
Return
Collisions:
` Player Deaths
` If an Enemy 1 Bullet Hits Player 1
if sprite exist(4)=1
if sprite collision(4,1)>0
delete sprite 4
Death=1
endif
endif
` If an Enemy 2 Bullet Hits Player 1
if sprite exist(6)=1
if sprite collision(6,1)>0
delete sprite 6
Death=1
endif
endif
` If a Player 1 Bullet Hits Player 2
for T=10 to MaxBullets
if sprite exist(T)=1 and sprite exist(7)=1
if sprite collision(T,7)>0
delete sprite T
Winner$="Player 1"
Death=1
endif
endif
next T
` If a Player 2 Bullet Hits Player 1
for T=20 to MaxBullets2
if sprite exist(T)=1
if sprite collision(T,1)>0
delete sprite T
Winner$="Player 2"
Death=1
endif
endif
next T
` Enemy Deaths
` If a Bullet Hits Enemy 1
for T=10 to MaxBullets
if sprite exist(T)=1 and sprite exist(3)=1
if sprite collision(T,3)>0
delete sprite T
delete sprite 3
Inc Score,100
endif
endif
next T
` If a Bullet Hits Enemy 2
for T=10 to MaxBullets
if sprite exist(T)=1 and sprite exist(5)=1
if sprite collision(T,5)>0
delete sprite T
delete sprite 5
Inc Score,200
endif
endif
next T
Return
Game_Over:
` Red Ink For Game Over Screen
ink rgb(255,0,0) , 0
` Print Game Over with Accomodations
Repeat
set text size 72
center text screen width()/2,screen height()/2-150,"Game Over."
set text size 50
if NumPlayers=2
center text screen width()/2,screen height()/2,Winner$+" Wins!!!"
center text screen width()/2,screen height()/2+100,"Press ENTER to replay and ESC to quit."
else
center text screen width()/2,screen height()/2,"Your Score Is: " + str$(Score)
center text screen width()/2,screen height()/2+100,"Press ENTER to replay and ESC to quit."
endif
sync
Until returnkey()=1
` Enter to Replay, Delete Sprites + CLS
for SpriteDelete=1 to 30
if sprite exist(SpriteDelete)=1
delete sprite SpriteDelete
endif
next SpriteDelete
CLS
Return
``````
``````
End```
``````
``````
TDK_Man