PDA

View Full Version : New game from newbe



steinie
08-08-2011, 23:10
Hi, I just did a new game called Gnip Gnop. It's a one player pong game. It will start slow and get faster as the score gos up. It uses the mouse to move the paddle in a canvas window. Give it a try, see if you can get to level 10.:p

matthew
08-08-2011, 23:32
Hi steinie, nice game but I wasn't very good at it. :)

It took me about 160 seconds to get to level 2. ;)

Petr Schreiber
08-08-2011, 23:40
Very nice game!

I must admit I had to use Sleep 10 instead of Sleep 1, as it ran too fast on my PC and I could not react fast enough

I have few ThinBasic specific tips which might be of interest:

Little tip #1: You currently organise all the code in Main function, which you explicitly call on the beginning.
If you remove the function call and rename the function to TBMain, ThinBASIC will run it for you automagically.

So you just change:


'---------------------------------------------------------------
Main ' You have 3 Min to get your best score - use mouse to move paddle
'---------------------------------------------------------------
Function Main () As Long


to


'---------------------------------------------------------------
' You have 3 Min to get your best score - use mouse to move paddle
'---------------------------------------------------------------
Function TBMain () As Long ' TBMain is recognised by ThinBASIC and called first


Little tip #2: To measure the 180 seconds (3 minutes) more precisely, you might use new nice cTimer class, introduced in ThinBASIC 1.8.8.0 (http://www.thinbasic.com/community/showthread.php?11223-thinBasic-Beta-1.8.8).
It is quite easy to set up and use:


Local myTimer As cTimer ' -- Declare
myTimer = New cTimer() ' -- Initialize

...

myTimer.Start ' -- Start measuring

...

myTimer.Elapsed(%cTimer_Seconds) ' -- Returns elapsed seconds


Little tip #3: For testing intervals, such as:

IF x > 2 AND x < 15 THEN ...

ThinBASIC offers nice simplified syntax:

IF Between(x, 2, 15) THEN ...

The source containing the tips is listed here:


' Gnip Gnop By: Jim Steinbrecher 08-08-11

#MINVERSION 1.8.8.0 ' -- We use cTimer introduced in 1.8.8.0

Uses "UI"

'---------------------------------------------------------------
' You have 3 Min to get your best score - use mouse to move paddle
'---------------------------------------------------------------
Function TBMain () As Long

Local Yellow, black, time As String
Local cwh, score, level As Long
Local cx,cy,x1,y1,py,ps As Long
Local x,y,i,xd,yd,xs,ys As Single
Local mousePosition As POINTAPI
Local locX, locY As Long

' -- Create new "object" for time measurements
Local myTimer As cTimer
myTimer = New cTimer()

Yellow = Rgb(255, 255, 0) '---Foreground color
black = Rgb(0,0,0) '---Background color
score = 0
level = 1
x=1 : y=1
xd=2:yd=2
py = 200
ps = 5
xs = 3
ys = 3
xd = 2
yd = 2

MsgBox 0, "Game starts slow, but gets faster as the score gets heigher...."

cwh = Canvas_Window "GNIP GNOP By: Jim Steinbrecher 08-08-11 Use mouse to move the paddle", 300, 200, 640, 480
Canvas_Attach cwh, 0, %TRUE
Canvas_Color Yellow, black '---Foreground and background colors
Canvas_Clear black
Canvas_Redraw

GetAsyncKeyState(-1) '---Reset keys

' -- Start measuring the time
myTimer.Start

While IsWindow(cwh)
DoEvents(On)
Canvas_SetPos(250, 1)

Canvas_Print(" Level " & level & " Score " & score & " Time " & myTimer.ElapsedToString(%CTIMER_SECONDS, "0"))

x = x+xd : If x > 625 Then xd = -xs 'ball
If x<1 Then xd = xs
y = y+yd : If y > 465 Then yd=-ys
If y<1 Then yd = ys
Canvas_Ellipse(x, y, x+15, y+15, Yellow, Yellow)

Win_GetCursorPos(mousePosition) 'Paddle
Win_ScreenToClient(cwh, mousePosition)
Control Get Loc cwh, 0 To locX, locY
mousePosition.x -= locX
mousePosition.y -= locY
py = mousePosition.y
Canvas_Box(2,py,10,py+40,0,Yellow,Yellow)

If Between(x, 2, 15) And Between(y, py, py+40) Then score += 1 : xd = xs : Beep 'Score
level = Int(score/10+1)
xs = level+3 : ys = level+3 : ps = level+6

Sleep 10
canvas_REDRAW
Canvas_Clear black

If GetAsyncKeyState(%VK_ESCAPE) Then Exit While

If myTimer.Elapsed(%CTIMER_SECONDS) >= 180 Then 'Game over = abt 3 Min
MsgBox 0, "times-Up, you have Level " & level & " Score of " & score
Exit While
End If

wend

canvas_WINDOW end

End Function



Petr

ErosOlmi
09-08-2011, 00:43
Just to add a little more info:
[*=1]also YELLOW and BLACK can be avoided to be defined and initiated because they are predefined equates (constants)
but as %YELLOW and %BLACK[*=1]DoEvents can be avoided because WHILE/WEND loops in thinBasic do not kill CPU because they already leave enough time slices to the operating system to perform other operations
steinie thanks a lot for this present.

steinie
09-08-2011, 00:54
Thanks for the hints Petr, I will add them. About the Sleep. The reason I added it was that, when you click the mouse on the Caption, the game will speed-up to about double speed. Any idea why?

zak
09-08-2011, 12:33
thanks steinie for the game, and thanks Petr for the tips especialy the new cTimer class , and the if between. i begins to think about collecting the unique functions of thinbasic which differentiate it from other basics and which have the feature of fun and extraordinary like "if between" and sisters, and to post it. since i am from the GWbasic background i am still using this scheme and forgot the marvelous new features of thinbasic.
to steinie regarding "when you click the mouse on the Caption, the game will speed-up to about double speed." . i have posted before about this phenomena here http://www.thinbasic.com/community/showthread.php?10529-plotting-speed
try Eros example there to see it more evident. the reason are unknown. but when you release the pressing left mouse the plot goes normaly. so there is no permanent problem.
i am using windows xp sp3 , i don't know about the situation in windows 7.