Need help with a game

Hello everyone!

How are you?

I’m finishing rebuilding a game from the 80s, FatCat, originally programmed by Randy Salo in 1981 at the request of HP.
(Still awaiting formal authorization to publish…)

I’m in the final stages, and I’m racking my brains to solve a problem with the “Mouse” control.

At first, I started using KeyDown and soon realized (and was told) that I was going the wrong way.

// wdwGame → keyDown Event

Select Case key.Asc
Case 30 
c_game.myMouse.Move("UP", c_game)
Case 29 
c_game.myMouse.Move("RIGHT", c_game)
Case 31 
c_game.myMouse.Move("DOWN", c_game)
Case 28 
c_game.myMouse.Move("LEFT", c_game)
End Select

The correct option was to use Keyboard.AsyncKeyDown, so I can control the speed of the “Mouse” movement

// inside a timer, every 100ms
// and of course, i can speed up or down, just changing the perio

If Keyboard.AsyncKeyDown(getKeyCode("Arrow Up")) Then
c_game.myMouse.Move("UP", c_game) 
ElseIf Keyboard.AsyncKeyDown(getKeyCode("Arrow Right")) Then
c_game.myMouse.Move("RIGHT", c_game)
ElseIf Keyboard.AsyncKeyDown(getKeyCode("Arrow Down")) Then
c_game.myMouse.Move("DOWN", c_game)
ElseIf Keyboard.AsyncKeyDown(getKeyCode("Arrow Left")) Then
c_game.myMouse.Move("LEFT", c_game)
End If

The problem I’m having, and I can’t solve, is that I feel like I’m mistiming the “Mouse” direction. Sometimes it changes before, sometimes after… I can’t quite explain it!

So here’s a copy of the game, only one level, compiled for Mac.
https://fatcat.alemac.com.br/FatCatDemo.zip

And for windows:
https://fatcat.alemac.com.br/FatCatDemoWin.zip
(still need to create an installer exe)

If anyone with gaming experience could give it a try and shed some light on it, I’d be immensely grateful.

Alex

You’re going to have to find a way to explain what your concern is before anyone could help you…

I think the issue you are having is that you can press the key down and release it before the next timer has fired. Maybe keep the key down event logic in there and keep track of the last key down event that can be consumed on the next timer event.

1 Like

Short answer: That CANT be done with timers.

For the long answer, try to google something like: How to create a Game Loop

Side note:

It seems you like coding very very very simple games. IMO Xojo isn’t really suited for doing this (in fact you shouldn’t even thinking using Xojo for games at all).

Take a look at Imagi (‎imagi - fun coding game on the App Store)
Incredible easy to code (Python) and has tons of APIs especially for creating games.
I ones created a Galaga clone with it that was really close to the original.

Just my 2C

I’ve done Snake in Xojo. In high speed mode the snake has to react to your key immediately otherwise the games sucks. I did everything around movement in the Timer.Run event and I have never run into timing issues with AsyncKeyDown. The key is then passed to the Snake object and in the game loop the direction of the snake is changed. Works like a charm.

Have you used the Profiler to see if the what is influencing the timing?

1 Like

i would change this into a boolean for all 4 directions

c_game.myMouse.MoveUp= Keyboard.AsyncKeyDown(getKeyCode("Arrow Up")) 

100ms read 10x per second the status, i remember the interrupt for input at amiga was 50 times per second.

maybe use a timer for input and movement (update) and one separate for the drawing.

i tested your demo.
if you would use a joy stick your get the inout up and left at the same time, the mouse would goto left until she can goto upwards.

you can map the input and overwrite with possible movements,

Slow simple games like tetris can be created in Xojo in a Canvas. Xojo is not designed for performance gaming and has issues with Game Loops (see Testing Xojo Game Loops). Slow updates to the Canvas can be achieved with Xojo with poor-to-moderate refresh rates and key-mouse updates. This is a good way to learn basic graphic techniques, and I have written a book about Canvas control with Xojo at Canvas Control 3 with Xojo Desktop.

Moderate speed games like old pixelated games such as DOOM can be created with embedded windows (forms) in Xojo. The closer that dependency is used with Xojo, the slower the game.

High speed games that are dependent on GPU require a separate window (forms), and a graphics language such as OpenGL or Vulkan. I have written a book with plugins with Windows and how to program the upgraded OpenGL 2.1 at OpenGL 2.1 64-bit for Windows.

My suggestions is to start with Canvas in Xojo to better-understand gaming graphics and learn the basics of gameplay. Then work your way up to GPU’s and graphics programming.

Happy programming :blush:

1 Like

ahhhh… ok. i’ll think about it.

maybe the 100ms refresh does not sync with the snacks the mouse is eating…
hummm… and the distance… hummm… maybe!!

i need the timers, and indeed it the best choice, because i can change the speed of the mouse movement.

and if i don’t use timer, i’m slave of user’s keyboard repeat delay settings on operating system.

ok. great. i’ll take a look. thank you.

snake… that snake? 2 snakes blocking each other?
is it available for download?

the logic is the same i’m using.

sorry, i don’t know what “profiler” is…

this way?

c_game.myMouse.MoveU = Keyboard.AsyncKeyDown(getKeyCode("U"))
c_game.myMouse.MoveR = Keyboard.AsyncKeyDown(getKeyCode("R"))
c_game.myMouse.MoveD = Keyboard.AsyncKeyDown(getKeyCode("D"))
c_game.myMouse.MoveL = Keyboard.AsyncKeyDown(getKeyCode("L"))

why? to be more efficient?

i use more than one timer. one only to refresh the canvas (60ms)
other for the mouse (100ms)
other for the cat (level 1 → 300ms)… (level 31 → 250ms)
other for “vanish” effect

this 4 elseifs i did, it’s because before was “ifs” and the mouse could move diagonally, and i don’t want.

you can map the input and overwrite with possible movements

this is an excelent idea. because the player “never” will enter left, if there are a wall there…

i need to think!

ual!

Don’t misunderstand (because of my English)
I feel like I’m in kindergarten, trying to get the bottle… lol

Really, for someone to write two books (at least), they certainly have a lot of baggage.

Thank you so much for the explanation. All information is valid and useful, even if for me, at this moment and for this game, it’s “too much information.”

But i’ll keep this in mind. We never know…

1 Like

When you wrote mouse, you mean the Mouse Cursor (arrow, usually) ?

c_game.myMouse.MoveUp = Keyboard.AsyncKeyDown(getKeyCode("Arrow Up"))
c_game.myMouse.MoveRight = Keyboard.AsyncKeyDown(getKeyCode("Arrow Right"))
c_game.myMouse.MoveDown = Keyboard.AsyncKeyDown(getKeyCode("Arrow Down"))
c_game.myMouse.MoveLeft = Keyboard.AsyncKeyDown(getKeyCode("Arrow Left"))

as example if the player hold up and left, the mouse will goto left until a intersection she goto up.
this slide will feel naturally and not hang at the edges.

your if then else is not good because you can only press one key, the others are ignored in this cycle.

Xojo menu>Project>Profile Code. Then run your code in the debugger. Do the things you want and exit the game. In the treeview left will be the outcome of the profiler. You can see the cost in milliseconds for methods/loops etc.

Check whether the mouse is already going in the direction the user wants. So if the mouse goes UP… ignore the key that moves UP etc. So with every change of direction you disable the key for that direction unless a new direction is selected (key).

it is a cat catch mouse game, i guess myMouse is the player

1 Like

Games are not my cup of tea.

Alexander always use “mouse”, not the cursor or anything else; what if the user press the TouchPad ?
He use also different words for many things… :wink: