Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Duck Hunt in Turing
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
FredB




PostPosted: Mon Nov 07, 2016 10:31 am   Post subject: Duck Hunt in Turing

What is it you are trying to achieve?
I am trying to make a game similar to Duck Hunt, I do not need to add any graphics to it, I just need the base of the game.


What is the problem you are having?
I don't know how to make my target (a square bouncing around the screen) a button in order for it to act like the duck. I also don't know how to put in a crosshair and a bullet system.


Describe what you have tried to solve this problem
I have tried using previous coding lessons to try and solve my issue but I am seriously confused.


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)


Turing:

View.Set ("title: Duck Hunt!, position: middle, middle, nobuttonbar")
var graphics : string
View.Set ('graphics:1280;720')
View.Set ("offscreenonly")


var xdir : int
var ydir : int
var xpos : int
var ypos : int


xdir := 1
ydir := 2
xpos := 50
ypos := 100



loop 
    cls
if xpos >= maxx or xpos <= 0 then
        xdir *= -1
    end if


    if ypos >= maxy or ypos <= 0 then
        ydir *= -1
    end if


    xpos += xdir
    ypos += ydir

    drawfillbox (xpos, ypos, (xpos+25), (ypos+25), 255)
delay(5)
    View.Update

end loop



Please specify what version of Turing you are using
4.1.1
Sponsor
Sponsor
Sponsor
sponsor
lordroba




PostPosted: Mon Nov 07, 2016 6:17 pm   Post subject: Re: Duck Hunt in Turing

There's many ways you can go about this. You can have a stationary object shoot bullets at the duck, you can have a moving crosshair, or you can have a player that moves and shoots bullets. With the code below you can have a moving crosshair which is similar to what you would get with the NES duckhunt.

With a little bit of extra code you can make it so that whenever you press the SPACE BAR for example, the gun fires. You just have to do some collision detection and see if the duck is hit. Just a matter of comparing the x and y coordinates of the crosshair to that of the duck and make sure they fall within a certain range.

Turing:



View.Set ("title: Duck Hunt!, position: middle, middle, nobuttonbar")
var graphics : string
View.Set ('graphics:1280;720')
View.Set ("offscreenonly")

var xdir : int
var ydir : int
var xpos : int
var ypos : int

xdir := 1
ydir := 2
xpos := 50
ypos := 100

var chars : array char of boolean
var anykey : string (1)
var px : int := 100
var py : int := 100

procedure crosshair %Procedure to Draw crosshair
    drawfillbox (0 + px, 0 + py, 100 + px, 10 + py, black)
    drawfillbox (45 + px, 50 + py, 55 + px, -45 + py, black)
end crosshair

loop
    delay (5)
    cls
    if xpos >= maxx or xpos <= 0 then
        xdir *= -1
    end if


    if ypos >= maxy or ypos <= 0 then
        ydir *= -1
    end if


    xpos += xdir
    ypos += ydir

    drawfillbox (xpos, ypos, (xpos + 25), (ypos + 25), 255)


    %Character commands
    Input.KeyDown (chars)

    %Moves crosshair down
    if chars (KEY_DOWN_ARROW) then
        if py > 50 then
            py -= 2
        end if
    end if

    %Moves crosshair up
    if chars (KEY_UP_ARROW) then
        if py < maxy - 50 then
            py += 2
        end if
    end if

    %Moves crosshair left
    if chars (KEY_LEFT_ARROW) then
        if px > 0 then
            px -= 2
        end if
    end if

    %Moves crosshair right
    if chars (KEY_RIGHT_ARROW) then
        if px < maxx - 100 then
            px += 2
        end if
    end if

    crosshair
    View.Update
end loop
FredB




PostPosted: Tue Nov 08, 2016 1:55 pm   Post subject: Re: Duck Hunt in Turing

Hello again, I have played around a bit with the code you have posted and ended up turning the crosshair to move with my mouse instead, and I had turned in into something else. I think all I have left to do is add a bullet system, make it so my target is a button, and figure out how to increase the difficulty. I have an idea for each but I am not sure on how to proceed with it.

