Skip to content

Commit

Permalink
贪吃蛇1.0版本基础功能初步全部完成
Browse files Browse the repository at this point in the history
================================

wid
  • Loading branch information
mrwid committed Apr 8, 2013
1 parent be2d9e1 commit 60f6078
Show file tree
Hide file tree
Showing 17 changed files with 605 additions and 100 deletions.
13 changes: 9 additions & 4 deletions DealBeginMenuMsg.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ LRESULT DealBeginMenuMsg( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam
static int menuID = 0;
static int oldMenuID = 0;

HDC hdc;
HDC hdc;
PAINTSTRUCT ps;

switch( message )
{
case WM_CREATE:
hInst = ((LPCREATESTRUCT)lParam)->hInstance;
PlaySound( MAKEINTRESOURCE( IDR_GAME_START ), hInst, SND_RESOURCE | SND_ASYNC | SND_LOOP );
SetTimer( hwnd, TMR_BEGIN, 60, NULL );
hInst =(HINSTANCE) GetWindowLong( hwnd, GWL_HINSTANCE );
PostMessage( hwnd, CM_GAME_READY, 0, 0 );
return 0;

case WM_PAINT:
Expand All @@ -26,8 +25,14 @@ LRESULT DealBeginMenuMsg( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam
EndPaint( hwnd, &ps );
return 0;

case CM_GAME_READY:
SetTimer( hwnd, TMR_BEGIN, 60, NULL );
PlaySound( MAKEINTRESOURCE( IDR_GAME_START ), (HINSTANCE)GetWindowLong( hwnd, GWL_HINSTANCE ), SND_RESOURCE | SND_ASYNC | SND_LOOP );
return 0;

case CM_START_GAME:
InvalidateRect( hwnd, NULL, TRUE );
SetWindowLong( hwnd, GWL_WNDPROC, (long)PlayingProc );
SetTimer( hwnd, TMR_PLAYING_READY, 500, NULL ); //开启游戏准备音效定时器
PlaySound( NULL, NULL, SND_FILENAME ); //终止当前音效
return CM_START_GAME;
Expand Down
1 change: 1 addition & 0 deletions DealBeginMenuMsg.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "Init_Begin_UI.h"
#include "HelpDialogProc.h"
#include "MacroDefine.h"
#include "PlayingProc.h"
#include "resource.h"

//////////////////////////////////////////////////////////////////////////
Expand Down
46 changes: 46 additions & 0 deletions GameOverDlgProc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "GameOverDlgProc.h"
#include "MacroDefine.h"

//////////////////////////////////////////////////////////////////////////

LRESULT CALLBACK GameOverProc( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam )
{
HWND hStatic;
LOGFONT lf = {0};
HFONT hFont;

strcpy( lf.lfFaceName, "ËÎÌå" );
lf.lfWidth = 30;
lf.lfHeight = 50;
lf.lfWeight = FW_NORMAL;
lf.lfCharSet = GB2312_CHARSET;
lf.lfPitchAndFamily = 35;

hFont = CreateFontIndirect (&lf);

switch( message )
{
case WM_INITDIALOG:
hStatic = CreateWindow( TEXT("static"), TEXT("GAME OVER !"), WS_CHILD | WS_VISIBLE, 25, 50, 400, 50, hDlg, NULL, NULL, NULL );
SendMessage( hStatic, WM_SETFONT, (WPARAM)hFont, 0 );
return TRUE;

case WM_COMMAND:
switch( LOWORD(wParam) )
{
case IDC_EXIT_GAME:
EndDialog( hDlg, GAME_EXIT );
return TRUE;

case IDC_GAME_AGAIN:
EndDialog( hDlg, GAME_AGAIN );
return TRUE;

case IDC_RETURN_MAIN:
EndDialog( hDlg, GAME_MAIN );
return TRUE;
}
break;
}
return 0;
}
10 changes: 10 additions & 0 deletions GameOverDlgProc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

//////////////////////////////////////////////////////////////////////////

#include <windows.h>
#include "resource.h"

//////////////////////////////////////////////////////////////////////////

LRESULT CALLBACK GameOverProc( HWND, UINT, WPARAM, LPARAM );
8 changes: 2 additions & 6 deletions GameWndProc.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,16 @@

LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
{

static int gameStatus = CM_UN_START;
LRESULT valReturn;

switch( gameStatus )
{
case CM_UN_START:
valReturn = DealBeginMenuMsg( hwnd, message, wParam, lParam );
if( valReturn == CM_START_GAME )
gameStatus = CM_START_ED;
SetWindowLong( hwnd, GWL_WNDPROC, (long)PlayingProc );
return valReturn;

case CM_START_ED:
return PlayingProc( hwnd, message, wParam, lParam );
}

return DefWindowProc( hwnd, message, wParam, lParam );
Expand Down
45 changes: 38 additions & 7 deletions Init_Playing_Map.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,22 @@
#include <stdio.h>
//////////////////////////////////////////////////////////////////////////

void InitPlayingMap( HWND hwnd, HDC hdc, HINSTANCE hInst )
void InitPlayingMap( HWND hwnd, HDC hdc, POINT ptFood, CMAP *gm_map, int level )
{
HPEN hPen;
HBRUSH hBrush;

hPen = CreatePen( PS_SOLID, 3, RGB(128, 128, 128) );
hBrush = CreateSolidBrush( RGB(192, 192, 192) );
SelectObject( hdc, hPen );
SelectObject( hdc, hBrush );

DrawBrickWall( hwnd, hdc );
DrawRandWall( hwnd, hdc, gm_map, level );
DrawRandomFood( hdc, ptFood );

DeleteObject( hPen );
DeleteObject( hBrush );
}

//////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -62,16 +75,14 @@ void DrawRandWall( HWND hwnd, HDC hdc, CMAP *gm_map, int lev )
}
}

//////////////////////////////////////////////////////////////////////////

//绘制随机障碍物
void drawWallLine( HDC hdc, int x1, int y1, int x2, int y2 )
{
int i = 0;
int xPos = 0, yPos = 0;

FILE *fp;
fp = fopen( "D:\\a.txt", "a" );
fprintf( fp, "%d %d %d %d\n", x1, y1, x2, y2 );
fclose( fp );

if( x2-x1 == 0 )
{
yPos = y1;
Expand All @@ -91,5 +102,25 @@ void drawWallLine( HDC hdc, int x1, int y1, int x2, int y2 )
xPos += 10;
}
}
}

//////////////////////////////////////////////////////////////////////////

//绘制食物
void DrawRandomFood( HDC hdc, POINT ptFood )
{
HPEN hPen;
HBRUSH hBrush;

hPen = GetStockObject( WHITE_PEN );
hBrush = CreateSolidBrush( RGB(255, 0, 0) );
SelectObject( hdc, hPen );
SelectObject( hdc, hBrush );

}
Ellipse( hdc, ptFood.x, ptFood.y, ptFood.x+10, ptFood.y+10 );

DeleteObject( hPen );
DeleteObject( hBrush );
}

