I am currently creating a game from the 80s (1978), Space Invaders ("older" people know this of sure 😁).
The first steps have been taken, and some functionality is already in place.
One problem is pressing the left and right keys for moving the defender.
When pressed individually, everything works fine.
When the key is held down, it takes too long for the repeat function to kick in. So the Aliens
shoot me down very often because of this delay.
What I tried without success:
if the key is pressed (left or right in this case) the _keypress() sub is called.
With a public var I set to true in this case I call the moveit sub.
In there I check if the var is true or false.
If true it repeats moving, till the var is set to false in the _keyrelease() event.
Somehow the hole app freezes… ( maybe because of endless calls to _keypress() )
Has anyone an idea to get this running?
I don't want to change the system accessibility values if possible (or If we have to we set the values
back to the default values at the end of the game)
Also what I never encountered before:
I gave the form background color to black, so the menu also gets black, I just did a foreground colour
for better sight, any chance to set the background colour of the menu independently?
Image
Invader.jpg
And the last one:
in the keypress sub the key.code for ctrl is not recognised, so I used the key.code I got as a number,
ctrl is for the shooting, (Documentation: NEVER use the key values directly, as they change between GUI components. Always use these constants! )
I am currently creating a game from the 80s (1978), Space Invaders ("older" people know this of sure 😁).
Played that a lot when I was young. Great game. Been thinking often to create this myself, but just too busy with other stuff.
Good to see soemone else on it
Yogi said
One problem is pressing the left and right keys for moving the defender.
When pressed individually, everything works fine.
When the key is held down, it takes too long for the repeat function to kick in. So the Aliens
shoot me down very often because of this delay.
Change the delay on your system should fix that
Yogi said
I don't want to change the system accessibility values if possible (or If we have to we set the values<br />
back to the default values at the end of the game)
Ok, so change the delay when starting the game from within the game and set it back when closing the game, from within the game.
You will need to dive into where this configuration info is saved and modify it on the fly with some code in your game..
So, some research into how your distro takes care of that will be required and where all this info is stored.
As this is user based, you should not require elevated rights to change this from code.
Yogi said
Also what I never encountered before:
I gave the form background color to black, so the menu also gets black, I just did a foreground colour
for better sight, any chance to set the background colour of the menu independently?
]
These things (customise colors) are all very toolkit related and a pain in the ass. There is no one good solutions.
Did you try setting the form arrangement to fill and add a panel set to expand with a black background and use that as form background instead. That should not interfere with the form menu.
Edit: With that I mean. Select ALL controls on the form. CTRL + X (Cut), draw a Panel on the form, select the Panel and CTLR + V (Paste) all controls into the Panel.
Clear property Background on the form. Set form property Arrangement to Fill and Panel property Expand to True. Change Panel Background to black.
Run and see what happens…
Yogi said
And the last one:
in the keypress sub the key.code for ctrl is not recognised, so I used the key.code I got as a number,
ctrl is for the shooting, (Documentation: NEVER use the key values directly, as they change between GUI components. Always use these constants! )
One problem is pressing the left and right keys for moving the defender. When pressed individually, everything works fine. When the key is held down, it takes too long for the repeat function to kick in. So the Aliens shoot me down very often because of this delay.
Can you post some code so we can see what's happening. Or all of it, it looks cool!
but for the moment other things are more important - but, if the delay is less everything is working well. (so you have been right, it solves the problem!)
Regarding the Container, thanks for the detailed description, maybe it is also possible to select all, right click and select embed in a container.
The freezing thing is obsolet now, because the delay if correctly set does the trick.
Here is the current source, after giving a container to the form the keypress events are no longer working.
Public Sub Form_KeyPress()
and then I added an event to the panel where all the other objects are in, also not working!
Public Sub PanelBack_KeyPress()
No keypress… 🤮
If you are in the same line as the defender, left mouse click is shooting, right mouse click moving, but only in the same line as the defender!
It is not the copy of the original even if the invaders should be similar. The shooting defense allows up to 3 shots visible on the screen, that is much more than the original, and the buildings are bombproof 😁
Here I went back to the previous version without container, just the main form.
I don't know if this happens to you too, sometimes in the beginning the keypresses are not recognized - even the ESC key. But after a while shooting with the left mouse button, it works again 🤷♀️
In this game I used 4 timers, I thought it would be more fluent if you have more independent "Threads".
Actual I'm working with 2 timers, calling different subs. Seems to work also fluent.
But the main problem stays, at the moment I can neither fire nor move till the 1st stage cleared,
no keypress event fires.
After that, it works again. Frustrating not to see the tree in the wood….
Any help is greatly appreciated.
In the virtualbox with GambOS Gambas 3.19.6 I tried the project, but it shows this:
So, I nailed it somehow down, deactivated the timers, still no keypress detected, debugged with F8 - and see, it is working from the beginning after last command awaiting keypress and mouse (timers still deactivated!).
After form_open() the keypress event is working. If pressing the button "Start Game" no keyevent is triggered.
if I make a breakpoint F9 at the last END command after creating the scene and press F5 to continue, it works! Repeatedly tested!
I even did a PRINT for the console to see when a keypress is triggered, it works only as described above.
You have 3.21.3 bytecode (your source).
So, on 3.19.6 you FIRST DO COMPILE ALL.
That will give you the 3.19.6 bytecode and it should now work fine.
It runs on my 3.19.6 version (same as the one on GambOS).
And when it come to these kind of problems, the order in wich things trigger can be of influence, what has focus can be of influence, a lot can be of influence.
The debugger is not helpful for this, but the Debug instruction could be (a print to console with location in code included). See here on the wiki
To track stuff that makes no sense, I sometimes place an instruction like, Debug "Name of event" at the start of each events (replace Name of event with the event name you placed the Debug code in).
Next, I run the code to see in what order things happen and managed to solve issues by simply moving code to an earlier or later event, because the code was fine, the moment it got seen and triggered wasn't.
You have 3.21.3 bytecode (your source).
So, on 3.19.6 you FIRST DO COMPILE ALL.
That will give you the 3.19.6 bytecode and it should now work fine.
It runs on my 3.19.6 version (same as the one on GambOS).
I'm struggling with the special effects, sound especially. It becomes not fluent the more sound I put in the game via Mediaplayer. No matter what I've tried, with more timers, etc., it doesn't run smoothly as soon as I play sound files. If anyone has any ideas, let me know.
There are 2 versions in this source, FMain1 and FMain2, if you try both as starting class you know what I mean.
I think the timers might be causing delays Adding more won't help.
They might also interfere with you key presses.
Have you tried my Debug suggestion as it will reveal to you how often and in what order all is triggered and will illuminate your comprehension of what is really going on.
You will be surprised of what is going on, make sure to place them on all events.
gbWilly Dear Willy, Always a pleasure for me to read your suggestions, they help in going deeper into Gambas.
Yes I did the debug suggestion, but not that much as it have should. From this early morning till now I did a deeper research with the Debug and what I learned about the timers, wow, they are really precise. I worked all the subs with the timers, and what I could do because of the timers - I removed some. For most purposes it should be enough to have one timer and from the sub event of this timer call the other subs needed.
So what I could find out, the timers are ok, also my subs hopefully ok - I couldn't find anything in the source delaying much so that it is not fluent. Also one thing what disturbed me was the ctrl key, if pressed once, it triggered mostly twice in the _KeyPress() sub
So it came to the next step, I changed the Project from gb.gui to gb.qt5 - and - tatah the hole thing works like a charm - including no double trigger from the ctrl key. Just seeing that I have to adjust the graphical elements.
What is your suggestion regarding the GUI component for use?
I have looked at FMain2 > Timersnds. I can't see why it is there. If you want the sound, just place the code in the main routine. Or am I missing something?
The routine below has an Integer created, but it only counts to 20, a byte would be faster.
Code
Public Sub createAlien(ByRef aPict As PictureBox[], oPanel As Panel, oPict As Picture) Dim i As Integer
The same here as i only counts to 4
Code
Public Sub createBase() Dim i, x As Short
You could also consider using Task, which will allow the code to run in its own thread.
My suggestion is use the component that works best for you under your circumstances.
I use qt5 as stuff just works better even though I run a gtk based desktop (Mate).
In upcoming Gambas Book 2, chapter 1 write on the matter of GUI components, toolkits, x11 and Wayland.
A little sneak preview in screenshot below on what I advise on GUI to use.
It's important background info to be aware off, as I see a lot of problems posted here are related to either one of these (GUI/Toolkit and X11/Wayland) and can be easily prevented when you know what you are dealing with.
Book 2 is in testing now, so a little patience is needed to read it all, or maybe apply to become part of the contributor team and become a guide tester as well , so you have early access to the Book for testing purposes (meaning study it, try it and report issues etc)
Fun stuff, using Debug, to see how all is going under the hood, often very insightful. Also a tip from Book 2
I have looked at FMain2 > Timersnds. I can't see why it is there. If you want the sound, just place the code in the main routine. Or am I missing something?
The routine below has an Integer created, but it only counts to 20, a byte would be faster.
Code
Public Sub createAlien(ByRef aPict As PictureBox[], oPanel As Panel, oPict As Picture) Dim i As Integer
The same here as i only counts to 4
Code
Public Sub createBase() Dim i, x As Short
You could also consider using Task, which will allow the code to run in its own thread.
That's pretty much the same approach I take when someone asks me about Yoga. There isn't just one right way; people are different and everyone has to find their own path - but you can help a little.