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

Username:   Password: 
 RegisterRegister   
 Snake Game Help
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Phenomena




PostPosted: Tue Oct 10, 2006 7:55 pm   Post subject: Snake Game Help

Okay I've been learning turing in my grade 11 Computer Engineering class, and as the final project for this unit we have to make a video game. I decided to make a simple snake game. The only problem is, we never learnt how to identify if a character has been pressed, so my question how do i tell if the one of the arrow keys has been pressed.

so far for my snake i have this Razz

code:
colourback (grey)
cls
var ch:string (1)
var cx,cy,x,y:int :=0
var s,g:int := 0
var e,m,d,l:string
e:="Easy"
m:="Medium"
d:="Hard"
Text.Locate (10,30)
put ("Select Difficulty:")
Text.Locate (11,35)
put e
Text.Locate (11,39)
put (" - 1")
Text.Locate (12,35)
put m
Text.Locate (12,41)
put (" - 2")
Text.Locate (13,35)
put d
Text.Locate (13,39)
put (" - 3")
Text.Locate (14,28)
put ("Enter Difficulty Number:")
Text.Locate (15,39)
get g

cls
Text.Locate (8,20)
put ("Instructions: Use the arrow keys to direct your snake")
Text.Locate (9,20)
put ("to the GREEN apples, but watch out for the poisnous")
Text.Locate (10,20)
put ("RED apples. Eat a red apple and its game over for you")
delay (5000)
cls

for i: 1 .. maxx by 20
    drawline (i,0,i,400,white)
end for
for h: 1 .. maxy by 20
    drawline (0,h,750,h,white)
end for

randint (cx,1,120)
randint (cy,1,75)

proc screen
if
    g = 1
    then l := e
end if
if
    g = 2
    then l := m
end if
if
    g = 3
    then l := d
 end if

put ("Score:")
Text.Locate (1,8)
put s
Text.Locate (1,65)
put ("Level:")
Text.Locate (1,73)
put l
drawfilloval (cx * 5, cy * 5, 5, 5,green)
end screen
screen

proc snake
for x: 293 .. 321 by 14
    drawfilloval (x,y+200,7,7,black)
end for
end snake
snake


its my first application with turing so it may be bit rough >_>
Sponsor
Sponsor
Sponsor
sponsor
neufelni




PostPosted: Tue Oct 10, 2006 8:42 pm   Post subject: (No subject)

To use the arrow keys you need to use Input.KeyDown. Here is a little program that I wrote to show how to use it.
code:
View.Set ("offscreenonly")

var input : array char of boolean
var x, y : int := 200
var direction : string := "right"

loop
    Input.KeyDown (input)
    if input (KEY_RIGHT_ARROW) then
        direction := "right"
    elsif input (KEY_LEFT_ARROW) then
        direction := "left"
    elsif input (KEY_UP_ARROW) then
        direction := "up"
    elsif input (KEY_DOWN_ARROW) then
        direction := "down"
    end if
    if direction = "right" then
        x += 1
    elsif direction = "left" then
        x -= 1
    elsif direction = "up" then
        y += 1
    elsif direction = "down" then
        y -= 1
    end if
    Draw.FillOval (x, y, 10, 10, 7)
    delay (5)
    View.Update
    cls
end loop

This tutorial might also help.
http://www.compsci.ca/v2/viewtopic.php?t=114
You might also want to go through the Turing Walkthroug.
http://compsci.ca/v2/viewtopic.php?t=8808
ericfourfour




PostPosted: Tue Oct 10, 2006 10:27 pm   Post subject: (No subject)

Here is exactly the same thing that Nick did, except it uses arrays. I thought it would be useful since you will need arrays in your snake game if you want to track where the snake has turned etc.

code:

View.Set ("offscreenonly")

const UP := 0
const DOWN := 1
const LEFT := 2
const RIGHT := 3

var input : array char of boolean
var x, y := 200
var dir := RIGHT
var dirs : array 0 .. 3 of char
dirs (UP) := KEY_UP_ARROW
dirs (DOWN) := KEY_DOWN_ARROW
dirs (LEFT) := KEY_LEFT_ARROW
dirs (RIGHT) := KEY_RIGHT_ARROW

loop
    Input.KeyDown (input)
    for i : 0 .. upper (dirs)
        if input (dirs (i)) then
            dir := i
            exit
        end if
    end for
    if dir = UP then
        %action for going right
        y += 1
    elsif dir = DOWN then
        %action for going left
        y -= 1
    elsif dir = LEFT then
        %action for going up
        x -= 1
    elsif dir = RIGHT then
        %action for going down
        x += 1
    end if
    Draw.FillOval (x, y, 10, 10, 7)
    Time.DelaySinceLast (5)
    View.Update
    cls
end loop
Phenomena




PostPosted: Wed Oct 11, 2006 2:16 pm   Post subject: (No subject)

heh thanks guys it helped out alot Very Happy

now i have to set up the collision... sounds fun >_>
ericfourfour




PostPosted: Wed Oct 11, 2006 8:39 pm   Post subject: (No subject)

In my previous post I wrote in the wrong directions in the comments. It should be:

code:

if dir = UP then
    %action for going up
    y += 1
elsif dir = DOWN then
    %action for going down
    y -= 1
elsif dir = LEFT then
    %action for going left
    x -= 1
elsif dir = RIGHT then
    %action for going right
    x += 1
end if
Phenomena




PostPosted: Thu Oct 12, 2006 7:46 pm   Post subject: (No subject)

heh thanks to you guys this is almost done. Im just having troubles with the last few things Razz

im actually proud of myself, although ive probably done in this in the most ineffecient way its actually kinda fun and its my first app Razz
Phenomena




PostPosted: Thu Oct 12, 2006 7:49 pm   Post subject: (No subject)

sorry for double post but i cant edit my last post.

anyways maybe you guys could help me with one last thing. I need to create the red (poisnous) apples. But my problem is since the red apples dont disappear, but just add new ones to the board the way I see it id need to have an infinite amount of variables to do that. Which obvisouly wont work Razz

anyways maybe you guys could just point me in the right direction, i wont learn if you guys just give me the code XD
ericfourfour




PostPosted: Thu Oct 12, 2006 8:09 pm   Post subject: (No subject)

You should probably look up flexible arrays (resizable arrays) and types/records. This would be a simple solution. You could have the types hold the data for the apples and the flexible array could hold the data type.

If you want to go overboard try using classes (OOP). It will take more than a week or maybe a month to learn how to use classes. Then you have to learn how to use them effectively which will take even longer.
Sponsor
Sponsor
Sponsor
sponsor
Phenomena




PostPosted: Thu Oct 12, 2006 8:13 pm   Post subject: (No subject)

well since i only have about a week left ill go with the first Razz

thanks
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  [ 9 Posts ]
Jump to:   


Style:  
Search: