Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers AppGameKit Corner / Random placement

Author
Message
rneilg
4
Years of Service
User Offline
Joined: 18th Mar 2015
Location:
Posted: 23rd Mar 2015 14:02
Hi everyone, I am trying to place an aobject randomly at one of 3 points. I am trying to make a whack-a-mole style game.
If the mouse pointer hits the character I want him to appaer in one of 3 places. Can anyone help with my code please...

[setvirtualresolution(1024,768)

`LOAD IMAGES AND SOUNDS FOR THE GAME
loadimage(1,"goodguy.png")
loadimage (8, "crosshair.png")
loadsound (4, "phaser.wav")

`SETUP GAME VARIABLES
GoodGuy = 1
GoodGuyXpos = 100
GoodGuyYpos = 550

crosshair = 8
crosshairxpos = 512
crosshairypos = 360

score = 0

`CREATE GAMESPRITES
createsprite(goodguy, 1)
createsprite(crosshair, 8)

`SET POSITION FOR THE SPRITES
setspriteposition(goodguy, GoodGuyXpos, GoodGuyYpos)
setspriteposition (crosshair, crosshairxpos, crosshairypos)

`START THE DO LOOP
do

print ("score: " +str(score))
print ("random" +str (a))
gosub crosshair
gosub quitkey

sync()
loop

`GOSUBS BELOW.......
`````````````````````````````````````````````````````````````````````````````
crosshair:

setspriteposition (crosshair, getpointerx(), getpointery())
if getpointerpressed()=1 and getspritecollision(crosshair, goodguy)=1

playsound (4)

setrandomseed (100)
a = random ()

if a <= 20
goodguyxpos = 100
endif

if a >= 21 or a <= 70
goodguyxpos = 400
endif

if a >= 71 or a <= 100
goodguyxpos = 800
endif

print ("random" +str (a))

setspriteposition(goodguy, GoodGuyXpos, GoodGuyYpos)
score = score +1
endif

Return

`````````````````````````````````
quitkey:
`q to quit
if getRawKeyPressed(81) = 1
end
endif

return
][img]null[/img]
BatVink
Moderator
16
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 23rd Mar 2015 20:28
Have you taken a look at the SmackIt example game...

C:\Users\[USER]\AppData\Local\AGK Projects\Games\SmackIt

Quidquid latine dictum sit, altum sonatur
Myq
4
Years of Service
User Offline
Joined: 25th Mar 2015
Location:
Posted: 25th Mar 2015 16:38
In reviewing your code there are a few things I would point out.



When you provide a seed for randomization you are telling the random number generator to generate the same series of numbers every time it is executed. I would recommend removing this line entirely as it will, by default, rely on the system time as the seed which will ensure a new number is generated each time.



The random() function returns a default value between 0 and 65535. Based on the code that appears below this that checks for the value of a it appears that you intended for the value to fall between 0 and 100.

To accomplish this you can do one of two things to your code:

You can use keep using random() but use the mod() function to trim your values into the appropriate range.

-OR-

You can use random(from,to) where you actually define the range/bounds of values that you want to be returned. Since this is built-in, this is the way I would recommend that you do it.



These lines actually contain a logic error. I've labeled each of the if statements in order to add some clarity to this explanation.

Suppose that the value of a is 17.

IF-A asks is the value <= 20 (TRUE)
IF-B asks is the value >= 21 (FALSE) -OR- is the value <= 70 (TRUE)
IF-C asks is the value >= 71 (FALSE) -OR- is the value <= 100 (TRUE)

Because part of the -OR- expression is true the entire expression evaluates to true. I would suggest reviewing the boolean operators documentation to get a better handle on this.

In this case, you should be using -AND- instead because you are wanting to define a range where both bounds must be true.
rneilg
4
Years of Service
User Offline
Joined: 18th Mar 2015
Location:
Posted: 25th Mar 2015 22:13
Thanks for your help - I fixed the issue and added timers to make a simple whack-a-mole game.
Can you advise me on a physics issue please?
I have an asteroid that drops down the screen and you use a mouse pointer to keep the asteroid from falling through the floor. Each time you hit the asteroid a sound plays. How do I end the game when it falls through the floor? my asteroidypos > 769 fails?
Thanks for any advice
Neil

Login to post a reply

Server time is: 2020-02-20 14:33:43
Your offset time is: 2020-02-20 14:33:43