📄 cmd_tetris.cpp
字号:
/*++
original: Tetris v1.0 by Joe Wingbermuehle
19981226
http://www.usmo.com/~joewing
joewing@usmo.com
Module Name:
cmd_tetris.cpp
Abstract: Implements !TETRIS extension commands.
Adaptation to SoftICE 9x: The Owl 2000/07
Adaptation to SoftICE NT: Sten 2002/06
keys: left/right arrows to move the block
up arrow to rotate it
down arrow to make it fall faster
esc to end the game
--*/
extern "C" {
#pragma warning ( push, 3 )
#include <ntddk.h>
#pragma warning ( pop )
}
#pragma warning ( disable: 4514 ) // unreferenced inline function has been removed
#include <windef.h>
#include <ntverp.h>
#include <stdio.h>
#include "wdbgexts.h"
#include "defs.h"
#include "softice.h"
#include "screen.h"
#include "keyboard.h"
static DWORD ticks = 0;
static DWORD seed = 0;
static DWORD a = 16807;
static DWORD r = 2836;
static DWORD q = 127773;
static DWORD delay = 200;
static DWORD LINES = 0;
static DWORD WIDTH = 0;
static DWORD NumOfBlocks = 7;
static struct stats
{
DWORD score;
DWORD lines;
DWORD blocks;
}stats;
static struct board
{
DWORD x;
DWORD y;
DWORD lines;
DWORD width;
}board;
static struct block
{
DWORD x;
DWORD y;
DWORD id;
DWORD lines;
}block;
static WORD blocks[] =
{
0x7F20, 0x0000, 0x0000, 0x0000, // 00
0x7F20, 0x0000, 0x0000, 0x0000,
0x7F20, 0x0000, 0x0000, 0x0000,
0x7F20, 0x0000, 0x0000, 0x0000,
4,
0x7F20, 0x7F20, 0x7F20, 0x7F20, // 01
0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000,
1,
0x7F20, 0x0000, 0x0000, 0x0000, // 02
0x7F20, 0x0000, 0x0000, 0x0000,
0x7F20, 0x0000, 0x0000, 0x0000,
0x7F20, 0x0000, 0x0000, 0x0000,
4,
0x7F20, 0x7F20, 0x7F20, 0x7F20, // 03
0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000,
1,
0xAF20, 0x0000, 0x0000, 0x0000, // 10
0xAF20, 0x0000, 0x0000, 0x0000,
0xAF20, 0xAF20, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000,
3,
0x0000, 0x0000, 0xAF20, 0x0000, // 11
0xAF20, 0xAF20, 0xAF20, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000,
2,
0xAF20, 0xAF20, 0x0000, 0x0000, // 12
0x0000, 0xAF20, 0x0000, 0x0000,
0x0000, 0xAF20, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000,
3,
0xAF20, 0xAF20, 0xAF20, 0x0000, // 13
0xAF20, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000,
2,
0x5F20, 0x0000, 0x0000, 0x0000, // 20
0x5F20, 0x5F20, 0x0000, 0x0000,
0x5F20, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000,
3,
0x0000, 0x5F20, 0x0000, 0x0000, // 21
0x5F20, 0x5F20, 0x5F20, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000,
2,
0x0000, 0x5F20, 0x0000, 0x0000, // 22
0x5F20, 0x5F20, 0x0000, 0x0000,
0x0000, 0x5F20, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000,
3,
0x5F20, 0x5F20, 0x5F20, 0x0000, // 23
0x0000, 0x5F20, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000,
2,
0x4F20, 0x0000, 0x0000, 0x0000, // 30
0x4F20, 0x4F20, 0x0000, 0x0000,
0x0000, 0x4F20, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000,
3,
0x0000, 0x4F20, 0x4F20, 0x0000, // 31
0x4F20, 0x4F20, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000,
2,
0x4F20, 0x0000, 0x0000, 0x0000, // 32
0x4F20, 0x4F20, 0x0000, 0x0000,
0x0000, 0x4F20, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000,
3,
0x0000, 0x4F20, 0x4F20, 0x0000, // 33
0x4F20, 0x4F20, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000,
2,
0x0000, 0x3F20, 0x0000, 0x0000, // 40
0x0000, 0x3F20, 0x0000, 0x0000,
0x3F20, 0x3F20, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000,
3,
0x3F20, 0x3F20, 0x3F20, 0x0000, // 41
0x0000, 0x0000, 0x3F20, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000,
2,
0x3F20, 0x3F20, 0x0000, 0x0000, // 42
0x3F20, 0x0000, 0x0000, 0x0000,
0x3F20, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000,
3,
0x3F20, 0x0000, 0x0000, 0x0000, // 43
0x3F20, 0x3F20, 0x3F20, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000,
2,
0x0000, 0x2F20, 0x0000, 0x0000, // 50
0x2F20, 0x2F20, 0x0000, 0x0000,
0x2F20, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000,
3,
0x2F20, 0x2F20, 0x0000, 0x0000, // 51
0x0000, 0x2F20, 0x2F20, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000,
2,
0x0000, 0x2F20, 0x0000, 0x0000, // 52
0x2F20, 0x2F20, 0x0000, 0x0000,
0x2F20, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000,
3,
0x2F20, 0x2F20, 0x0000, 0x0000, // 53
0x0000, 0x2F20, 0x2F20, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000,
2,
0x9F20, 0x9F20, 0x0000, 0x0000, // 60
0x9F20, 0x9F20, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000,
2,
0x9F20, 0x9F20, 0x0000, 0x0000, // 61
0x9F20, 0x9F20, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000,
2,
0x9F20, 0x9F20, 0x0000, 0x0000, // 62
0x9F20, 0x9F20, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000,
2,
0x9F20, 0x9F20, 0x0000, 0x0000, // 63
0x9F20, 0x9F20, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000,
2
};
//////////////////////////////////////////////////////////////////////////////
// Prototypes
//////////////////////////////////////////////////////////////////////////////
void DrawBoard ();
void DrawNewBlock();
void DrawBlock ();
void TetrisMain ();
void Tick ();
void TetrisScrollLeft ();
void TetrisScrollRight ();
void TetrisScrollDown ();
void TetrisFastDown ();
void TetrisRotateBlock ();
void TetrisEraseBlock ();
void TetrisCompactLines();
//////////////////////////////////////////////////////////////////////////////
// !tetris [speed]
//////////////////////////////////////////////////////////////////////////////
DECLARE_API(tetris)
{
UNREFERENCED_PARAMETER(dwProcessor);
UNREFERENCED_PARAMETER(dwCurrentPc);
UNREFERENCED_PARAMETER(hCurrentThread);
UNREFERENCED_PARAMETER(hCurrentProcess);
if (args[0]=='!') args += 9; // "! tetris "
if (args[0] != 0)
{
_asm
{
mov esi, args
call si_Expression2Integer
jb syntax_error
mov delay, eax
}
}
else
delay = 250;
SaveScreen();
ClearScreen();
LINES = GetHeight();
WIDTH = GetWidth();
ticks = 0;
if (seed == 0) seed = 0x3292; // TO DO: get seed from time
si_EmptyKbdBuffer();
board.lines = 20;
board.width = 12;
board.x = (WIDTH - board.width) / 2;
board.y = (LINES - board.lines) / 2;
stats.score = 0;
stats.lines = 0;
stats.blocks = 0;
DrawBoard();
DrawNewBlock();
HideCursor();
char ch; ch = si_GetChar();
TetrisMain(); // main game loop
RestoreScreen();
DbgPrint("lines completed: %d, score: %d\n", stats.lines, stats.score);
return;
syntax_error:
DbgPrint("Syntax error.\n");
return;
}
//////////////////////////////////////////////////////////////////////////////
//
// TetrisMain
// Main game loop
//
//////////////////////////////////////////////////////////////////////////////
void __declspec(naked) TetrisMain ()
{
__asm
{
_main_loop:
call Tick
mov eax,ticks
cmp eax,delay
jb _F01
and ticks, 0 // ticks = 0
call TetrisScrollDown
jnc _main_loop
call DrawNewBlock
jnc _main_loop
jmp short _exit
_F01:
call si_ReadFromKbdBuffer_char
cmp al, KBD_ESC
jz _exit
push _main_loop // small trick :)
cmp al, KBD_LEFT
jnz not_left
jmp TetrisScrollLeft
not_left:
cmp al, KBD_RIGHT
jnz not_right
jmp TetrisScrollRight
not_right:
cmp al, KBD_DOWN
jnz not_down
jmp TetrisFastDown
not_down:
cmp al, KBD_UP
jnz not_up
jmp TetrisRotateBlock
not_up:
_exit:
retn
}
}
void __declspec(naked) TetrisScrollLeft ()
{
__asm
{
call TetrisEraseBlock
dec block.x
call DrawBlock
jnc exit_loc
inc block.x
call DrawBlock
exit_loc:
retn
}
}
void __declspec(naked) TetrisScrollRight()
{
__asm
{
call TetrisEraseBlock
inc block.x
call DrawBlock
jnc exit_loc
dec block.x
call DrawBlock
exit_loc:
retn
}
}
void __declspec(naked) TetrisScrollDown()
{
__asm
{
call TetrisEraseBlock
inc block.y
call DrawBlock
jnc exit_loc
dec block.y
call DrawBlock
call TetrisCompactLines
stc
exit_loc:
retn
}
}
void __declspec(naked) TetrisFastDown ()
{
__asm
{
call TetrisScrollDown
jnc exit_loc
call DrawNewBlock
jnc exit_loc
add esp, 4 // skip _main_loop
exit_loc:
retn
}
}
void __declspec(naked) TetrisRotateBlock()
{
__asm
{
call TetrisEraseBlock
push block.id
push block.lines
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -