c模擬源碼
創建自己的植物大戰僵屍項目,作為初學者的項目,非常值得期待,不僅能夠提升個人的項目開發能力,還能作為課程設計的一部分。此項目詳細步驟如下:
項目准備階段,使用Visual Studio 創建空項目模板,導入所需素材,並在項目目錄下創建「res」文件夾,將解壓後的素材文件復制至其中。
實現游戲初始場景,代碼需配合視頻講解,具體實現細節請回復「代碼講解」。
添加啟動菜單,創建菜單界面,確保在main函數中調用菜單,實現游戲的初步啟動。
生成陽光機制,植物大戰僵屍中種植植物需要陽光值,通過隨機降落陽光或種植向日葵自動生產陽光實現。定義陽光結構體,使用圖片幀數組模擬旋轉效果,初始化陽光幀數組,創建並更新陽光位置與幀序號。在updateGame函數中調用創建與更新陽光狀態的函數,並在updateWindow函數中渲染陽光。
收集陽光功能,用戶點擊陽光球時,收集陽光並增加當前總陽光值,設置全局變數表示總陽光值,初始化值,並在用戶點擊處理中調用收集陽光的函數。在gameInit初始化中設置字體,更新window中繪制陽光值。
僵屍生成機制,創建僵屍數據模型,初始化僵屍數組與序列幀圖片數組,實現僵屍的創建與更新數據。在updateGame函數中創建僵屍並更新數據,並創建繪制僵屍的介面。在updateWindow函數中繪制僵屍。
陽光球的飛躍效果,實現陽光被點擊後自動飛向左上角,增加陽光值。給陽光結構體添加偏移量成員,設置陽光飛躍過程中的偏移量,修改渲染判斷條件。
豌豆發射功能,僵屍靠近時植物自動發射豌豆子彈。定義子彈數據類型,初始化子彈池與圖片,更新僵屍的「行」成員,實現豌豆發射並更新子彈位置。在updateGame函數中發射子彈並更新位置,在updateWindow函數中繪制子彈。
子彈與僵屍碰撞檢測,子彈碰到僵屍後爆炸並顯示效果,給僵屍添加血量成員與爆炸狀態處理。初始化子彈幀圖片數組,更新子彈爆炸狀態與幀序號。在updateGame函數中檢測碰撞,渲染子彈爆炸效果。
僵屍死亡機制,豌豆子彈擊中僵屍後,減少血量直至死亡,僵屍狀態變為黑沙。給僵屍添加死亡狀態成員,初始化死亡狀態圖片幀數組,更新僵屍狀態與繪制死亡狀態。後續實現細節,點擊鏈接查看完整實現。
B. 如何用c語言編寫QQ聊天程序(源代碼)
1、首先,我們編寫C語言的頭文件#include <stdio.h>。
C. C語言編程——發牌洗牌模擬,求幫助
實現了2副牌的發牌,和每個人的牌和底牌
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
struct CARD //牌
{
char suit[10]; /*花色*/
char face[10]; /*牌面*/
};
enum { posA, posB, posC, posD};//定義好每個人的位置
struct Postion
{
struct CARD getcard[25];//每人獲得的牌
};
struct Postion postion[4];//分配四個位置
struct CARD leftCard[8]; //底牌
struct CARD card[54]; //54張牌
char *suit[]={"Spades","Hearts","Clubs","Diamonds"};
char *face[] = {"A","2","3","4","5","6","7","8","9",
"10","jack","Queen","King"};
/* 函數功能:將52張牌的順序打亂,
函數參數:結構體數組wCard,表示52張牌
函數返回值:無
*/
void Shuffle(struct CARD *wCard)
{
int i,j;
struct CARD temp;
for (i=0; i<54; i++)
{
j=rand()%54;
temp=wCard[i];
wCard[i]=wCard[j];
wCard[j]=temp;
}
}
/*函數功能:發牌結果
函數參數:結構體數組wCard,表示有54張牌
函數返回值:無
*/
void Deal(struct CARD *wCard)
{
int i,aidx=0,bidx=0,cidx=0,didx=0;
Shuffle(card);//將牌打亂
/*************發第一副牌,只發50張,分別分給A,B,C,D四個位置 4張留底**************/
// 第一次發完50張後,A,B多一張,所以下面第二次讓C,D排在前面,兩次發完剛好各40張 */
for (i=0; i<50; i++)//發牌數
{
// printf("%10s %5s\n", wCard[i].suit, wCard[i].face);
if(i%4==0)
postion[posA].getcard[aidx++]=wCard[i];
else if(i%4==1)
postion[posB].getcard[bidx++]=wCard[i];
else if(i%4==2)
postion[posC].getcard[cidx++]=wCard[i];
else if(i%4==3)
postion[posD].getcard[didx++]=wCard[i];
}
/**********剩下的四張作為底牌*********/
leftCard[0]=wCard[i++];
leftCard[1]=wCard[i++];
leftCard[2]=wCard[i++];
leftCard[3]=wCard[i++];
Shuffle(card);//再次將牌打亂
/*************發第二副牌,也只發50張,分別分給A,B,C,D四個位置,4張留底,一共8張底**************/
for (i=0; i<50; i++)//發牌數
{
// printf("%10s %5s\n", wCard[i].suit, wCard[i].face);
if(i%4==0)
postion[posC].getcard[cidx++]=wCard[i];
else if(i%4==1)
postion[posD].getcard[didx++]=wCard[i];
else if(i%4==2)
postion[posA].getcard[aidx++]=wCard[i];
else if(i%4==3)
postion[posB].getcard[bidx++]=wCard[i];
}
/**********剩下的四張作為底牌,這樣就一共為8張底牌*********/
leftCard[4]=wCard[i++];
leftCard[5]=wCard[i++];
leftCard[6]=wCard[i++];
leftCard[7]=wCard[i++];
}
/* 函數功能:將52張牌按黑桃、紅桃、草花、方塊花色順序,面值按A~K順序排列
函數參數:結構體數組wCard,表示不同花色和面值的52張牌
指針數組wFace,指向面值字元串
指針數組wSuit,指向花色字元串
函數返回值:無
*/
void FillCard(struct CARD wCard[],char *wSuit[], char *wFace[])
{
int i;
for (i=0; i<52; i++)
{
strcpy(wCard[i].suit, wSuit[i/13]);
strcpy(wCard[i].face, wFace[i%13]);
}
// wCard[53].face="Big"; //大小王
strcpy(wCard[52].suit, "Small");
strcpy(wCard[52].face, "ghost");
strcpy(wCard[53].suit, "Big");
strcpy(wCard[53].face, "ghost");
}
void print(char ch)//輸出牌
{
int i;
switch(ch)
{
case 'A': for(i=0; i<25; i++)
{
printf("%10s %5s\n", postion[posA].getcard[i].suit, postion[posA].getcard[i].face);
}
break;
case 'B': for(i=0; i<25; i++)
{
printf("%10s %5s\n", postion[posB].getcard[i].suit, postion[posB].getcard[i].face);
}
break;
case 'C': for(i=0; i<25; i++)
{
printf("%10s %5s\n", postion[posC].getcard[i].suit, postion[posC].getcard[i].face);
}
break;
case 'D': for(i=0; i<25; i++)
{
printf("%10s %5s\n", postion[posD].getcard[i].suit, postion[posD].getcard[i].face);
}
break;
}
}
void outputLeftCard()//輸出底牌
{
int i;
for(i=0; i<8; i++)
printf("%10s %5s\n", leftCard[i].suit, leftCard[i].face);
}
int main()
{
char pos;
srand(time(NULL));
FillCard(card,suit,face);
//Shuffle(card);
Deal(card);
printf("Please choose your position(A、B、C、D):");
scanf("%c", &pos);
print(pos);//輸出你所在位置的牌
/**********下面輸出的是,除了你之外其他人的牌**********/
if(pos !='A')
{
printf("A:\n");
print('A');
}
if(pos !='B')
{
printf("B:\n");
print('B');
}
if(pos !='C')
{
printf("C:\n");
print('C');
}
if(pos !='D')
{
printf("D:\n");
print('D');
}
printf("底牌為:\n");
outputLeftCard();//輸出底牌
return 0;
}
D. C語言簡易文字冒險游戲源代碼
記憶游戲
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<windows.h>
#defineN10
intmain()
{inti,k,n,a[N],b[N],f=0;
srand(time(NULL));
printf("按1開始 按0退出:_");
scanf("%d",&n);
system("cls");
while(n!=0)
{for(k=0;k<N;k++)a[k]=rand()%N;
printf(" [請您牢記看到顏色的順序] ");
for(k=0;k<N;k++)
{switch(a[k])
{case0:system("color90");printf("0:淡藍色 ");break;//淡藍色
case1:system("colorf0");printf("1:白色 ");break;//白色
case2:system("colorc0");printf("2:淡紅色 ");break;//淡紅色
case3:system("colord0");printf("3:淡紫色 ");break;//淡紫色
case4:system("color80");printf("4:灰色 ");break;//灰色
case5:system("colore0");printf("5:黃色 ");break;//黃色
case6:system("color10");printf("6:藍色 ");break;//藍色
case7:system("color20");printf("7:綠色 ");break;//綠色
case8:system("color30");printf("8:淺綠色 ");break;//淺綠色
case9:system("color40");printf("9:紅色 ");break;//紅色
}
Sleep(1500);
system("colorf");//單個控制文字顏色
Sleep(100);
}
system("cls");
printf("0:淡藍色,1:白色,2:淡紅色,3:淡紫色,4:灰色,5:黃色,6:藍色7:綠色,8:淺綠色,9:紅色 ");
printf(" 請輸入顏色的順序:");
for(k=0;k<N;k++)scanf("%d",&b[k]);
for(k=0;k<N;k++)if(a[k]==b[k])f++;
if(f==0)printf("你的記憶弱爆了0 ");
elseif(f==1)printf("你的記憶有點弱1 ");
elseif(f<5)printf("你的記憶一般<5 ");
elseprintf("你的記憶力很強! ");
Sleep(2000);
system("cls");
printf(" 按0退出 按任意鍵繼續游戲: ");
scanf("%d",&n);
system("cls");
}
return0;
}
註:DEVc++運行通過,每輸入一個數字要加入一個空格。
E. 求二分法查找演示C語言源代碼
二分法查找演算法:
1.主要思想是:假設數據是按升序排序的,對於給定值x,從序列的中間位置開始比較,如果當前位置值等於x,則查找成功;若x小於當前位置值,則在數列的前半段 中查找;若x大於當前位置值則在數列的後半段中繼續查找,直到找到為止。
2. 時間復雜度: O(log2n)。
3. C語言源代碼(小例子)
search函數即為核心代碼:遞歸查找
#include<stdio.h>
intsearch(int*a,intnum,intlow,inthigh)
{
intmid=(low+high)/2;
if(low<=high)
{
if(num<a[mid])
returnsearch(a,num,low,mid-1);//加return
if(num>a[mid])
returnsearch(a,num,mid+1,high);//加return
if(num==a[mid])
return1;
}
else
return0;
}
intmain(){
inta[11]={0,1,2,3,4,5,9,11,12,13,15};
if(search(a,11,0,10)==1)
printf("success!!");
else
printf("failed!!");
}
F. 求用C語言模擬簡單檯球運動的源代碼,不需要圖形化界面
這源代碼應該有個桌面類(Table),球類(Sphere),游戲類等等。我用C++
#pragma once (Table.h)
#endif // _MSC_VER > 1000
#include "Base.h"
#define MESH_D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX1)
class CTable:public CBase
{
public:
DWORD Render();
CTable(LPDIRECT3DDEVICE8 pD3DDevice,LPSTR pFilename);
virtual ~CTable();
LPD3DXMESH GetMeshTablePointer();
private:
void TransformTable();
LPDIRECT3DDEVICE8 m_pD3DDevice;
DWORD m_dwNumMaterials;
LPD3DXMESH m_pMeshTable;
D3DMATERIAL8 *m_pMeshTableMaterials;
LPDIRECT3DTEXTURE8 *m_pMeshTableTextures;
};#endif
#include "Table.h" (Table.cpp)
CTable::CTable(LPDIRECT3DDEVICE8 pD3DDevice,LPSTR pFilename)
{
LPD3DXBUFFER pMaterialsBuffer=NULL;
LPD3DXMESH pMeshTable=NULL;
m_pD3DDevice=pD3DDevice;
if(FAILED(D3DXLoadMeshFromX(pFilename,D3DXMESH_MANAGED,m_pD3DDevice,NULL,
&pMaterialsBuffer,&m_dwNumMaterials,&pMeshTable)))
{
m_pMeshTable=NULL;
m_pMeshTableMaterials=NULL;
m_pMeshTableTextures=NULL;
LogError("<li>Table Mesh '%s' failed to load",pFilename);
return;
}
D3DXMATERIAL *matMaterials=(D3DXMATERIAL*)pMaterialsBuffer->GetBufferPointer();
//Create two arrays. One to hold the materials and one to hold the textures
m_pMeshTableMaterials=new D3DMATERIAL8[m_dwNumMaterials];
m_pMeshTableTextures=new LPDIRECT3DTEXTURE8[m_dwNumMaterials];
for(DWORD i=0;i<m_dwNumMaterials;i++)
{
//Copy the material
m_pMeshTableMaterials[i]=matMaterials[i].MatD3D;
//Set the ambient color for the material(D3DX does not do this)
m_pMeshTableMaterials[i].Ambient=m_pMeshTableMaterials[i].Diffuse;
D3DCOLORVALUE rgbaSpecular={0.0f,0.0f,0.0f,0.0f};
m_pMeshTableMaterials[i].Specular=rgbaSpecular;
m_pMeshTableMaterials[i].Power=50.0f;
//Create the texture
char buffer[255];
sprintf(buffer,"textures/%s",matMaterials[i].pTextureFilename);
if(FAILED(D3DXCreateTextureFromFile(m_pD3DDevice,
buffer, &m_pMeshTableTextures[i])))
{
m_pMeshTableTextures[i]=NULL;
}
}
//finished with the material buffer,so release it
SafeRelease(pMaterialsBuffer);
//Make sure that the normals are setup for mesh
pMeshTable->CloneMeshFVF(D3DXMESH_MANAGED,MESH_D3DFVF_CUSTOMVERTEX,m_pD3DDevice,&m_pMeshTable);
SafeRelease(pMeshTable);
// D3DXComputeNormals(m_pMesh);
LogInfo("<li>Mesh '%s' loaded OK",pFilename);
}
CTable::~CTable()
{
SafeDelete(m_pMeshTableMaterials);
if(m_pMeshTableTextures != NULL)
{
for(DWORD i=0;i<m_dwNumMaterials;i++)
{
if(m_pMeshTableTextures[i])
SafeRelease(m_pMeshTableTextures[i]);
}
}
SafeDelete(m_pMeshTableTextures);
SafeRelease(m_pMeshTable);
LogInfo("<li>Table Mesh destroyed OK");
}
DWORD CTable::Render()
{
TransformTable();
if(m_pMeshTable!=NULL)
{
for(DWORD i=0;i<m_dwNumMaterials;i++)
{
m_pD3DDevice->SetMaterial(&m_pMeshTableMaterials[i]);
m_pD3DDevice->SetTexture(0,m_pMeshTableTextures[i]);
m_pMeshTable->DrawSubset(i);
}
return m_pMeshTable->GetNumFaces();
}
else
return 0;
}
LPD3DXMESH CTable::GetMeshTablePointer()
{
return m_pMeshTable;
}
void CTable::TransformTable()
{
D3DXMATRIX matWorld;
D3DXMatrixTranslation(&matWorld,0,0,0);
m_pD3DDevice->SetTransform(D3DTS_WORLD,&matWorld);
}
(Sphere.h)
#if !defined (AFX_SPHERE_H__FC705F3B_568E_4973_B608_B8F7700D9ECE__INCLUDED_)
#define AFX_SPHERE_H__FC705F3B_568E_4973_B608_B8F7700D9ECE__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "Base.h"
#define SPHERE_D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX1)
class CSphere:public CBase
{
private:
struct SPHERE_CUSTOMVERTEX
{
float x,y,z; //Position of vertex in 3D space
float nx,ny,nz; //Lighting Normal
float tu,tv; //Texture coordinates
};
struct SPHERE_STATE
{
D3DXVECTOR3 sVector; //Position of Centigram in 3D space
D3DXVECTOR3 vVector; //Direction of Velocity in 3D space
float v; //Speed of Sphere
};
SPHERE_STATE *m_pSphereState;
D3DXVECTOR3 m_vecSavePosition; //Save sphere position for collision bar
D3DXVECTOR3 m_vecSavePosition2; //Save sphere position for collision sphere
public:
BOOL SetMaterial(D3DCOLORVALUE rgbaDiffuse,D3DCOLORVALUE rgbaAmbient,
D3DCOLORVALUE rgbaSpecular,D3DCOLORVALUE rgbaEmissive,float rPower);
BOOL SetTexture(const char* szTextureFilePath);
DWORD Render();
CSphere(LPDIRECT3DDEVICE8 pD3DDevice,int iRings=20,int iSegments=20);
void MoveSphere();
void MoveSphereForUser(float x,float z);
virtual ~CSphere();
inline void SetSpherePosition(float x,float y,float z)
{
m_pSphereState->sVector.x=x;
m_pSphereState->sVector.y=y;
m_pSphereState->sVector.z=z;
};
inline void GetSpherePosition(D3DXVECTOR3 &vecSpherePos)
{
vecSpherePos=m_pSphereState->sVector;
};
inline void GetSavedSpherePosition(D3DXVECTOR3 &vecSavedSpherePos)
{
vecSavedSpherePos=m_vecSavePosition;
};
inline void GetSavedSpherePosition2(D3DXVECTOR3 &vecSavedSpherePos)
{
vecSavedSpherePos=m_vecSavePosition2;
};
inline void SaveSpherePosition()
{
m_vecSavePosition=m_pSphereState->sVector;
};
inline void SaveSpherePosition2()
{
m_vecSavePosition2=m_pSphereState->sVector;
};
inline void ContradictoryZv()
{
m_pSphereState->vVector.z=-m_pSphereState->vVector.z;
};
inline void ContradictoryXv()
{
m_pSphereState->vVector.x=-m_pSphereState->vVector.x;
};
void MirrorVAoubtAxis(D3DXVECTOR3 &n);
inline void ReceSphereVelocity(float percent)
{
m_pSphereState->v=m_pSphereState->v*percent;
};
inline float CheckSphereEnergy()
{
return m_pSphereState->v;
};
inline void SetSphereVelocityDir(const D3DXVECTOR3 &vDir)
{
m_pSphereState->vVector=vDir;
};
inline void SetSphereVelocity(const float &velocity)
{
m_pSphereState->v=velocity;
};
inline void GetSphereVelocityDir(D3DXVECTOR3 &vDir)
{
vDir=m_pSphereState->vVector;
};
inline float GetSphereVelocity()
{
return m_pSphereState->v;
};
inline void SetSphereStateToFalse()
{
m_bSphereInUse=FALSE;
};
inline void SetSphereStateToTrue()
{
m_bSphereInUse=TRUE;
};
inline BOOL GetSphereState()
{
return m_bSphereInUse;
};
void SetSphereVelocityAt_Y_NegativeAxis();
inline float GetSpherePosAt_Y_Axis()
{
return m_pSphereState->sVector.y;
};
private:
BOOL CreateIndexBuffer();
BOOL UpdateVertices();
BOOL CreateVertexBuffer();
void TransformSphere();
void TransformSphereForUser();
void UpdateSpherePosition();
void FrictionReseVelocity();
LPDIRECT3DDEVICE8 m_pD3DDevice;
LPDIRECT3DVERTEXBUFFER8 m_pVertexBuffer;
LPDIRECT3DTEXTURE8 m_pTexture;
D3DMATERIAL8 m_matMaterial;
LPDIRECT3DINDEXBUFFER8 m_pIndexBuffer;
int m_iRings;
int m_iSegments;
float m_fTotalDis;
D3DXVECTOR3 m_vecSphereRotationAxis;
BOOL m_bSphereInUse;
DWORD m_dwNumOfVertices;
DWORD m_dwNumOfIndices;
DWORD m_dwNumOfPolygons;
};#endif