Snake Game

No preview image

1 collaborator

Tags

(This model has yet to be categorized with any tags)
Visible to everyone | Changeable by everyone
Model was written in NetLogo 6.0.4 • Viewed 275 times • Downloaded 11 times • Run 0 times
Download the 'Snake Game' modelDownload this modelEmbed this model

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)


Nicholas Evangelinos's version of: SNAKE

Concept:

To play snake you are going to start off as a tiny little snake head trying to make your way through life, and become the biggest snake possible. To grow bigger you're going to want to eat those apples, cookies, and McDonalds french fries that keep conveniently popping up as soon as you've finnished eating another. But life isn't so easy for you since you live in a world where bobs keep dropping from the sky to destroy you. luckily you have some time before each one explodes. Watch out for the black wall along to sides too, if you it that your a dead man... or snake rather. You also cant tutch yourself because if you do you'll eat yourself and die. Good luck out there, and Have fun.

How To play: controls

Use W, D, S, and A to turn your snake Note you can not use any one of these botons at any time:

If yourfaceing straight foward at 0 degrees you cannot just press S to turn your snake 180 degrees. You must frst press D, then followed by S.

additionally each botton will either turn the snake right or left depending on what direction the snake is facing

If you are facing to your right you can either use W to turn left or S to turn right and so on ecetra

Bug and Struggles:

  1. One major issue I faced was that the foods the turtle had to eat would spawn randomly so occasionally they would spawn on the black boarder, meaning as soon as the player went to eat the food, the player would loose. The way I solved this issue was that I would ask the foods in the spawning food procedure: if pcolor = black [spawn-food die]. That way if the food was on the black boarder it would kill itself and make a new food using the very same procedure used to create itself.

  2. To monitor if the snake is in the blast radius of a bomb's explotion the bombs woudl color the patches under themselves red, and if the snake was every on a red patch it would die. However if the bomb was too close to the boarder it would turn part of the boarder red, and then when the red disappeared the recolored boarder would turn white aswell. to fix this I had to create my own custom random x-cor y-cor generator, since to dimentions of the world have been tampered with.

  3. A strange bug that i faced was that the first time I pressed the PLAY button when opening my model Everything would set up as it should, except that the first food that the snake needed to collect was in the default shape. It still acted like all the other foods should when the snake came in contact with it, so it was definitley still a food breed but it didn't look it. To fix this problem I created a reset procedure, which for the most part did the same things as the setup procedure except for asking the player for their name, since they would have already put than in from the setup procedure. Then I put the reset procedure into the end of the setup procedure. so it would clear everything except for important variables, and then it would recreate everyting. this meant that when you press the PLAY botton it would call the setup procedure: makeing the strange default shaped food, then it would kill it and make a normal one.

Salutations:

  • I used Mr. Peter Brooks's model called Whack-A-Demo.netlogo as reference to create external txt files outside of netlogo and use there information in netlogo. I used this to keep Highscores saved evan after the player closes the Game model. Wack-A-Demo can be found on the [Intro to computer Science homepage] (http://bert.stuy.edu/pbrooks/fall2018/materials/intro-year-1/intro-pilot-1_homepage.htm)

  • I coded the rest of the code myself: Nicholas Evangelinos

Comments and Questions

