The first experimental report on curriculum design in spring 2019

Keywords: Python git

I. name of experiment project

Aircraft Wars

II. Functional description of the experimental project (in its own language)

1. Use the mouse to control the aircraft movement.
2. Launch bullets: press the left mouse button to launch bullets.
3. Add enemy aircraft: the enemy aircraft will automatically move downward and reappear after disappearing from the border,
4. Judge the victory or defeat: add points if the bullet hits the enemy aircraft, or the game fails if the enemy aircraft hits our aircraft, and increase the picture effect of the explosion of our aircraft.
5. Increase the sound effect: increase the background music, the sound effect of firing bullets, the sound effect of aircraft explosion, and the score reward sound effect.

III. introduction to project module structure (text + overall model)

#include<graphics.h>
#include<conio.h>
#include<math.h>
#include<stdio.h>

#pragma comment(lib,"Winmm.lib")

#define High 768 / / game screen size
#define Width 512

IMAGE img_bk;          //Background picture
int position_x,position_y;     //Location of aircraft
int bullet_x,bullet_y;         //The location of the bullet
float enemy_x,enemy_y;         //Location of enemy aircraft
IMAGE img_planeExplode1,img_planeExplode2; //Pictures of exploding aircraft
IMAGE img_planeNormal1,img_planeNormal2;   //Aircraft pictures
IMAGE img_bullet1,img_bullet2;             //Bullet pictures
IMAGE img_enemyPlane1,img_enemyPlane2;     //Enemy aircraft pictures
int isExpolde=0;                           //Does the plane explode
int score=0;                               //Score
void startup()
{
    mciSendString("open C:\\Users\\admin\\Desktop\\Aircraft war game material\\game_music.mp3 alia bkmusic",NULL,0,NULL);     //Turn on background music
    mciSendString("play bkmusic repeat",NULL,0,NULL);       //Loop Playback
    initgraph(Width,High);
    loadimage(&img_bk,"C:\\Users\\admin\\Desktop\\Aircraft war game material\\Map.jpg");
    loadimage(&img_planeNormal1,"C:\\Users\\admin\\Desktop\\Aircraft war game material\\Aircraft background.jpg");
    loadimage(&img_planeNormal2,"C:\\Users\\admin\\Desktop\\Aircraft war game material\\An airplane.jpg");
    loadimage(&img_bullet1,"C:\\Users\\admin\\Desktop\\Aircraft war game material\\Background of bullets.jpg");
    loadimage(&img_bullet2,"C:\\Users\\admin\\Desktop\\Aircraft war game material\\Bullet.jpg");
    loadimage(&img_enemyPlane1,"C:\\Users\\admin\\Desktop\\Aircraft war game material\\Background of enemy aircraft.jpg");
    loadimage(&img_enemyPlane2,"C:\\Users\\admin\\Desktop\\Aircraft war game material\\Enemy airplane.jpg");
    loadimage(&img_planeExplode1,"C:\\Users\\admin\\Desktop\\Aircraft war game material\\Background of aircraft explosion.jpg");
    loadimage(&img_planeExplode2,"C:\\Users\\admin\\Desktop\\Aircraft war game material\\Aircraft explosion.jpg");
    position_x=High*0.7;
    position_y=Width*0.5;
    bullet_x=position_x;
    bullet_y=-85;
    enemy_x=Width*0.5;
    enemy_y=10;
    BeginBatchDraw();
}

void show()                    //Show pictures and scores
{
    putimage(0,0,&img_bk);            //Display background
    if(isExpolde==0)
    {
    putimage(position_x-58,position_y-60,&img_planeNormal1,NOTSRCERASE);   //Display aircraft
    putimage(position_x-58,position_y-60,&img_planeNormal2,SRCINVERT);
    putimage(bullet_x-9.5,bullet_y,&img_bullet1,NOTSRCERASE);                //Display bullets
    putimage(bullet_x-9.5,bullet_y,&img_bullet2,SRCINVERT);
    putimage(enemy_x,enemy_y,&img_enemyPlane1,NOTSRCERASE);                //Display enemy planes
    putimage(enemy_x,enemy_y,&img_enemyPlane2,SRCINVERT);
    }
    else
    {
        putimage(position_x-41,position_y-43,&img_planeExplode1,NOTSRCERASE);
                                                                             //Show Blaster
        putimage(position_x-41,position_y-43,&img_planeExplode2,SRCINVERT);
    }
    outtextxy(Width*0.48,High*0.95,"Score:");                 //Display score
    char s[5];
    sprintf(s,"%d",score);
    outtextxy(Width*0.55,High*0.95,s);
    FlushBatchDraw();
    Sleep(1);
}

