Tetris

Tetris preview image

1 collaborator

Uri_dolphin3 Uri Wilensky (Author)

Tags

game 

Tagged by Reuven M. Lerner over 10 years ago

tetris 

Tagged by Marzieh Jahanbazi over 10 years ago

Model group CCL | Visible to everyone | Changeable by group members (CCL)
Model was written in NetLogo 5.0.4 • Viewed 684 times • Downloaded 74 times • Run 0 times
Download the 'Tetris' modelDownload this modelEmbed this model

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


WHAT IS IT?

This is the classic puzzle game, Tetris. The game involves falling pieces composed of four blocks in different configurations. The object of the game is to complete horizontal rows of blocks in the well.

Any time a row is completed it disappears and the blocks above it fall down. The more rows you clear with the placement of a single piece, the more points you receive. If you clear enough rows, you move on to the next level. The higher the level, the more points you receive for everything, but the pieces fall faster as well, increasing the challenge.

HOW TO USE IT

Monitors: -- SCORE shows your current score. -- LINES shows the number of lines you have cleared. -- LEVEL shows your current level.

Sliders: -- STARTING-LEVEL selects the beginning level for the game. Choosing a higher level to begin allows you to get more points faster and increase the initial falling speed. Your level will not increase until your number of lines is 10*(level+1). (i.e. starting-level=3, level will stay 3 until 40 lines are cleared.) -- DEBRIS-LEVEL sets how many lines of random blocks will be created at the bottom of the well at the beginning of the game.

Buttons: -- NEW sets up a new game with the initial settings. -- PLAY begins the game.

Controls: -- ROTLEFT rotates the current piece 90 degrees to the left. -- ROTRIGHT rotates the current piece 90 degrees the right. -- LEFT moves the current piece one space left. -- DROP causes the current piece to drop to the bottom of the well immediately. -- RIGHT moves the current piece one space right. -- DOWN moves the current piece one space down.

Options (Switches) -- SHOW-NEXT-PIECE? toggles the option which causes the piece which will appear in the well after you place the current one to be shown in a small box to the right of the well.

THINGS TO NOTICE

There are seven types of pieces. These are all the shapes that can be made by four blocks stuck together.

   [][]      Square-Block - good filler in flat areas,
   [][]         hard to place in jagged areas

   [][][]    L-Block - fits well into deep holes
   []

     [][]    S-Block - good filler in jagged areas,
   [][]         hard to place in flat areas

   [][][]    T-Block - good average piece, can fit
     []         almost anywhere well

   [][]      Reverse S-Block (Or Z-Block) - good
     [][]       filler in jagged areas, hard to
                place in flat areas

   [][][]    Reverse L-Block - fits well into
       []       deep holes

   [][][][]  I-Bar - Only piece that allows you to
                clear 4 lines at once (aka a Tetris)

Scoring System: Note: Points are scored using level + 1 so that points are still scored at level 0. -- 1 Line = 50*(level + 1) points -- 2 Lines = 150*(level + 1) points -- 3 Lines = 350*(level + 1) points -- 4 Lines = 1000*(level + 1) points (aka a Tetris) -- Clear the board = 2000*(level + 1) -- Every piece = 10*(level + 1) points

THINGS TO TRY

Beat your highest score.

EXTENDING THE MODEL

Add options for changing the width and depth of the well.

Add the option of including pieces composed of more than four blocks, or fewer.

NETLOGO FEATURES

This model makes use of turtle breeds.

HOW TO CITE

If you mention this model in a publication, we ask that you include these citations for the model itself and for the NetLogo software:

  • Wilensky, U. (2001). NetLogo Tetris model. http://ccl.northwestern.edu/netlogo/models/Tetris. Center for Connected Learning and Computer-Based Modeling, Northwestern Institute on Complex Systems, Northwestern University, Evanston, IL.
  • Wilensky, U. (1999). NetLogo. http://ccl.northwestern.edu/netlogo/. Center for Connected Learning and Computer-Based Modeling, Northwestern Institute on Complex Systems, Northwestern University, Evanston, IL.

COPYRIGHT AND LICENSE

Copyright 2001 Uri Wilensky.

CC BY-NC-SA 3.0

This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.

Commercial licenses are also available. To inquire about commercial licenses, please contact Uri Wilensky at uri@northwestern.edu.

This model was created as part of the projects: PARTICIPATORY SIMULATIONS: NETWORK-BASED DESIGN FOR SYSTEMS LEARNING IN CLASSROOMS and/or INTEGRATED SIMULATION AND MODELING ENVIRONMENT. The project gratefully acknowledges the support of the National Science Foundation (REPP & ROLE programs) -- grant numbers REC #9814682 and REC-0126227.

Comments and Questions

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

Click to Run Model

globals [
  score
  level
  lines
  next-shape   ;; we number the different piece shapes 0 through 6
  game-over?
]
breed [ pieces piece ]   ;; pieces fall through the air...
breed [ blocks block ]   ;; ...and when they land, they become blocks

pieces-own [
  x y      ;; these are the piece's offsets relative to turtle 0
]
blocks-own [
  line     ;; the bottom line is line 0, above it is line 1, and so on
]

;;;;;;;;;;;;;
;;; Notes ;;;
;;;;;;;;;;;;;

;  It's a little tricky to make it so that the four turtles that make
;  up each piece always move together.  There are various possible ways
;  that you could arrange this, but here's how we chose to make it work.
;  We only ever make four turtles of the "pieces" breed, and we reuse
;  those same four turtles to make up each successive piece that falls.
;  Since they are the first four turtles created, we know that their
;  "who" numbers will be 0, 1, 2, and 3.  turtle 0 is special; it's
;  the turtle around which the others rotate when the user rotates
;  the piece.  The breed variables "x" and "y" hold each turtle's
;  offsets from turtle 0.

;;;;;;;;;;;;;;;;;;;;;;;;
;;; Setup Procedures ;;;
;;;;;;;;;;;;;;;;;;;;;;;;

to new
  clear-all
  set-default-shape turtles "square big"
  set score 0
  set level starting-level
  set lines 0
  set next-shape random 7
  set game-over? false
  ;; make the piece turtles first so they get who numbers of 0 through 3
  create-pieces 4
  [ set heading 180 ]
  ;; draw the board
  ask patches
  [ ifelse (pxcor = -9) or (pxcor > 3) or (pycor = min-pycor)
    [ set pcolor gray ]
    [ ;; make debris
      if ((pycor + max-pycor) <= debris-level and
          (pycor + max-pycor) > 0 and
          (random 2 = 0))
      [ sprout-blocks 1
        [ set line (pycor + max-pycor)
          set heading 180
          set color blue ]
      ]
    ]
  ]
  ;; make the "Next Piece" area
  ask patch 6 10 [ set plabel "Next" ]
  ask patches with [((pxcor > 4) and (pxcor < 9) and
                     (pycor > 5) and (pycor < 11))]
  [ set pcolor black ]
  ;; setup the new piece
  new-piece
end 

;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Interface Buttons ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;

to rotate-right
  if rotate-right-clear? and not game-over?
    [ ask pieces [ rotate-me-right ] ]
end 

to rotate-me-right  ;; Piece Procedure
  let oldx x
  let oldy y
  set x oldy
  set y (- oldx)
  set xcor ([xcor] of turtle 0) + x
  set ycor ([ycor] of turtle 0) + y
end 

to rotate-left
  if rotate-left-clear? and not game-over?
    [ ask pieces [ rotate-me-left ] ]
end 

to rotate-me-left  ;; Piece Procedure
  let oldx x
  let oldy y
  set x (- oldy)
  set y oldx
  set xcor ([xcor] of turtle 0) + x
  set ycor ([ycor] of turtle 0) + y
end 

to shift-right
  if (clear-at? 1 0) and not game-over?
    [ ask pieces [ set xcor xcor + 1 ] ]
end 

to shift-left
  if (clear-at? -1 0) and not game-over?
    [ ask pieces [ set xcor xcor - 1 ] ]
end 

to shift-down
  ifelse (clear-at? 0 -1) and not game-over?
    [ ask pieces [ fd 1 ] ]
    [ place-piece ]
end 

to drop-down
  while [(clear-at? 0 -1) and not game-over?]
    [ ask pieces [ fd 1 ] display ]
  place-piece
end 

;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Runtime Procedures ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;

to play
  if game-over? [ stop ]
  every (0.01 + 0.12 * (10 - level))
    [ shift-down
      check-lines ]
  display
end 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Overlap prevention Reporters ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to-report clear? [p]  ;; p is a patch
  if p = nobody [ report false ]
  report (not any? blocks-on p) and ([pcolor] of p != gray)
end 

to-report clear-at? [xoff yoff]
  report all? pieces [clear? patch-at xoff yoff]
end 

to-report rotate-left-clear?
  report all? pieces [clear? patch-at (- y) x]
end 

to-report rotate-right-clear?
  report all? pieces [clear? patch-at y (- x)]
end 

;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Blocks Procedures ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;

to check-lines
  let n 1
  let line-count 0
  repeat (2 * max-pycor)
  [ ifelse (count blocks with [line = n] = 12)
    [ delete-line n
      set line-count line-count + 1 ]
    [ set n n + 1 ]
  ]
  score-lines line-count
  set lines lines + line-count
  if (floor (lines / 10) > level)
  [ set level floor (lines / 10) ]
end 

to delete-line [n]
  ask blocks with [line = n] [ die ]
  ask blocks with [line > n]
  [ fd 1
    set line (pycor + max-pycor) ]
