Computer Science Canada COLLISION does not work; help needed |
Author: | FC7321 [ Thu May 16, 2013 12:32 pm ] |
Post subject: | COLLISION does not work; help needed |
I'm trying to make a program similar to Rock Band/Guitar Hero/Tap Tap, but I can't get the collision to work. To narrow down the problem, there is something wrong with this part (keyboard input): %THE b-VALUE % lane 01 controls b1 := -500 if chars ('d') then b1 := 75 end if [syntax="turing"] setscreen ("graphics: 1000; 600") %MUSIC_SONG 01 process song01_subprogram Music.Play ("demo.mp3") end song01_subprogram % ENABLE CONTROLS var chars : array char of boolean var hit : boolean := false Input.KeyDown (chars) % MAIN PROGRAM VARIABLES & CONSTANTS var score : int := 0 var lanenumber : int const totalnotes : int := 10 % PROCEDURE (LOOP) VARIABLES (LANES 1-4) % KEYBOARD TRIGGER var b1 : int var b2 : int var b3 : int var b4 : int % DESCENDING NOTES var y1 : int := 450 var y2 : int := 450 var y3 : int := 450 var y4 : int := 450 % HIT NOTE COUNT var count : int := 0 var winpercentage : int := 0 % PROCEDURES % sucessful hit procedure hitnote count := count + 1 winpercentage := count * 100 div totalnotes % number of milliseconds it takes for the note to go from y=75 to y=0 delay (150) end hitnote % animation of descending note in lane 01 procedure line01_individualnote loop drawoval (100, 75, 25, 25, red) drawoval (200, 75, 25, 25, blue) drawoval (300, 75, 25, 25, green) %THE b-VALUE % lane 01 controls b1 := -500 if chars ('d') then b1 := 75 end if % THE y-VALUE y1 := y1 - 5 exit when y1 = -25 %collision if b1 = y1 then exit end if if b1 + 1 = y1 then exit end if if b1 + 2 = y1 then exit end if if b1 + 3 = y1 then exit end if if b1 + 4 = y1 then exit end if if b1 + 5 = y1 then exit end if if b1 + 6 = y1 then exit end if if b1 + 7 = y1 then exit end if if b1 + 8 = y1 then exit end if if b1 - 1 = y1 then exit end if if b1 - 2 = y1 then exit end if if b1 - 3 = y1 then exit end if if b1 - 4 = y1 then exit end if if b1 - 5 = y1 then exit end if if b1 - 6 = y1 then exit end if if b1 - 7 = y1 then exit end if if b1 - 8 = y1 then exit end if drawfilloval (100, y1, 25, 25, red) delay (3) cls end loop end line01_individualnote % animation of descending note in lane 01 procedure line02_individualnote loop drawoval (100, 75, 25, 25, red) var c := blue drawoval (200, 75, 25, 25, c) drawoval (300, 75, 25, 25, green) % THE b-VALUE b2 := -500 if chars ('f') then b2 := 75 end if %THE y-VALUE y2 := y2 - 5 exit when y2 = -25 %collision if b2 = y2 then c := white end if if b2 + 1 = y2 then c := white end if if b2 + 2 = y2 then c := white end if if b2 + 3 = y2 then c := white end if if b2 + 4 = y2 then c := white end if if b2 + 5 = y2 then c := white end if if b2 + 6 = y2 then c := white end if if b2 + 7 = y2 then c := white end if if b2 + 8 = y2 then c := white end if if b2 - 1 = y2 then c := white end if if b2 - 2 = y2 then c := white end if if b2 - 3 = y2 then c := white end if if b2 - 4 = y2 then c := white end if if b2 - 5 = y2 then c := white end if if b2 - 6 = y2 then c := white end if if b2 - 7 = y2 then c := white end if if b2 - 8 = y2 then c := white end if drawfilloval (200, y2, 25, 25, blue) delay (3) cls end loop end line02_individualnote % animation of descending note in lane 03 procedure line03_individualnote loop drawoval (100, 75, 25, 25, red) drawoval (200, 75, 25, 25, blue) drawoval (300, 75, 25, 25, green) %THE b-VALUE % lane 01 controls b3 := -500 if chars ('j') then b3 := 75 end if % THE y-VALUE y3 := y3 - 5 exit when y3 = -25 %collision if b3 = y3 then exit end if if b3 + 1 = y3 then exit end if if b3 + 2 = y3 then exit end if if b3 + 3 = y3 then exit end if if b3 + 4 = y3 then exit end if if b3 + 5 = y3 then exit end if if b3 + 6 = y3 then exit end if if b3 + 7 = y3 then exit end if if b3 + 8 = y3 then exit end if if b3 - 1 = y3 then exit end if if b3 - 2 = y3 then exit end if if b3 - 3 = y3 then exit end if if b3 - 4 = y3 then exit end if if b3 - 5 = y3 then exit end if if b3 - 6 = y3 then exit end if if b3 - 7 = y3 then exit end if if b3 - 8 = y3 then exit end if drawfilloval (300, y3, 25, 25, green) delay (3) cls end loop end line03_individualnote % PROCEDURE for consecutive notes within song procedure lanes for i : 1 .. totalnotes %<add delay here>: time interval between consecutive notes, as a variable delay (40) randint (lanenumber, 1, 3) if lanenumber = 1 then line01_individualnote elsif lanenumber = 2 then line02_individualnote elsif lanenumber = 3 then line03_individualnote end if y1 := 450 drawoval (100, 75, 25, 25, red) y2 := 450 drawoval (200, 75, 25, 25, blue) y3 := 450 drawoval (300, 75, 25, 25, green) end for end lanes %--------------------------------------------- drawoval (100, 75, 25, 25, red) drawoval (200, 75, 25, 25, blue) drawoval (300, 75, 25, 25, green) % GAME BEGINS // MUSIC STARTS PLAYING fork song01_subprogram lanes put count |
Author: | Nathan4102 [ Thu May 16, 2013 12:51 pm ] |
Post subject: | RE:COLLISION does not work; help needed |
Collision between what and what? I don't remember any collision in Guitar Hero. |
Author: | FC7321 [ Thu May 16, 2013 12:59 pm ] |
Post subject: | RE:COLLISION does not work; help needed |
when the user presses the d/f/j key at the right time (i.e. when the descending note is at a height of 75 pixels) there should be a "collision" the y value determines where the descending note is, in terms of height the b value is set to 75 when the user is pressing the key, if not, it is set at -500 (to ensure that it never collides with the y value IF the key is not pressed) |
Author: | Nathan4102 [ Thu May 16, 2013 1:11 pm ] | ||
Post subject: | RE:COLLISION does not work; help needed | ||
Pseudocode:
|
Author: | FC7321 [ Thu May 16, 2013 5:06 pm ] |
Post subject: | RE:COLLISION does not work; help needed |
I changed the program to make it more efficient (thanks to your advice ), but the collision part still isn't working. Here's what I have now: setscreen ("graphics: 1000; 600") %MUSIC_SONG 01 process song01_subprogram Music.Play ("demo.mp3") end song01_subprogram % ENABLE CONTROLS var chars : array char of boolean var hit : boolean := false Input.KeyDown (chars) % MAIN PROGRAM VARIABLES & CONSTANTS var score : int := 0 var lanenumber : int const totalnotes : int := 10 % PROCEDURE (LOOP) VARIABLES (LANES 1-4) % KEYBOARD TRIGGER var b1 : int var b2 : int var b3 : int var b4 : int % DESCENDING NOTES var y1 : int := 450 var y2 : int := 450 var y3 : int := 450 var y4 : int := 450 % DETECT PRESSED KEYS var left_pressed, down_pressed, right_pressed : boolean := false % HIT NOTE COUNT var count : int := 0 var winpercentage : int := 0 % PROCEDURES % sucessful hit procedure hitnote count := count + 1 winpercentage := count * 100 div totalnotes % number of milliseconds it takes for the note to go from y=75 to y=0 delay (45) end hitnote % animation of descending note in lane 01 procedure line01_individualnote loop drawoval (100, 75, 25, 25, red) drawoval (200, 75, 25, 25, blue) drawoval (300, 75, 25, 25, green) %THE b-VALUE % lane 01 controls if chars (KEY_LEFT_ARROW) then left_pressed := true else left_pressed := false end if % THE y-VALUE y1 := y1 - 5 exit when y1 = -25 %collision if 83 > y1 and y1 > 67 and left_pressed = true then hitnote exit else end if drawfilloval (100, y1, 25, 25, red) delay (10) cls end loop end line01_individualnote % animation of descending note in lane 01 procedure line02_individualnote loop drawoval (100, 75, 25, 25, red) drawoval (200, 75, 25, 25, blue) drawoval (300, 75, 25, 25, green) if chars (KEY_DOWN_ARROW) then down_pressed := true else down_pressed := false end if %THE y-VALUE y2 := y2 - 5 exit when y2 = -25 %collision if 83 > y2 and y2 > 67 and down_pressed = true then hitnote exit else end if drawfilloval (200, y2, 25, 25, blue) delay (10) cls end loop end line02_individualnote % animation of descending note in lane 03 procedure line03_individualnote loop drawoval (100, 75, 25, 25, red) drawoval (200, 75, 25, 25, blue) drawoval (300, 75, 25, 25, green) %THE b-VALUE % lane 01 controls if chars (KEY_RIGHT_ARROW) then right_pressed := true else right_pressed := false end if % THE y-VALUE y3 := y3 - 5 exit when y3 = -25 %collision if 83 > y3 and y3 > 67 and right_pressed = true then hitnote exit else end if drawfilloval (300, y3, 25, 25, green) delay (10) cls end loop end line03_individualnote % PROCEDURE for consecutive notes within song procedure lanes for i : 1 .. totalnotes %<add delay here>: time interval between consecutive notes, as a variable delay (40) randint (lanenumber, 1, 3) if lanenumber = 1 then line01_individualnote elsif lanenumber = 2 then line02_individualnote elsif lanenumber = 3 then line03_individualnote end if y1 := 450 drawoval (100, 75, 25, 25, red) y2 := 450 drawoval (200, 75, 25, 25, blue) y3 := 450 drawoval (300, 75, 25, 25, green) end for end lanes %--------------------------------------------- drawoval (100, 75, 25, 25, red) drawoval (200, 75, 25, 25, blue) drawoval (300, 75, 25, 25, green) % GAME BEGINS // MUSIC STARTS PLAYING fork song01_subprogram lanes put count |
Author: | Nathan4102 [ Thu May 16, 2013 5:43 pm ] |
Post subject: | RE:COLLISION does not work; help needed |
Could you re-post this in turing syntax tags? There's nothing I hate reading more then unindented code. |
Author: | FC7321 [ Thu May 16, 2013 6:09 pm ] | ||
Post subject: | Re: COLLISION does not work; help needed | ||
ah, sorry about that And thanks a lot for your help
|
Author: | Raknarg [ Thu May 16, 2013 6:14 pm ] |
Post subject: | RE:COLLISION does not work; help needed |
The problem is that you aren't updating the chars variable. Input.KeyDown (chars) means that the procedure takes in the variable chars, and see if any keys are pressed. If a key is pressed, it'll change the value of chars. However, it won't do anything if you do not call it. Input.KeyDown (chars) needs to be in your loop if you want it to update |
Author: | FC7321 [ Thu May 16, 2013 6:30 pm ] |
Post subject: | RE:COLLISION does not work; help needed |
AH, thank you so much. I didn't learn about keyboard input, so I just tried imitating the coding that I found in another example |