void updateWithoutInput()                 //Updates unrelated to user input
{
    if(isExpolde==0)
    {
    if(bullet_y>-25)
        bullet_y=bullet_y-3;
    if(enemy_y<High-25)
        enemy_y=enemy_y+0.5;
    else
        enemy_y=10;
    if(abs(bullet_x-enemy_x-52)+abs(bullet_y-enemy_y-74)<50)               //The bullet hit the enemy plane
    {
        enemy_x=rand()%Width;                                        //rand() is a random function
        enemy_y=-40;
        bullet_y=-85;
        mciSendString("close gemusic",NULL,0,NULL);            //Turn off the previous music first
        mciSendString("open C:\\Users\\admin\\Desktop\\Aircraft war game material\\gotEnemy.mp3 alias gemusic",NULL,0,NULL);
                                                                          //Turn on the music
        mciSendString("play gemusic",NULL,0,NULL);                        //Play once only
        score++;
        if(score>0&&score%5==0&&score%2!=0)
        {
            mciSendString("close 5music",NULL,0,NULL);                    //Turn off the previous music first
            mciSendString("open C:\\Users\\admin\\Desktop\\Aircraft war game material\\5.mp3 alias 5music",NULL,0,NULL);   //Turn on the music
            mciSendString("play 5music",NULL,0,NULL);                     //Play once only
        }
        if(score%10==0)
        {
            mciSendString("close 10music",NULL,0,NULL);                   //Turn off the previous music first
            mciSendString("open C:\\Users\\admin\\Desktop\\Aircraft war game material\\10.mp3 alias 10music",NULL,0,NULL);  //Turn on the music
            mciSendString("play 10music",NULL,0,NULL);                    //Play once only
        }
    }
    if(abs(position_x-enemy_x-52)+abs(position_y-enemy_y-74)<80)          //The enemy plane hit our plane
    {
        isExpolde=1;
        mciSendString("close exmusic",NULL,0,NULL);                   //Turn off the previous music first
        mciSendString("open C:\\Users\\admin\\Desktop\\Aircraft war game material\\explode.mp3 alias 10music",NULL,0,NULL);  //Turn on the music
        mciSendString("play exmusic",NULL,0,NULL);                    //Play once only
    }
}
}
void updateWithInput()                       //Updates related to user input
{
    if(isExpolde==0)
    {
    MOUSEMSG m;              //Define mouse message
    while(MouseHit())        //This function is used to detect whether there is a mouse message currently
    {
        m=GetMouseMsg();
        if(m.uMsg == WM_MOUSEMOVE)
        {
            //The position of the plane is equal to the position of the mouse
            position_x=m.x;
            position_y=m.y;
        }
        else if(m.uMsg == WM_LBUTTONDOWN)
        {
            //Press the left mouse button to launch the bullet
            bullet_x=position_x;
            bullet_y=position_y-43;
            mciSendString("close fgmusic",NULL,0,NULL);                   //Turn off the previous music first
            mciSendString("open C:\\Users\\admin\\Desktop\\Aircraft war game material\\f_gun.mp3 alias fgmusic",NULL,0,NULL);  //Turn on the music
            mciSendString("play fgmusic",NULL,0,NULL);                    //Play once only
        }
    }
}
}
void gameover()
{
    EndBatchDraw();
    getch();
    closegraph();
}

int main()
{
    startup();                 //Data initialization
    while(1)                   //Game cycle execution
    {                          
        show();                //display frame
        updateWithoutInput();    //Updates unrelated to user input
        updateWithInput();     //Updates related to user input
    }
    gameover();                //After the game, carry out follow-up processing
    return 0;
}

IV. interface display (screenshot)

V. code hosting link

https://gitee.com/heleiya/he_lei.git

Vi. experiment summary (put forward problems + Solutions + thoughts)

Why does the game get stuck every time before the sound effect?
Because it is to compile and run the game source code directly, try to build the game source code group into an exe program and run it. The problem is solved.
It's not easy to play a game. I need to study more carefully. Step by step, when the game runs perfectly, I still have a sense of achievement.

Posted by Platinumweaver on Sat, 02 Nov 2019 21:32:30 -0700