Please start the discussion about this model! (You'll first need to log in.)

Click to Run Model

globals [score
  highscore
  BestPlayer
food-x
food-y
ways-to-die
UN
explotion-time
faster
random-color
tail-color
stripe-color]
patches-own [p-timer]
turtles-own [explotion
  IC-timer
]
breed [apps app]
breed [freds fred]
breed [CMs CM]
breed [snks snk]
breed [bmbs bmb]
breed [Bmans Bman]
breed [ebmbs ebmb]
breed [s-stripes s-stripe]

to setup
  ca
  let user-name user-input "Player's Name"
  ask patches [set pcolor white]
  set random-color (((random 12) * 10) + 15)
  set tail-color ( random-color + 2)
  set stripe-color ( random-color - 2)
  create-snks 1 [set heading 0
    set shape "snake2"
  set size 2
  set color random-color]
  create-s-stripes 1 [set heading 0
    set size 2
    set color stripe-color
  ]
   create-Bmans 1 [
    setxy -17 0
    set heading 0
    set pcolor black
    repeat 13 [
      fd 1
      set pcolor black]
    rt 90
    repeat 35 [
      fd 1
      set pcolor black]
    rt 90
    repeat 27 [
      fd 1
      set pcolor black]
    rt 90
    repeat 35 [
      fd 1
      set pcolor black]
    rt 90
    repeat 20 [
      fd 1
      set pcolor black]
    die
  ]
  spawn-food
  set-default-shape apps "apple"
  set-default-shape freds "fries"
  set-default-shape CMs "cookie"
  set-default-shape bmbs "bomb"
  set-default-shape ebmbs "ebomb"
  set-default-shape s-stripes "snake-stripes"
  set score 1
  set UN user-name
  set ways-to-die 0
  set explotion-time 0
  set faster 4
  Show-Highscore
  reset
end 

to reset
  ct
  cp
  set random-color (((random 11) * 10) + 15)
  set tail-color (random-color + 2)
  set stripe-color (random-color - 2)
  ask patches [set pcolor white]
  create-snks 1 [set heading 0
    set shape "snake2"
  set size 2
  set color random-color]
  create-s-stripes 1 [set heading 0
    set size 2
    set color stripe-color
  ]
   create-Bmans 1 [
    setxy -17 0
    set heading 0
    set pcolor black
    repeat 13 [
      fd 1
      set pcolor black]
    rt 90
    repeat 35 [
      fd 1
      set pcolor black]
    rt 90
    repeat 27 [
      fd 1
      set pcolor black]
    rt 90
    repeat 35 [
      fd 1
      set pcolor black]
    rt 90
    repeat 20 [
      fd 1
      set pcolor black]
    die
  ]
  spawn-food
  set-default-shape apps "apple"
  set-default-shape freds "fries"
  set-default-shape CMs "cookie"
  set-default-shape bmbs "bomb"
  set-default-shape ebmbs "ebomb"
  set-default-shape s-stripes "snake-stripes"
  set score 1
  set ways-to-die 0
  set explotion-time 0
  set faster 4
  set highscore 0
  Show-Highscore
end 

to go
every 0.1 [
    ask turtles [
      if shape = "snake-stripes" [
        fd 1]
      if shape = "snake2"
      [
      fd 1
        ifelse pcolor = white [set pcolor tail-color ]
        [ifelse pcolor = tail-color
          [
          set pcolor black
          set ways-to-die 1
        ]

          [if pcolor = red [set ways-to-die 1]]
        ]
        ask bmbs
        [if size > 3 [set size (size - 1)]
        ifelse color = black
          [set color red
          set explotion (explotion + 3)]
      [set color black
           set explotion (explotion + 2)]
           if explotion = 50 [ask patch-here [sprout-ebmbs 1 [set size 17]
            ]
            die
          ]

      ]
        ask ebmbs [set explotion-time (explotion-time + 1)
          ask patches in-radius 5 [if pcolor = white [
            set pcolor red]
          ]
        if explotion-time mod 20 = 0 [die]
        ]
      ]
      ask snks [if pcolor = black [set ways-to-die 1]]
        ask patch xcor ycor[ set p-timer score
      ]
        ask patch food-x food-y [
          if count turtles-here = 2 [
      ask apps [ set score (score + 3)
            die]
      ask freds [ set score (score + 2)
            die]
      ask CMs [ set score (score + 4)
            die]
        spawn-food]
      ]
    ]
    ask patches [set p-timer (p-timer - 1)
        if p-timer <= 0 and pcolor = tail-color [set pcolor white]
      if explotion-time mod 20 = 0 and pcolor = red [set pcolor white]
    ]
    if score > 25 [set faster 3]
    if ways-to-die = 1 [
      wait 0.4
      You-lose
      stop]
  ]
  every faster [spawn-bomb]
end 

to spawn-bomb
    ask patch (((random 20 + 1) * -1) + 10) (((random 14 + 1) * -1) + 7) [
    sprout-bmbs 1 [set heading 28
        set color red
      set size 15]
    ]
end 

to spawn-food
   ask patch  (((random 16 + 1) * -1) + 8) (((random 12 + 1) * -1) + 6)
  [ifelse random 3 = 0
      [sprout-CMs 1  [setxy random-xcor random-ycor
      set heading 0
      set size 2
  set food-x xcor
  set food-y ycor
      if pcolor = black or pcolor = 67 [spawn-food
      die]
      ]
  ]
      [ifelse random 2 = 1 [sprout-freds 1  [setxy random-xcor random-ycor
      set heading 0
      set size 2
    set food-x xcor
  set food-y ycor
      if pcolor = black or pcolor = 67[spawn-food
      die]
      ]
   ]
    [sprout-apps 1 [setxy random-xcor random-ycor
        set heading 0
        set size 2
     set food-x xcor
  set food-y ycor
        if pcolor = black or pcolor = 67 [spawn-food
      die]
        ]
  ]
  ]
  ]
end 

to W
 ask snks [
    if heading = 270 [set heading 0]
    if heading = 90 [set heading 0]
  ]
   ask s-stripes [
    if heading = 270 [set heading 0]
    if heading = 90 [set heading 0]
  ]
end 

to D
  ask snks [
    if heading = 0 [set heading 90]
    if heading = 180 [set heading 90]
  ]
  ask s-stripes [
    if heading = 0 [set heading 90]
    if heading = 180 [set heading 90]
  ]
end 

to S
  ask s-stripes [
    if heading = 270 [set heading 180]
    if heading = 90 [set heading 180]
  ]
  ask snks [
    if heading = 270 [set heading 180]
    if heading = 90 [set heading 180]
  ]
end 

to A
  ask s-stripes [
  if heading = 0 [set heading 270]
  if heading = 180 [set heading 270]
]
   ask snks [
  if heading = 0 [set heading 270]
  if heading = 180 [set heading 270]
]
end 

to You-lose
  ct
  ask patches [set pcolor black]
  ask patch 4 0 [set plabel "YOU LOSE!!"]
  ask patch 5 -2 [set plabel "PRESS RESTART"]
  ask patch 7 -4 [set plabel "THEN PRESS GO TO TRY AGAIN"]
  New-Highscore
end 

to Show-Highscore
  ifelse file-exists? "EKANS.txt" [
   file-open "EKANS.txt"
   set highscore file-read
   set BestPlayer file-read
   file-close
  ]
  [set highscore 0
      set BestPlayer "None"]
end 

to New-Highscore
  if score > highscore [
    set highscore score
    set BestPlayer UN
  if file-exists? "EKANS.txt" [file-delete "EKANS.txt"]
 file-open "EKANS.txt"
 file-write highscore
 file-write BestPlayer
  file-close
]
end 

to delete-highscore
  set highscore 0
  set BestPlayer "none"
end 

;to spawn-the-holy-icecream
 ; ask patch (((random 16 + 1) * -1) + 8) (((random 12 + 1) * -1) + 6) [
  ;  sprout-icreams 1 [
   ;   set size 4
    ;set heading 0
     ; set ice-x xcor
      ;set ice-y ycor
      ;if pcolor = black [
      ;  spawn-the-holy-icecream
    ;    die ]
    ;]
  ;]
;end

There is only one version of this model, created over 1 year ago by Nicholas Evangelinos.

Attached files

No files

This model does not have any ancestors.

This model does not have any descendants.