//////////////////////////////////////////////////////////////////////////
10 changes: 7 additions & 3 deletions Init_Playing_Map.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@

//////////////////////////////////////////////////////////////////////////

void InitPlayingMap( HWND, HDC, HINSTANCE );
void InitPlayingMap( HWND, HDC, POINT, CMAP *, int );

//////////////////////////////////////////////////////////////////////////

void DrawBrickWall( HWND, HDC ); //»­ËÄÖÜש¿é
void DrawBrickWall( HWND, HDC ); //绘制四周砖块

void DrawRandWall( HWND, HDC, CMAP*, int ); //绘制随机障碍物

void DrawRandomFood( HDC, POINT ); //绘制食物


void DrawRandWall( HWND, HDC, CMAP*, int ); //Ëæ»úǽ±Ú
29 changes: 25 additions & 4 deletions MacroDefine.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,37 @@
//////////////////////////////////////////////////////////////////////////

//游戏状态
#define CM_UN_START 0 //停留在起始界面
#define CM_START_ED 1 //正在进行游戏
#define CM_UN_START 0 //停留在起始界面
#define CM_START_ED 1 //正在进行游戏

//////////////////////////////////////////////////////////////////////////

#define CM_START_GAME (WM_USER + 100) //开始游戏消息
#define CM_READY_GAME (WM_USER + 100) //准备游戏消息
#define CM_START_GAME (WM_USER + 101) //开始游戏消息
#define CM_MOVE_SNAKE (WM_USER + 102) //移动蛇身
#define CM_GAME_OVER (WM_USER + 103) //游戏结束
#define CM_GAME_NEXT (WM_USER + 104) //下一关
#define CM_GAME_SUCCEED (WM_USER + 105) //全关成功
#define CM_GAME_READY (WM_USER + 106) //游戏准备

//////////////////////////////////////////////////////////////////////////

#define TMR_BEGIN 10000 //开始界面定时器
#define TMR_PLAYING_READY 10001 //播放准备音效
#define TMR_PLAYING_GO 10002 //播放开始音效
#define TMR_IMPACE_TEST 10003 //碰撞检测正确性测试
#define TMR_IMPACE_TEST 10003 //碰撞检测正确性测试

#define TMR_START_MOVE 10004 //移动蛇身

//////////////////////////////////////////////////////////////////////////

#define DR_UP 20000 //上移动
#define DR_DOWN 20001 //下移动
#define DR_LEFT 20002 //左移动
#define DR_RIGHT 20003 //右移动

//////////////////////////////////////////////////////////////////////////

#define GAME_AGAIN 30000 //再来一局
#define GAME_MAIN 30001 //回主菜单
#define GAME_EXIT 30003 //退出游戏
Loading

0 comments on commit 60f6078

Please sign in to comment.