// Game.cpp: implementation of the CGame class.
//
//////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include "Game.h"
#include "resource.h"
#define SCREEN_W 640
#define SCREEN_H 480
#define OFFSETX 100
#define OFFSETY 48
#define PLAYER1_STARTX 130
#define PLAYER1_STARTY 386
#define PLAYER2_STARTX 258
#define PLAYER2_STARTY 386
#define SAFE_RELEASE(x) if(x){ x->Release()x = NULL}
CGame* g_pGame
inline int random( int min, int max )
{
return (min + rand() % (max - min + 1))
}
void TRACE( LPCTSTR format, ... )
{
charbuf[128]
va_list vl
va_start(vl, format)
sprintf(buf, format, vl)
OutputDebugString( buf )
va_end(vl)
}
void CGame::OutputText( int x, int y, LPCTSTR string )
{
HDC hdc
if( m_pddsBackBuffer &&
m_pddsBackBuffer->GetDC(&hdc) == DD_OK )
{
SetBkMode( hdc, TRANSPARENT )
SetTextColor( hdc, RGB(255,255,0) )
TextOut( hdc, x, y, string, lstrlen(string) )
m_pddsBackBuffer->ReleaseDC( hdc )
}
}
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CGame::CGame()
{
m_hInst = NULL
m_hWnd = NULL
m_bActive = FALSE
m_bShowStats = FALSE
m_bFullScreen = TRUE
m_bSingle = TRUE
m_pDD = NULL
m_pddsFrontBuffer = NULL
m_pddsBackBuffer = NULL
g_pGame = this
}
CGame::~CGame()
{
}
LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
if( g_pGame )
return g_pGame->MsgProc( hWnd, uMsg, wParam, lParam )
return DefWindowProc( hWnd, uMsg, wParam, lParam )
}
LRESULT CGame::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
switch( uMsg )
{
case WM_ACTIVATEAPP:
if( m_bActive = (BOOL)wParam )
m_DirectInput.Acquire()
return 0
case WM_SETCURSOR:
if( m_bFullScreen )
{
SetCursor( NULL )
return TRUE
}
break
case WM_CLOSE:
PostQuitMessage(0)
return 0
case WM_MOVE:
m_bActive = TRUE
GetClientRect( hWnd, &m_rcWindow )
ClientToScreen( hWnd, (LPPOINT)&m_rcWindow )
ClientToScreen( hWnd, (LPPOINT)&m_rcWindow + 1 )
return 0
case WM_MOVING:
m_bActive = FALSE
break
case WM_KEYDOWN:
switch( wParam )
{
case VK_ESCAPE:
if( m_gameState == GS_ACTIVE )
m_gameState = GS_SPLASH
else if( m_gameState == GS_SPLASH )
PostQuitMessage(0)
else if( m_gameState == GS_OVER )
m_gameState = GS_SPLASH
break
case VK_F4:
DDTerm()
m_bFullScreen = !m_bFullScreen
DDInit()
return 0
case VK_F5:
m_bShowStats = !m_bShowStats
return 0
case VK_DOWN:
case VK_UP:
if( m_gameState == GS_SPLASH )
{
m_bSingle = !m_bSingle
return 0
}
break
case VK_PRIOR:
if( m_gameState == GS_ACTIVE &&m_nLevel >1 )
{
m_nLevel --
InitLevel()
}
break
case VK_NEXT:
if( m_gameState == GS_ACTIVE )
{
m_nLevel ++
InitLevel()
}
break
case VK_RETURN:
if( m_gameState == GS_SPLASH )
{
ResetGame()
return 0
}
else if( m_gameState == GS_ACTIVE &&!m_bSingle )
{
if( m_player[0].m_nLife >1 &&
m_player[1].m_nLife <= 0 )
{
m_player[0].m_nLife --
m_player[1].m_nLife = 1
ResetPlayer( m_player[1] )
}
else if( m_player[1].m_nLife >1 &&
m_player[0].m_nLife <= 0 )
{
m_player[1].m_nLife --
m_player[0].m_nLife = 1
ResetPlayer( m_player[0] )
}
}
break
}
break
}
return DefWindowProc( hWnd, uMsg, wParam, lParam )
}
BOOL CGame::Initialize( HINSTANCE hInst )
{
m_hInst = hInst
if( !InitApplication() ||
!DDInit() ||
!InitGame() )
{
DDTerm()
return FALSE
}
return TRUE
}
BOOL CGame::InitApplication()
{
char szClassName[] = "BATTLECITY"
char szTitle[] = "Battle City"
WNDCLASS wc
wc.hInstance = m_hInst
wc.lpszClassName = szClassName
wc.lpfnWndProc = WndProc
wc.style = CS_DBLCLKS
wc.hIcon = LoadIcon( m_hInst, MAKEINTRESOURCE(IDI_TANK) )
wc.hCursor = LoadCursor( NULL, IDC_ARROW )
wc.lpszMenuName = NULL
wc.cbClsExtra = 0
wc.cbWndExtra = 0
wc.hbrBackground = (HBRUSH)GetStockObject( BLACK_BRUSH )
if( !RegisterClass( &wc ) )
{
TRACE( "Error In RegisterClassEx\n" )
return FALSE
}
m_hWnd = CreateWindow( szClassName,
szTitle,
WS_OVERLAPPED | WS_MINIMIZEBOX | WS_SYSMENU,
CW_USEDEFAULT,
CW_USEDEFAULT,
646, 505,
NULL,
NULL,
m_hInst,
NULL )
if( !m_hWnd )
{
TRACE( "Error In CreateWindow\n" )
return FALSE
}
UpdateWindow( m_hWnd )
ShowWindow( m_hWnd, SW_NORMAL )
return TRUE
}
int CGame::Run()
{
MSG msg
while( TRUE )
{
if (PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE))
{
if (!GetMessage( &msg, NULL, 0, 0))
break
TranslateMessage(&msg)
DispatchMessage(&msg)
}
else if( m_bActive )
{
UpdateFrame()
}
else
WaitMessage()
}
DDTerm()
return msg.wParam
}
BOOL CGame::DDInit()
{
HRESULT hr
hr = DirectDrawCreate( NULL, &m_pDD, NULL )
if( FAILED(hr) )
{
TRACE( "Error Create DirectDraw\n" )
return FALSE
}
if( m_bFullScreen )
m_pDD->SetCooperativeLevel( m_hWnd, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN )
else
m_pDD->SetCooperativeLevel( m_hWnd, DDSCL_NORMAL )
方法如下:
1、安装完成后,双击易语言图标打开易语言。这里d出了一个对话框。
2、双击中间的“windows窗口程序”新建一个程序。界面如图所示。
菜单栏:程序的各种 *** 作及设置。
窗口编辑区:绘制界面
属性设置区:设置组件、窗口的各种属性
组件框:选择各种组件
提示区:显示当前程序的运行、编译情况。显示对于某一命令的帮助提示。
3、在右侧组件框找到“标签”组件 选择后在界面上拖动,松开鼠标,就画出了一个标签。
4、在左侧属性栏找到“标题”属性,修改为“我的第一个程序”并敲下回车。标签的标题就更改了
5、接下来用同样的方法创建一个编辑框和一个按钮。
打出如图中的代码。或者复制以下。
.版本 2
编辑框1.内容 = “你好,世界”
信息框 (“我的第一个易语言程序”, 0, )
6、双击按钮进入代码编辑页面。把输入法调为英文状态。
打出如图中的代码。或者复制以下。
.版本 2
编辑框1.内容 = “你好,世界”
信息框 (“我的第一个易语言程序”, 0, )
7、点击上方三角按钮,或者单击F5运行程序查看效果。效果如图所示
8、点击按钮。d出了一个信息框,编辑框的标题也变为了“你好,世界”我们的第一个易语言程序制作完成!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)