end 

to score-lines [n]
  let bonus 0
  if n = 1 [ set bonus 50   * (level + 1) ]
  if n = 2 [ set bonus 150  * (level + 1) ]
  if n = 3 [ set bonus 350  * (level + 1) ]
  if n = 4 [ set bonus 1000 * (level + 1) ]
  if (not any? blocks) and (n > 0)
  [ set bonus bonus + 2000 * (level + 1) ]
  set score score + bonus
end 

;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Pieces Procedures ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;

to place-piece
  ask pieces
  [ hatch-blocks 1
    [ set line (pycor + max-pycor) ]
  ]
  set score score + 10 * (level + 1)
  new-piece
end 

to new-piece
  let new-shape next-shape
  set next-shape random 7
  if (show-next?) [ show-next-piece ]
  ask turtle 0 [ setxy -3 12 ]
  ask pieces
  [ setup-piece new-shape ]
  if any? pieces with [any? other turtles-here]
  [ user-message "Game Over"
    set game-over? true ]
end 

to show-next-piece
  ask patches with [(pxcor > 4) and (pxcor < 9) and (pycor > 5) and (pycor < 11)]
  [ set pcolor black ]
  ask turtle 0 [ setxy 6 8 ]
  ask pieces
  [ setup-piece next-shape
    set pcolor color ]
end 

to setup-piece [s]  ;; Piece Procedure
  if (s = 0) [ setupO  set color blue   ]
  if (s = 1) [ setupL  set color red    ]
  if (s = 2) [ setupJ  set color yellow ]
  if (s = 3) [ setupT  set color green  ]
  if (s = 4) [ setupS  set color orange ]
  if (s = 5) [ setupZ  set color sky    ]
  if (s = 6) [ setupI  set color violet ]
  setxy ([xcor] of turtle 0) + x
        ([ycor] of turtle 0) + y
end 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Pieces Setup Procedures ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; The numbers 0123 show the relative positions of turtles
;; 0, 1, 2, and 3 within the overall shape.

;; O-Block
;; 01
;; 23

to setupO ;;Piece Procedure
  if (who = 1) [ set x 1 set y  0 ]
  if (who = 2) [ set x 0 set y -1 ]
  if (who = 3) [ set x 1 set y -1 ]
end 

;; L-Block
;; 201
;; 3

to setupL ;;Piece Procedure
  if (who = 1) [ set x  1 set y  0 ]
  if (who = 2) [ set x -1 set y  0 ]
  if (who = 3) [ set x -1 set y -1 ]
end 

;; J-Block
;; 102
;;   3

to setupJ ;;Piece Procedure
  if (who = 1) [ set x -1 set y  0 ]
  if (who = 2) [ set x  1 set y  0 ]
  if (who = 3) [ set x  1 set y -1 ]
end 

;; T-Block
;; 201
;;  3

to setupT ;;Piece Procedure
  if (who = 1) [ set x  1 set y  0 ]
  if (who = 2) [ set x -1 set y  0 ]
  if (who = 3) [ set x  0 set y -1 ]
end 

;; S-Block
;;  01
;; 23

to setupS ;;Piece Procedure
  if (who = 1) [ set x  1 set y  0 ]
  if (who = 2) [ set x -1 set y -1 ]
  if (who = 3) [ set x  0 set y -1 ]
end 

;; Z-block
;; 10
;;  32

to setupZ ;;Piece Procedure
  if (who = 1) [ set x -1 set y  0 ]
  if (who = 2) [ set x  1 set y -1 ]
  if (who = 3) [ set x  0 set y -1 ]
end 

;; I-Block
;; 1023

to setupI ;;Piece Procedure
  if (who = 1) [ set x -1 set y 0 ]
  if (who = 2) [ set x  1 set y 0 ]
  if (who = 3) [ set x  2 set y 0 ]
end 


; Copyright 2001 Uri Wilensky.
; See Info tab for full copyright and license.

There are 10 versions of this model.

Uploaded by When Description Download
Uri Wilensky almost 11 years ago Updated to NetLogo 5.0.4 Download this version
Uri Wilensky over 11 years ago Updated version tag Download this version
Uri Wilensky over 11 years ago Updated to version from NetLogo 5.0.3 distribution Download this version
Uri Wilensky over 12 years ago Updated to NetLogo 5.0 Download this version
Uri Wilensky almost 14 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky almost 14 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky almost 14 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky almost 14 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky almost 14 years ago Model from NetLogo distribution Download this version
Uri Wilensky almost 14 years ago Tetris Download this version

Attached files

File Type Description Last updated
Tetris.png preview Preview for 'Tetris' about 11 years ago, by Uri Wilensky Download

This model does not have any ancestors.

This model does not have any descendants.