I would like to :
Turn the target into a button.
Learn how to determine whether or not the target was hit or not.
Have a scoring system that can be used to determine difficulty. (ie : if score reaches a certain level, have a higher value for xdir and/or ydir.
Have a randint that changes the xdir and ydir values to increase difficulty, rather than playing wtih the delay.

Thank you again in advance.


Turing:

View.Set ("title: Duck Hunt!, position: middle, middle, nobuttonbar")
var graphics : string
View.Set ('graphics:1280;720')
View.Set ("offscreenonly")

var x, y, m, xdir, ydir, xpos, ypos, px, py : int

xdir := 1
ydir := 3
xpos := 50
ypos := 100



procedure crosshair %Procedure to Draw crosshair
    drawline (px - 30, py, px - 5, py, black)
    drawline (px + 30, py, px + 5, py, black)
    drawline (px, py - 30, px, py - 5, black)
    drawline (px, py + 30, px, py + 5, black)

    Draw.ThickLine (px - 100, py, px - 30, py, 5, black)
    Draw.ThickLine (px + 100, py, px + 30, py, 5, black)
    Draw.ThickLine (px, py - 100, px, py - 30, 5, black)
    Draw.ThickLine (px, py + 100, px, py + 30, 5, black)


    drawoval (px, py, 100, 100, black)
    drawoval (px, py, 30, 30, black)

    drawfilloval (px, py, 3, 3, 12)
end crosshair

loop
    Mouse.Where (x, y, m)
    delay (2)
    cls
    if xpos >= maxx or xpos <= 0 then
        xdir *= -1
    end if


    if ypos >= maxy or ypos <= 0 then
        ydir *= -1
    end if

    px := x
    py := y

    xpos += xdir
    ypos += ydir

    drawfillbox (xpos, ypos, (xpos + 35), (ypos + 35), 255)

    crosshair
    View.Update
end loop

Insectoid




PostPosted: Wed Nov 09, 2016 8:49 am   Post subject: RE:Duck Hunt in Turing

Quote:
Turn the target into a button.
Learn how to determine whether or not the target was hit or not.


What is a button, but a target that's been hit by the mouse? The code for these to things are exactly the same. All you're doing is comparing two sets of coordinates to see if they intersect or collide. There are many collision detection tutorials on this site.

Quote:
Have a scoring system that can be used to determine difficulty. (ie : if score reaches a certain level, have a higher value for xdir and/or ydir.


You might want to refine your bird's movement system or this will be a pain in the ass. Right now you control speed and direction by manipulating vertical and horizontal velocity. That makes it really, really hard to find the speed and direction you want. Better to have speed and direction variables and then use your grade 10 trig to calculate your vertical and horizontal velocities.

Now increasing the difficulty is easy, just increase the speed.
FredB




PostPosted: Thu Nov 10, 2016 12:08 pm   Post subject: Re: Duck Hunt in Turing

Turing:


loop
    Mouse.Where (x, y, b)
    if x >= xpos and x <= (xpos + 35) and y >= ypos and y <= (ypos + 35) and b = 1 then
        put "You hit the target!"     
    else
        put "Try again!"
    end if
    exit when x >= xpos and x <= (xpos + 35) and y >= ypos and y <= (ypos + 35) and b = 1
end loop

[/quote]

I tried to have this for collision detection just as a start but I'm seriously lost. Even when I click absolutely nothing happens..
TipsyCzar




PostPosted: Thu Nov 10, 2016 12:27 pm   Post subject: Re: Duck Hunt in Turing

I don't think you need the (exit when) line of code. You could replace it with this :

if x >= xpos and x <= (xpos + 35) and y >= ypos and y <= (ypos + 35) and b = 1 then
put "You hit the target!"
exit

else
put "Try again!"
end if
Insectoid




PostPosted: Fri Nov 11, 2016 8:52 am   Post subject: RE:Duck Hunt in Turing

That code snippet looks fine to me, so if it's doing nothing, then it's not even running. Can you think of a reason this code might never be executed?
FredB




PostPosted: Fri Nov 11, 2016 10:27 am   Post subject: Re: Duck Hunt in Turing

So I've made it so if I hit the target, the program ends and the message displays that you have hit the target, which is a good step forward. For now I just need to figure out how to make it loop and add a scaling difficulty system and bullets. And also making it display the message if you miss would be good as well.

Turing:


View.Set ("title: Duck Hunt!, position: middle, middle, nobuttonbar")
var graphics : string
View.Set ('graphics:1280;720')
View.Set ("offscreenonly")

var x, y, m, xdir, ydir, xpos, ypos, px, py, b, score : int

xdir := 1
ydir := 2
xpos := 50
ypos := 100
score := 0


procedure crosshair
    drawline (px - 30, py, px - 5, py, black)
    drawline (px + 30, py, px + 5, py, black)
    drawline (px, py - 30, px, py - 5, black)
    drawline (px, py + 30, px, py + 5, black)

    Draw.ThickLine (px - 100, py, px - 30, py, 5, black)
    Draw.ThickLine (px + 100, py, px + 30, py, 5, black)
    Draw.ThickLine (px, py - 100, px, py - 30, 5, black)
    Draw.ThickLine (px, py + 100, px, py + 30, 5, black)


    drawoval (px, py, 100, 100, black)
    drawoval (px, py, 30, 30, black)

    drawfilloval (px, py, 3, 3, 12)
end crosshair

locate(1,1)
put "Score : " put score

loop
    Mouse.Where (x, y, m)
    delay (5)
    cls
    if xpos >= maxx or xpos <= 0 then
        xdir *= -1
    end if


    if ypos >= maxy or ypos <= 0 then
        ydir *= -1
    end if

    px := x
    py := y

    xpos += xdir
    ypos += ydir

    drawfillbox (xpos, ypos, (xpos + 35), (ypos + 35), black)

    crosshair
    View.Update

    Mouse.Where (x, y, b)
if x >= xpos and x <= (xpos + 35) and y >= ypos and y <= (ypos + 35) and b = 1 then
put "You hit the target!"
exit

else
put "Try again!"
end if
end loop


Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: