ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

C语言打砖块小游戏

2020-10-24 23:00:34  阅读:211  来源: 互联网

标签:ball int broad window C语言 high length 小游戏 砖块


#include <stdio.h>
#include <graphics.h>
#include <conio.h>
#include <time.h>
#include <string.h>

#define window_length   500
#define window_high     500

#define map_length        (window_length/100)
#define map_high        (window_high/100)

#define broad_length    200
#define broad_high      20

#define ball_x            (window_length/2)
#define ball_y            (window_high/2)
#define ball_r          10
#define ball_top        (ball.y-ball.r)
#define ball_bot        (ball.y+ball.r)
#define ball_left        (ball.x-ball.r)
#define ball_right        (ball.x+ball.r)
#define ball_dx         (window_length/100)
#define ball_dy         (window_length/100)

#define brick_length    (window_length/(map_length))
#define brick_high        20

enum direction{ LEFT = VK_LEFT, RIGHT = VK_RIGHT};

int map[map_high][map_length] = { 0 };

struct{
    int x;
    int y;
    int r;
    int dx;
    int dy;
}ball = { ball_x,ball_y,ball_r,ball_dx,ball_dy};                                                                    //球的初始化

struct{
    int x;
    int y;
}broad = { window_length / 2 - broad_length / 2, window_high - broad_high};                                        //板子的初始化

void map_Checkpoint(){
    for (int y = 0; y < map_high; y++){
        for (int x = 0; x < map_length; x++){                            
            map[y][x] = 1;
        }
    }
}

int hit_bricks_top(){
    if (ball_top <= map_high*brick_high && ball_top%brick_high == 0 && map[ball_top/brick_high - 1][ball.x/brick_length] == 1)
    {
        map[ball_top / brick_high - 1][ball.x / brick_length] = 0;
        return 1;
    }
    return 0;
}

int hit_bricks_left(){
    if (ball_top <= map_high*brick_high && ball_left%brick_length == 0 && map[ball.y / brick_high + 1][ball.x / brick_length - 1] == 1 )
    {
        map[ball.y / brick_high + 1][ball.x / brick_length - 1] = 0;
        return 1;
    }
    return 0;
}

int hit_bricks_right(){
    if (ball_top <= map_high*brick_high && ball_right%brick_length == 0 && map[ball.y / brick_high + 1][ball.x / brick_length + 1] == 1)
    {
        map[ball.y / brick_high + 1][ball.x / brick_length + 1] = 0;
        return 1;
    }
    return 0;
}

void move_ball(){
    setfillcolor(RGB(64, 208, 88));
    fillcircle(ball.x,ball.y,ball.r);
    //小球碰撞上部
    if (ball_top == 0 || hit_bricks_top())
    {
        ball.dy = -ball.dy;
    }
    //小球碰撞下部
    else if (ball_bot == window_high-broad_high && ball.x >= broad.x &&ball.x <= broad.x+broad_length)
    {
        ball.dy = -ball.dy;
    }
    //小球碰撞左部
    else if (ball_left <= 0 || hit_bricks_left()){
        ball.dx = -ball.dx;
    }
    //小球碰撞右部
    else if (ball_right >= window_length || hit_bricks_right()){
        ball.dx = -ball.dx;
    }
    ball.x += ball.dx;
    ball.y += ball.dy;
}

void move_broad(){
    setfillcolor(RGB(23,18,237));
    fillrectangle(broad.x, broad.y, broad.x+broad_length, broad.y+broad_high);
    if (GetAsyncKeyState('A') || GetAsyncKeyState(LEFT)){
        if (broad.x > 0){
            broad.x -= 5;
        }
        else{
            broad.x = 0;
        }
    }
    if (GetAsyncKeyState('D') || GetAsyncKeyState(RIGHT)){
        if (broad.x < window_length - broad_length){
            broad.x += 5;
        }
        else{
            broad.x = window_length - broad_length;
        }
    }
}

void map_display(){
    for (int y = 0; y < map_high; y++){
        for (int x = 0; x < map_length; x++){
            switch (map[y][x]){
            case 0:
                setfillcolor(BLACK);
                solidrectangle(x*brick_length, y*brick_high, x*brick_length + brick_length, y*brick_high + brick_high);
                break;
            case 1:
                setfillcolor(RGB(170, 100, 110));
                fillrectangle(x*brick_length, y*brick_high, x*brick_length + brick_length, y*brick_high + brick_high);
                break;
            }
        }
    }
}

int win_judge(){
    if (ball_top > window_high){
        outtextxy(window_length / 2 - 30, window_high / 2, "Game over!");
        return 1;
    }
    int ret = 1;
    for (int y = 0; y < map_high; y++){
        for (int x = 0; x < map_length; x++){
            if (map[y][x] == 1){
                ret = 0;
                break;
            }
        }
    }
    if (ret == 1)
    {
        outtextxy(window_length / 2 - 30, window_high / 2, "666");
        return 2;
    }
    return 0;
}

void display(){
    setbkcolor(BLACK);
    cleardevice();
    map_Checkpoint();
    int ret = 1;
    while (ret){
        BeginBatchDraw();                            //开始 双缓冲
        cleardevice();
        map_display();
        move_broad();
        move_ball();
        win_judge();
        Sleep(20);
        EndBatchDraw();                                //结束 双缓冲
    }
}

int main(){
    srand((unsigned int)time(NULL));
    initgraph(window_length, window_high);
    display();
    closegraph();
    return 0;
}

----------------------------------------------------------------------------------------------------------------------

 

标签:ball,int,broad,window,C语言,high,length,小游戏,砖块
来源: https://www.cnblogs.com/hyby/p/13871484.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有