Page 1 of 1

My simple game [SOLVED]

Posted: Mon Apr 22, 2019 3:56 pm
by test
I wanted to share. It works with touch. You may test it in android. The screen is weird on desktop because default window size is 800x600px on pc. If you see bad pratices in the code, tell me please.

Re: My simple game

Posted: Mon Apr 22, 2019 7:25 pm
by Xugro
It took me a while to figure out how the game actually works. For anyone curious: You have to click on the fire/ice symbol in your corner to "charge" up your weapon. Afterwards you can shoot a single fireball/iceball starting from your red square and flying in the direction of your touch. After each shot you have to "recharge" again. Your goal is to hit the enemy red square and not to get hit by the enemy by moving around.
test wrote: Mon Apr 22, 2019 3:56 pmIf you see bad pratices in the code, tell me please.
A few things that I stumbled upon immediately:
  • You do not need to abbreviate variable names. Your game is takes up just a few kilobytes of space. If you write out variable names fully you wont be able to see a big difference. Just write them out fully so that they are easier to understand. As an example: I am still not sure what p1.b and p1.c do or even mean.
  • Do not use copy paste in your code. Every time you write something like player1, player2, player3, ... use an array to store all players and differentiate them on their index - e.g.: player[1], player[2], player[3], ... This way you have to write code only once for a player - e.g. player[n] where n is any number.
    This removes a lot of duplicated code. It will make changes easier (you only need to change the code in one place and not two or more) and it will help you to introduce less bugs (e.g. there is no reset2() function).

Re: My simple game

Posted: Mon Apr 22, 2019 7:32 pm
by test
thank you for your time xugro. can you give an example for your second advice? I mean how to store player1 and player2 in one array. I am new at programming. By the way, p1.b and p1.c is just a mistake. I could just do my job with one variable.

Re: My simple game

Posted: Mon Apr 22, 2019 7:53 pm
by Xugro
I changed your code a little bit to show you what I mean. Take a look at the attached file. Now the code for initializing a player is not duplicated any more and you only need to change it once for both players (line 39 to 56). Only the things that are different are outside the common code. You could do the same with the update and draw code.