📄 snake.asm
字号:
TITLE GAME SNAKE (2006510040-Assignment3.asm)
; Program Description : This program is for playing game called "snake"
; Author : Ceren 轆H軳 Last update :02.02.09
; Creation Date : 30.01.09
; Date : 02.02.09
include irvine32.inc
.data
;variables
sym_snake BYTE 219 ; symbol of snake something like a square
sym_spc BYTE 32 ; sybol of space
x byte 2 ;starting x index of snake
y byte 2 ;starting y index of snake
endX BYTE 24 ;ending x index of snake stansdart msdos screen
endY BYTE 80 ; ending y index of snake
xEat BYTE ? ; index of the food's x coordinate
yEat BYTE ? ;index of the food's y coordinate
Eat BYTE 42 ; this is a char like "*" for food
scorecounter BYTE 0
msg BYTE " Total Score is: :",0
score BYTE "SCORE :",0
wall BYTE "##############################################################################",0; top wall
;warning BYTE "Press CTRL+C to break",0
.code
main proc
call screen ; tto write to "score " to the first line and draws the wall
call printEat ; Eat is determinden where to put
loop1:
mov al,[sym_snake];snakes value is put on the game area and in al register
mov dh,[x] ;in dh register there is snake's starting x coordinate
mov dl,[y] ;in dl register there is snake's starting y coordinate
call WallControl ; control of wall
call EatControl ; Control of eat
mov al,[sym_snake]
mov dh,[x]
mov dl,[y]
call gotoxy ;the gotoxy proc locates the cursor at a given row and column on the screen.By default the console window's X-coordinate range is 0-79,and the Y-coordinate range is 0-24.
;when you call Gotoxy,pass the Y-coordinate(row) in dh and thr X-coordinate (column)in dl.
call WriteChar;the WriteChar proc. writes a single char. to standart output.Place the char(or its ASCII code) in al before calling the proc.
mov eax,100
call delay; the delay proc.pauses the program for a specified time interval.When calling this function set eax register to the desired interval,in miliseconds.
mov al,[sym_spc] ; the location where the snake was is erased.
call gotoxy
call writeChar
inc y ;the snake is going to the East
;controling the direction acoording to the arrow keys
call readkey
cmp ah,77
je East
cmp ah,75
je West
cmp ah,80
je South
;mov ah,72
cmp ah,72
je North
jmp loop1
;WHEN FINISHED (SAME MEANING WITH GAME OVER IT CALLS THIS)
finished::;end of the program
mov edx,OFFSET msg
call clrscr
call WriteString
movzx eax,scorecounter
call WriteDec
call crlf
call readKey
exit
main endp
;IS THERE A WALL?
;this controls the walls in case the snake hit the wall!
WallControl PROC
mov al,1
cmp al,x
je gameover
mov al,1
cmp al,y
je gameover
mov al,endX
cmp al,x
je gameover
mov al,endY
cmp al,y
je gameover
ret
WallControl ENDP
;this is for writing the Score counter on the screen so that it increases after every eat is eaten by the snake
screen PROC
mov edx,0
mov edx,OFFSET score
call writeString
call crlf
mov edx,OFFSET wall
call WriteString
ret
screen ENDP
;these are the directions controls that the snake move according to the user's arrow keys.
;TO SOUTH DIRECTION
South PROC
start:
mov al,[sym_snake]
mov dh,[x]
mov dl,[y]
call WallControl
call EatControl
mov al,[sym_snake]
mov dh,[x]
mov dl,[y]
call gotoxy
call WriteChar
mov eax,100
call Delay
mov al,[sym_spc]
call gotoxy
call WriteChar
inc x
;the same op. for contorling the directions with arrow keys,
call readkey
;mov ah,77
cmp ah,77
je East
;mov ah,75
cmp ah,75
;mov ah,80
cmp ah,80
je South
;mov ah,72
cmp ah,72
je North
jmp start
ret
South ENDP
;THE NORTH DIRECTION
North PROC
start:
mov al,[sym_snake]
mov dh,[x]
mov dl,[y]
call WallControl
call EatControl
mov al,[sym_snake]
mov dh,[x]
mov dl,[y]
call gotoxy
call writeChar
mov eax,100
call delay
mov al,[sym_spc]
call gotoxy
call writeChar
dec x
call readkey
;mov ah,77
cmp ah,77
je East
;mov ah,75
cmp ah,75
je West
;mov ah,80
cmp ah,80
je South
;mov ah,72
cmp ah,72
je North
jmp start
ret
North ENDP
;TO EAST DIRECTION
East PROC
start:
mov al,[sym_snake]
mov dh,[x]
mov dl,[y]
call WallControl
call EatControl
mov al,[sym_snake]
mov dh,[x]
mov dl,[y]
call gotoxy
call writeChar
mov eax,100
call delay
mov al,[sym_spc]
call gotoxy
call writeChar
inc y
call readkey
;mov ah,77
cmp ah,77
je East
;mov ah,75
cmp ah,75
je West
;mov ah,80
cmp ah,80
je South
;mov ah,72
cmp ah,72 ;
je North
jmp start
ret
East ENDP
;TO WEST DIRECTION
West PROC
start:
mov al,[sym_snake]
mov dh,[x]
mov dl,[y]
call WallControl
call EatControl
mov al,[sym_snake]
mov dh,[x]
mov dl,[y]
call gotoxy
call writeChar
mov eax,100
call delay
mov al,[sym_spc]
call gotoxy
call writechar
dec y
call readkey
;mov ah,77
cmp ah,77
je East
;mov ah,75
cmp ah,75
je West
;mov ah,80
cmp ah,80
je South
;mov ah,72
cmp ah,72
je North
jmp start
ret
West ENDP
;EATING CONTROL
EatControl PROC
; if the eat and snake in the same coordinat; x and y, erase the previous location
mov al,x
cmp xEat,al
jne continue
mov al,y
cmp yEat,al
jne continue
mov dl,[yEat]
mov dh,[xEat]
mov al,[sym_spc]
call gotoxy
call writeChar
inc scorecounter ;increasing the score after each crash to the eat
mov al,scorecounter
mov dl,7
mov dh,0
call gotoxy
call Writedec
call printEat ; write the new location of eat eith its proc. printEat
continue:
ret
EatControl ENDP
;PRINTING THE EAT BY RANDOM
printEat PROC
coorX:
call Randomize ;this random value is restricted by endx value
movzx eax,endX
call randomRange
cmp al,3
jb coorX ; not to enter the score area
cmp al,endX
ja coorX ; not the crash the wall
mov xEat,0
add xEat,al
coorY: ; for y coordinate
call Randomize
movzx eax,endY
call randomRange
cmp al,3
jb coorY
cmp al,endY
ja coorY
mov yEat,0
add yEat,al
mov al,[Eat]
mov dh,[xEat]
mov dl,[yEat]
call gotoxy
call writeChar
ret
printEat ENDP
;WHEN THE GAME OVER:
gameOver PROC
jmp finished
ret
gameOver ENDP
end main
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -