高分悬赏急求一段源代码

2024-11-20 07:28:18
推荐回答(5个)
回答(1):

#include
#include
#include
using namespace std;

class Librarian //图书管理员类
{
public:
Librarian();
Librarian(int n,int j,char na[20],int w );
int getnumber();
int getjobage();
char *getname();
int getwages();
void resetjobage(int j);
void resetwages(int w);
void resetname(char na[20]);
private:
int number;
int jobage;
char name[20];
int wages;
};
Librarian::Librarian() //其函数的实现
{
number=0;
char b[20]="no one";
jobage=0;
strcpy(name,b);
}
Librarian::Librarian(int n,int j,char na[20],int w)
{
number=n;
jobage=j;
strcpy(name,na);
wages=w;
}
int Librarian::getjobage()
{return jobage;}
char *Librarian::getname()
{return name;}
int Librarian::getnumber()
{return number;}
int Librarian::getwages()
{return wages;}
void Librarian::resetjobage(int j)
{
jobage=j;
}
void Librarian::resetname(char na[20])
{
strcpy(name,na);

}
void Librarian::resetwages(int w)
{
wages=w;
}

class reader //读者类
{
private:
int number;
int age;
char name[20];
char borrowbook[20];
public:
reader();
reader(int c,int a,char b[20],char e[20]);
int getnumber();
int getage();
char *getname();
char * getborrowbook();
void reworkborrowbook(char a[20]);
};
reader::reader() //其函数的实现
{
char a[20]="没有人";
char c[20]="没有借书";
number=0;
age=0;
}
reader::reader(int a,int c,char d[20],char e[20])
{
number=a;
age=c;
strcpy(name,d);

strcpy(borrowbook,e);
}
int reader::getnumber(){return number;}
int reader::getage(){return age;}
char *reader::getname(){return name;}
char * reader::getborrowbook(){return borrowbook;}
void reader::reworkborrowbook(char a[20]){strcpy(borrowbook,a);}

struct book //图书的结构体
{
int num;

char name[20];
char writer[20];
};

void outbookinf() //输出所有图书信息
{
int i;
ifstream infile("allbook.txt",ios::in);
char line[50];
cout<<"书名"<<'\t'<<"书号"<<'\t'<<"作者"< for(i=1;i<=10;i++)
{
infile.getline(line,50,'\n');
cout< }
infile.close();
}

void serchLaninf() //图书管理员信息函数
{
int n,i;
ifstream file("serchLaninf.txt");
if(file) { file>>n; file.close();}
else {cout<<"没有图书管信息理员"; exit(0);}
ifstream is("controlloer.txt",ios_base::binary);
if(is)
{
Librarian *A=new Librarian[n];
for(i=0;i {
is.read((char *)&A[i],sizeof(A[i]));
}
for(i=0;i {
cout<<"编号"<<'\t'<<"工龄"<<'\t'<<"名字"<<'\t'<<"工资"< cout< }
}
else
{
cout<<"txt文件打开出错'"< }
is.close();
}

void libary() //图书馆的总信息
{
system("cls");
system("color 2e");
cout<<" "< cout<<" "< cout<<" "< cout<<"本校图书馆由本部图书馆及彭州校区图书馆组成,图书馆总面积达了****平方米";
cout<<" (本馆现在有***类书籍***万册,****类书籍****万册,****类书籍****万册****类书籍****万册),";
cout<<"图书馆现有工作人员***名";
cout<<"图书馆现有的现代化设备价值超过****万元………………"<}

void storelibrarian() //存入新的管理员的信息
{
int age,n,num,wage;
char name[20];
ifstream file("serchLaninf.txt");
if(file) { file>>n; file.close();}
else { n=0;}
cout<<"已有的图书管理员个数:";
cout< cout<<"请输入新图书管理员的编号和工龄,名字和工资:";
cin>>num>>age>>name>>wage;
Librarian A(num,age,name,wage);
cout<<"编号 工龄 名字 工资 "< cout< ofstream outfile("controlloer.txt",ios_base::app );
outfile.write((char *)&A,sizeof(A));
outfile.close();
n++;
ofstream tfile("serchLaninf.txt");
tfile< tfile.close();
cout<<"保存成功!";
}

void reLaninf() //修改图书管理员
{
int i,n,h,jobage,wage,j;
char name[20];
ifstream file("serchLaninf.txt");
if(file) { file>>n; file.close();}
else {cout<<"没有图书管信息理员";
exit(0);}
cout<<"已有的图书管理员个数:";
cout< Librarian *A=new Librarian[n];
ifstream is("controlloer.txt",ios_base::binary);
if(is)
{
for(i=0;i {
is.read((char *)&A[i],sizeof(A[i]));
}
for(i=0;i {
cout<<"编号 工龄 名字 工资"< cout< }
}
else
{
cout<<"打开文件出错"< }
is.close();
cout<<"请选择你要修改的管理员编号:";
cin>>h;
cout<<"输入新的工龄,名字,工资:";
cin>>jobage>>name>>wage;
A[h-1].resetjobage(jobage);
A[h-1].resetname(name);
A[h-1].resetwages(wage);
cout<<"该管理员修改后的名字是:"< ofstream tfile("controlloer.txt",ios_base::binary);
for(j=0;j {
tfile.write((char *)&A[j],sizeof(A[j]));
}
tfile.close();
}

void aadbook() //新增图书
{
int num;char name[12];char writer[10];int n;
a:
ofstream fout("allbook.txt",ios::app);
cout<<"请录入图书信息:"<cout<<"书名"<<'\t'<<"书号"<<'\t'<<"作者"<cin>>name;fout<cin>>num;fout<cin>>writer;fout<fout.close();
cout<<"是否继续录入?1.是2.否"<cin>>n;
if(n==1)
goto a;
else
{void studentland();
studentland();}
}

void delate() //删除函数
{ int m;
cout<<"选择操作选项:"< cout<<" 1.删除全部信息"< cout<<" 2.单个删除"< cin>>m;
if(m==1)
{ofstream file("allbook.txt",ios::trunc);
cout<<"全部删除完毕,按任意键返回!"< file.close();
getch();
system("cls");
void studentland();
studentland();
}
char str[10][80],buf[20];
a:
int i=0,flag=100,c=0;

ifstream fin("allbook.txt");
if(!fin){
cout<<"文件打开失败!"<getch();
system("cls");
void studentland();
studentland();
}
cout<<"请输入你要删除的书名:"<cin>>buf;
strcat(buf,"\t");

while(fin.getline(str[i],80))
{
if(strncmp(str[i],buf,strlen(buf))==0)
{
flag=i;
}
i++;
}

fin.close();

if(flag==100)
{
cout<<"没有你所要删除的书籍!重新按1,否则0....."<int b;
cin>>b;
if(b==1)goto a;
else c=1;
}
if(c==0){
ofstream fout("allbook.txt");
for(int j=0;jif(j==flag)continue;
fout<}
fout.close();
cout<<"你要删除的书籍的信息已删除,继续按1,否则按0....."<int b;
cin>>b;
if(b==1)goto a;
}
cout<<"操作已经结束,按任意键返回......"<getch();
system("cls");
void studentland();
studentland();
}

void searchbook() // 查找书籍
{
a:
ifstream fin;
int flag=0,b,length;
char str[20];
char f[200];char g[200];
fin.open("allbook.txt");
if(!fin)
{
cout<<"Open f1.dat error....."< exit(1);
}
while(1)
{
cout<<"请输入书名:";
cin>>str;
strcat(str,"\t");
length=strlen(str);
cout<<"------------------------------------"< while(fin.getline(f,199))
{
strcpy(g,f);
if(strncmp(str,f,length)==0)
{
flag=1;
break;
}
}
if(flag==1)
{
cout<<"所查询的书已经找到:"<<'\n';
cout<<"--------------------------------"< cout<<"书名"<<'\t'<<"书号"<<'\t'<<"作者"<<'\t'< cout< cout<<"--------------------------------"< flag=0;
cout<<"要继续查询按1,否则按0:"< cin>>b;
if(b==0)break;
}
else
{
fin.close();
cout<<"没有此书,继续按1,否则按0..."< cin>>b;
if(b==0)break;
goto a;
}
}
fin.close();
cout<<"查询完毕,按任意键返回......"<getch();
system("cls");
void studentland();
studentland();
}

void seachreader() // 查询读者信息
{
a:
ifstream fin;
int flag=0,b,length;
char str[20];
char f[200];char g[200];
fin.open("readernum.txt");
if(!fin)
{
cout<<"没有此信息文件!"< exit(1);
}
while(1)
{
cout<<"请输入你要查找读者的名字:";
cin>>str;
strcat(str,"\t");
length=strlen(str);
cout<<"------------------------------------"< while(fin.getline(f,199))
{
strcpy(g,f);
if(strncmp(str,f,length)==0)
{
flag=1;
break;
}
}
if(flag==1)
{
cout<<"所查询的读者已经找到:"<<'\n';
cout<<"--------------------------------"< cout<<"名字"<<'\t'<<"编号"<<'\t'<<"年龄"<<'\t'<<"借书次数"< cout< cout<<"--------------------------------"< flag=0;
cout<<"要继续查询按1,否则按0:"< cin>>b;
if(b==0)break;
goto a;
}
else
{
fin.close();
cout<<"没有此人,继续按1,否则按0..."< cin>>b;
if(b==0)break;
goto a;
}
}
fin.close();
cout<<"查询完毕,按任意键返回......"<getch();
system("cls");
void studentland();
studentland();
}

/*void seachreader() //查询读者的信息
{
int i,b,h=0;
char a[20];
ifstream file("readernum.txt");
if(file) { file>>b; file.close();}
else { cout<<"没有文件";}
cout<<"已有的读者个数:";
cout< reader *A=new reader[b];
ifstream is("reader.txt",ios_base::binary);
if(is)
{

for(i=0;i {
is.read((char *)&A[i],sizeof(A[i]));
}
}
else
{
cout<<"txt文件打开出错"< }
is.close();
cout<<"请输入你要查找读者的名字:";
cin>>a;
for(i=0;i {
if(strcmp(A[i].getname(),a)==0)
{
cout<<"编号 年龄 名字 借书情况 "< cout< h++;
}
}
if(h==0)cout<<"没有这个读者!";
}
*/

void Librarianland() //管理员登陆函数
{
int q,l=1,i,h;
system("cls");
system("color 2e");
cout<<" "< cout<<" "< cout<<" "< cout<<" "<<"现在进行身份确认,请输入口令:";
cin>>i;
while(l)
{
if(i==00000)
{
system("cls");
system("color 3e");
cout<<" "< cout<<" "< cout<<" "< cout<<" "<<"|*******" <<"0,返回上一级菜单."<<" ******|"< cout<<" "<<"|*******" <<"1,查询图书馆管理员的信息"<<" ******|"< cout<<" "<<"|*******" <<"2,存入新的管理员信息. "<<" ******|"< cout<<" "<<"|*******" <<"3,修改图书管理员的信息"<<" ******|"< cout<<" "<<"|*******" <<"4,新增图书"<<" ******|"< cout<<" "<<"|*******" <<"5,删除图书"<<" ******|"< cout<<"请选择执行的操作:";
cin>>q;
if(q>=0&&q<6)
{
switch(q)
{
case 0:
l=0;
break;
case 1:serchLaninf();
system("pause");
break;
case 2:storelibrarian();
system("pause");
case 3:reLaninf();
system("pause");
break;
case 4:
void aadbook();
aadbook();
break;
case 5:
void delate();
delate();
break;
default:
break;
}
}
else
{
system("cls");
system("color 4e");
cout<<" "< cout<<" "< cout<<" "< cout<<" "<<"操作无效!"< cout<<" "< cout<<" "< cout<<" "< system("pause");
}
}
else
{
system("cls");
system("color 2e");
cout<<" "< cout<<" "< cout<<" "< cout<<" "<<" 身份确认失败"< cout<<" "<<" 是否重新确认!"< cout<<" "<<"重新确认请按请按1,放弃请按0"< cout<<" ";
cin>>h;
if(h==0) l=0;
if(h==1)
{
system("cls");
system("color 2e");
cout<<" "< cout<<" "< cout<<" "< cout<<" "<<"请再次输入口令:";
cin>>i;
}
}

}
}

void studentland() //学生登陆函数
{
int h;
char l='y';
while(l=='y')
{
system("cls");
system("color 3e");
cout<<" "< cout<<" "< cout<<" "< cout<<" "< cout<<" "< cout<<" "< cout<<" "<<"****** "<<"|输出藏书信息,请按1 | "<<"******"< cout<<" "<<"****** "<<"|查询图书馆的总信息,请按2| "<<"******"< cout<<" "<<"****** "<<"|按书名查找图书,请按3 | "<<"******"< cout<<" "<<"****** "<<"|查询读者的信息,请按4 | "<<"******"< cout<<" "<<" ****** "<<"|返回上一级菜单,请按0 | "<<"******"< cout<<"请选择您将执行的操作:";
cin>>h;
if(h>=0&&h<5)
switch(h)
{
case 0:
l='n';
break;
case 1:outbookinf();
system("pause");
break;
case 2:libary();
system("pause");
break;
case 3:searchbook();
system("pause");
break;
case 4:seachreader();
system("pause");
break;
}
else
{
system("cls");
system("color 4e");
cout<<" "< cout<<" "< cout<<" "< cout<<" "<<"您的操作是无效的!!!!"< system("pause");
}
}
}

void main() // 主函数
{
int h,l=1;
while(l)
{
system("cls");
system("color 7c");
cout<<" "< cout<<" "< cout<<" "< cout<<" "<<"图书管理系统"< cout<<" "< cout<<" "< cout<<" ------------------"< cout<<" "<<"||管理员登陆 请按1||"< cout<<" "<<"||学生登陆 请按2 ||"< cout<<" "<<"||退出系统 请按0 ||"< cout<<" -------------------"< cout<<""< cout<<" 请您选择登陆方式:";
cin>>h;
if(h>=0&&h<3)
{
switch(h)
{
case 0:
cout<<"谢谢光临本图书馆!"< exit(0);
case 1:
Librarianland();
system("pause");
break;
case 2:
studentland();
system("pause");
break;
}
}
else
{
system("cls");
system("color 4e");
cout<<" "< cout<<" "< cout<<" "< cout<<" "<<"您的操作是无效的!!!!"< system("pause");
}
}
}
图书馆的管理系统。

回答(2):

// SnakeDlg.h : 头文件
//
#pragma once
#include "afxwin.h"
// CSnakeDlg 对话框
class CSnakeDlg : public CDialog
{
// 构造
public:
CSnakeDlg(CWnd* pParent = NULL); // 标准构造函数
// 对话框数据
enum { IDD = IDD_SNAKE_DIALOG };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 实现
protected:
HICON m_hIcon;
// 游戏场景数据
CRect m_ScrDeviceRect;
CSize m_ScrDeviceSize, m_ScrLogicalSize;
int m_iLevel, m_iScore;
bool m_bPause, m_bGameOver;
CPen m_penSnake, m_penScrBk;
CWinThread *m_pGameThread;
// “贪吃蛇”数据
CList m_SnakeBody;
int m_iLength, m_iCourse, m_iRealCourse;
CPoint m_ptFood;
// 自定义成员函数
void LPtoDP(CPoint &Point);
void DPtoLP(CPoint &Point);
void GameInit();
bool IsInScreen(CPoint &Point);
bool IsGameOver();
bool IsFoodOK();
static UINT GameThread(LPVOID pParam);
// 生成的消息映射函数
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnBnClickedButtonLevel();
afx_msg void OnBnClickedButtonStart();
afx_msg void OnBnClickedButtonPause();
afx_msg void OnBnClickedButtonExit();
DECLARE_MESSAGE_MAP()
virtual void OnOK();
virtual void OnCancel();
virtual void WinHelp(DWORD dwData, UINT nCmd = HELP_CONTEXT);
virtual BOOL PreTranslateMessage(MSG* pMsg);
};

[点击此处复制到剪贴板] [ - ]CODE:
// SnakeDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "Snake.h"
#include "SnakeDlg.h"
#include "LevelDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// 对话框数据
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 实现
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()
// CSnakeDlg 对话框
CSnakeDlg::CSnakeDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSnakeDlg::IDD, pParent)
, m_iLevel(1)
, m_iScore(0)
, m_bPause(false)
, m_iLength(3)
, m_iCourse(1)
, m_iRealCourse(1)
, m_bGameOver(true)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CSnakeDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//DDX_Control(pDX, IDC_SCREEN, m_Screen);
DDX_Text(pDX, IDC_TEXT_LEVEL, m_iLevel);
DDX_Text(pDX, IDC_TEXT_SCORE, m_iScore);
DDX_Text(pDX, IDC_TEXT_LENGTH, m_iLength);
}
BEGIN_MESSAGE_MAP(CSnakeDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_WM_CTLCOLOR()
ON_BN_CLICKED(IDC_BUTTON_EXIT, &CSnakeDlg::OnBnClickedButtonExit)
ON_BN_CLICKED(IDC_BUTTON_LEVEL, &CSnakeDlg::OnBnClickedButtonLevel)
ON_BN_CLICKED(IDC_BUTTON_START, &CSnakeDlg::OnBnClickedButtonStart)
ON_BN_CLICKED(IDC_BUTTON_PAUSE, &CSnakeDlg::OnBnClickedButtonPause)
END_MESSAGE_MAP()
// CSnakeDlg 消息处理程序
BOOL CSnakeDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 将“关于...”菜单项添加到系统菜单中。
// IDM_ABOUTBOX 必须在系统命令范围内。
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
GetDlgItem(IDC_SCREEN)->GetWindowRect(m_ScrDeviceRect);
m_ScrDeviceSize.cx=m_ScrDeviceRect.right-m_ScrDeviceRect.left;
m_ScrDeviceSize.cy=m_ScrDeviceRect.bottom-m_ScrDeviceRect.top;
#ifdef _DEBUG
afxDump<<"Screen Device Rect : "< afxDump<<"Screen Device Size : "<#endif // _DEBUG
m_ScrLogicalSize.cx=m_ScrDeviceSize.cx/10;
m_ScrLogicalSize.cy=m_ScrDeviceSize.cy/10;
#ifdef _DEBUG
afxDump<<"Screen Logical Size : "<#endif // _DEBUG
GetDlgItem(IDC_BUTTON_PAUSE)->EnableWindow(FALSE);
m_penSnake.CreatePen(PS_SOLID, 12, RGB(0, 0, 0));
m_penScrBk.CreatePen(PS_SOLID, 12, RGB(255, 255, 255));
GameInit();
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
void CSnakeDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。
void CSnakeDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast(dc.GetSafeHdc()), 0);
// 使图标在工作矩形中居中
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CClientDC DC(GetDlgItem(IDC_SCREEN));
CPen *pOldPen=DC.SelectObject(&m_penScrBk);
for (int i=1; i<=m_ScrLogicalSize.cx; i++)
for (int j=1; j<=m_ScrLogicalSize.cy; j++)
{
CPoint ptTemp(i, j);
LPtoDP(ptTemp);
DC.MoveTo(ptTemp);
DC.LineTo(ptTemp);
DPtoLP(ptTemp);
}
pOldPen=DC.SelectObject(&m_penSnake);
if (IsInScreen(m_SnakeBody.GetHead()))
{
LPtoDP(m_SnakeBody.GetHead());
DC.MoveTo(m_SnakeBody.GetHead());
DC.LineTo(m_SnakeBody.GetHead());
DPtoLP(m_SnakeBody.GetHead());
}
POSITION Pos=m_SnakeBody.GetHeadPosition();
m_SnakeBody.GetNext(Pos);
for (int i=1; i {
CPoint ptTemp=m_SnakeBody.GetNext(Pos);
LPtoDP(ptTemp);
DC.MoveTo(ptTemp);
DC.LineTo(ptTemp);
DPtoLP(ptTemp);
}
LPtoDP(m_ptFood);
DC.MoveTo(m_ptFood);
DC.LineTo(m_ptFood);
DPtoLP(m_ptFood);
DC.SelectObject(pOldPen);
CDialog::OnPaint();
}
}
//当用户拖动最小化窗口时系统调用此函数取得光标显示。
//
HCURSOR CSnakeDlg::OnQueryDragIcon()
{
return static_cast(m_hIcon);
}
void CSnakeDlg::OnOK()
{
// TODO: 在此添加专用代码和/或调用基类
}
void CSnakeDlg::OnCancel()
{
// TODO: 在此添加专用代码和/或调用基类
if (m_bPause) m_pGameThread->ResumeThread();
if (!m_bGameOver)
{
m_bGameOver=true;
::WaitForSingleObject(m_pGameThread->m_hThread, INFINITE);
}
CDialog::OnCancel();
}
void CSnakeDlg::WinHelp(DWORD dwData, UINT nCmd)
{
// TODO: 在此添加专用代码和/或调用基类
CAboutDlg Dlg;
Dlg.DoModal();
}
void CSnakeDlg::OnBnClickedButtonLevel()
{
// TODO: 在此添加控件通知处理程序代码
CLevelDlg Dlg;
if (Dlg.DoModal()==IDOK) m_iLevel=Dlg.m_iLevel;
UpdateData(FALSE);
}
void CSnakeDlg::OnBnClickedButtonStart()
{
// TODO: 在此添加控件通知处理程序代码
GetDlgItem(IDC_BUTTON_LEVEL)->EnableWindow(FALSE);
GetDlgItem(IDC_BUTTON_START)->EnableWindow(FALSE);
GetDlgItem(IDC_BUTTON_PAUSE)->EnableWindow(TRUE);
GetDlgItem(IDC_SCREEN)->SetFocus();
GameInit();
UpdateData(FALSE);
OnPaint();
m_bGameOver=false;
m_pGameThread=AfxBeginThread(GameThread, this);
}
void CSnakeDlg::OnBnClickedButtonPause()
{
// TODO: 在此添加控件通知处理程序代码
m_bPause=!m_bPause;
if (m_bPause)
{
m_pGameThread->SuspendThread();
GetDlgItem(IDC_BUTTON_PAUSE)->SetWindowText("继续游戏");
GetDlgItem(IDC_BUTTON_LEVEL)->EnableWindow(TRUE);
}
else
{
m_pGameThread->ResumeThread();
GetDlgItem(IDC_BUTTON_PAUSE)->SetWindowText("暂停游戏");
GetDlgItem(IDC_BUTTON_LEVEL)->EnableWindow(FALSE);
}
}
void CSnakeDlg::OnBnClickedButtonExit()
{
// TODO: 在此添加控件通知处理程序代码
OnCancel();
}
BOOL CSnakeDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: 在此添加专用代码和/或调用基类
if (pMsg->message==WM_KEYDOWN)
{
switch (toupper((int)pMsg->wParam))
{
case 'W':
case 38:
if (m_iRealCourse!=2) m_iCourse=4;
break;
case 'S':
case 40:
if (m_iRealCourse!=4) m_iCourse=2;
break;
case 'A':
case 37:
if (m_iRealCourse!=1) m_iCourse=3;
break;
case 'D':
case 39:
if (m_iRealCourse!=3) m_iCourse=1;
break;
}
}
return CDialog::PreTranslateMessage(pMsg);
}
// 自定义成员函数
void CSnakeDlg::DPtoLP(CPoint &Point)
{
Point.x=(Point.x+5)/10;
Point.y=(Point.y+5)/10;
}
void CSnakeDlg::LPtoDP(CPoint &Point)
{
Point.x=Point.x*10-5;
Point.y=Point.y*10-5;
}
void CSnakeDlg::GameInit()
{
m_iScore=0;
m_bPause=false;
m_iLength=3;
m_iCourse=1;
m_SnakeBody.RemoveAll();
for (int i=10; i>7; i--)
{
CPoint ptTemp(i, 10);
m_SnakeBody.AddTail(ptTemp);
}
m_ptFood.x=10000;
m_ptFood.y=10000;
}
bool CSnakeDlg::IsInScreen(CPoint &Point)
{
return 1<=m_SnakeBody.GetHead().x && m_SnakeBody.GetHead().x<=m_ScrLogicalSize.cx && 1<=m_SnakeBody.GetHead().y && m_SnakeBody.GetHead().y<=m_ScrLogicalSize.cy;
}
bool CSnakeDlg::IsGameOver()
{
if (m_SnakeBody.GetHead().x<1 || m_SnakeBody.GetHead().x>m_ScrLogicalSize.cx) return true;
if (m_SnakeBody.GetHead().y<1 || m_SnakeBody.GetHead().y>m_ScrLogicalSize.cy) return true;
POSITION Pos=m_SnakeBody.GetHeadPosition();
m_SnakeBody.GetNext(Pos);
for (int i=1; i if (m_SnakeBody.GetHead()==m_SnakeBody.GetNext(Pos)) return true;
return false;
}
bool CSnakeDlg::IsFoodOK()
{
POSITION Pos=m_SnakeBody.GetHeadPosition();
for (int i=0; i if (m_ptFood==m_SnakeBody.GetNext(Pos)) return false;
return true;
}
UINT CSnakeDlg::GameThread(LPVOID pParam)
{
CSnakeDlg *pDlg=(CSnakeDlg*)pParam;
CClientDC DC(pDlg->GetDlgItem(IDC_SCREEN));
bool bMakeFood=true, bGameOver=false;
srand((unsigned)time(NULL));
while (!bGameOver && !pDlg->m_bGameOver)
{
CPen *pOldPen;
::Sleep(500/pDlg->m_iLevel);
if (bMakeFood)
{
pOldPen=DC.SelectObject(&pDlg->m_penScrBk);
pDlg->LPtoDP(pDlg->m_ptFood);
DC.MoveTo(pDlg->m_ptFood);
DC.LineTo(pDlg->m_ptFood);
pDlg->DPtoLP(pDlg->m_ptFood);
DC.SelectObject(pOldPen);
do
{
pDlg->m_ptFood.x=rand()%pDlg->m_ScrLogicalSize.cx+1;
pDlg->m_ptFood.y=rand()%pDlg->m_ScrLogicalSize.cy+1;
} while(!pDlg->IsFoodOK());
pOldPen=DC.SelectObject(&pDlg->m_penSnake);
pDlg->LPtoDP(pDlg->m_ptFood);
DC.MoveTo(pDlg->m_ptFood);
DC.LineTo(pDlg->m_ptFood);
pDlg->DPtoLP(pDlg->m_ptFood);
DC.SelectObject(pOldPen);
bMakeFood=false;
}
CPoint ptTarg=pDlg->m_SnakeBody.GetHead(), ptPrev=pDlg->m_SnakeBody.GetTail();
pDlg->m_SnakeBody.RemoveTail();
switch (pDlg->m_iCourse)
{
case 1:
ptTarg.x++;
break;
case 2:
ptTarg.y++;
break;
case 3:
ptTarg.x--;
break;
case 4:
ptTarg.y--;
break;
}
pDlg->m_SnakeBody.AddHead(ptTarg);
pDlg->m_iRealCourse=pDlg->m_iCourse;
if (pDlg->m_SnakeBody.GetHead()!=pDlg->m_ptFood)
{
pOldPen=DC.SelectObject(&pDlg->m_penScrBk);
pDlg->LPtoDP(ptPrev);
DC.MoveTo(ptPrev);
DC.LineTo(ptPrev);
pDlg->DPtoLP(ptPrev);
DC.SelectObject(pOldPen);
}
else
{
pDlg->m_SnakeBody.AddTail(ptPrev);
pDlg->m_iLength=(int)pDlg->m_SnakeBody.GetSize();
CString sTemp;
sTemp.Format("%d", pDlg->m_iLength);
pDlg->GetDlgItem(IDC_TEXT_LENGTH)->SetWindowText(sTemp);
pDlg->m_iScore=pDlg->m_iScore+pDlg->m_iLevel;
sTemp.Format("%d", pDlg->m_iScore);
pDlg->GetDlgItem(IDC_TEXT_SCORE)->SetWindowText(sTemp);
pDlg->m_ptFood.x=10000;
pDlg->m_ptFood.y=10000;
bMakeFood=true;
}
if (!(bGameOver=pDlg->IsGameOver()))
{
pOldPen=DC.SelectObject(&pDlg->m_penSnake);
pDlg->LPtoDP(pDlg->m_SnakeBody.GetHead());
DC.MoveTo(pDlg->m_SnakeBody.GetHead());
DC.LineTo(pDlg->m_SnakeBody.GetHead());
pDlg->DPtoLP(pDlg->m_SnakeBody.GetHead());
DC.SelectObject(pOldPen);
}
pOldPen=DC.SelectObject(&pDlg->m_penSnake);
pDlg->LPtoDP(pDlg->m_ptFood);
DC.MoveTo(pDlg->m_ptFood);
DC.LineTo(pDlg->m_ptFood);
pDlg->DPtoLP(pDlg->m_ptFood);
DC.SelectObject(pOldPen);
}
if (!pDlg->m_bGameOver)
{
pDlg->GetDlgItem(IDC_BUTTON_LEVEL)->EnableWindow(TRUE);
pDlg->GetDlgItem(IDC_BUTTON_START)->EnableWindow(TRUE);
pDlg->GetDlgItem(IDC_BUTTON_PAUSE)->EnableWindow(FALSE);
pDlg->m_bGameOver=true;
AfxMessageBox("游戏已经结束 !");
}
return 0;
}

回答(3):

很长 很和谐

回答(4):

牛!

回答(5):

这个有难度啊!!!