DEV Community

Stukdee_Gorye
Stukdee_Gorye

Posted on

c语言数组与链表实现角色克隆

0.背景

我需要在一个画布上通过鼠标左击添加一个方形,右击删除鼠标碰到的方形。

1.数组克隆

数组克隆的实现相对简单,但是数量有限。

#include "raylib.h"
#include <stdbool.h>
#define MAX_CUBE 500
int the_cube = 0;
typedef struct cube{
    Vector2 position;
    Color color;
    bool is_die;
}cube;
void cube_Update(cube *self);
int main(void){
    cube cubes[MAX_CUBE];
    InitWindow(800,800,"hello");
    SetTargetFPS(60);
    while(!WindowShouldClose()){
        if(IsMouseButtonDown(0) && the_cube < MAX_CUBE){
            cubes[the_cube].is_die = 0;
            cubes[the_cube].position.x = GetMousePosition().x;
            cubes[the_cube].position.y = GetMousePosition().y;
            cubes[the_cube].color = (Color){255,0,0,255};
            the_cube++;
        }
        for(int i = 0;i < the_cube;i++){
            cube_Update(&cubes[i]);
            if(cubes[i].is_die){
                cubes[i] = cubes[the_cube - 1];
                the_cube--;
                i--;
            }
        }
        BeginDrawing();
        ClearBackground((Color){255,255,255,255});
        for(int i = 0;i < the_cube;i++){
            DrawRectangle(cubes[i].position.x,cubes[i].position.y,50,50,cubes[i].color);
        }
        EndDrawing();
    }
    CloseWindow();
    return 0;
}
void cube_Update(cube *self){
    if(IsMouseButtonDown(1)){
        if(GetMousePosition().x > self -> position.x && 
        GetMousePosition().x < (self -> position.x + 50) && 
        GetMousePosition().y > self -> position.y &&
        GetMousePosition().y < self -> position.y + 50){
            self -> is_die = 1;
        }
    }
    return;
}
Enter fullscreen mode Exit fullscreen mode

注:raylib是一个图形库,系统通常不自带。
图片效果1

2.链表克隆

链表克隆的数量理论上是没有限制的,实际上取决于电脑。相对于数组,使用链表会更加灵活。

#include "raylib.h"
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
typedef struct cube{
    Vector2 position;
    Color color;
    bool is_die;
    struct cube *next;
}cube;
void cube_Update(cube *self);
int main(void){
    cube *head_cube;
    head_cube = NULL;
    InitWindow(800,800,"hello");
    SetTargetFPS(60);
    while(!WindowShouldClose()){
        if(IsMouseButtonDown(0)){
            cube *the_cube = (cube*)malloc(sizeof(cube));
            if(the_cube == NULL){
                exit(EXIT_FAILURE);
            }
            the_cube -> next = NULL;
            if(head_cube == NULL){
                head_cube = the_cube;
            }
            else{
                the_cube -> next = head_cube;
                head_cube = the_cube;
            }
            the_cube -> is_die = 0;
            the_cube -> position.x = GetMousePosition().x;
            the_cube -> position.y = GetMousePosition().y;
            the_cube -> color = (Color){255,0,0,255};
        }
        if(head_cube != NULL){
            cube *temp = head_cube;
            cube *pre_temp;
            while(temp != NULL){
                cube_Update(temp);
                if(temp -> is_die){
                    if(temp == head_cube){
                        head_cube = temp -> next;
                        free(temp);
                        temp = head_cube;
                        continue;
                    }
                    pre_temp -> next = temp -> next;
                    free(temp);
                    temp = pre_temp -> next;
                    continue;
                }
                pre_temp = temp;
                temp = temp -> next;
            }
        }
        BeginDrawing();
        ClearBackground((Color){255,255,255,255});
        if(head_cube != NULL){
            cube *temp = head_cube;
            while(temp != NULL){
                DrawRectangle(temp -> position.x,temp -> position.y,50,50,temp -> color);
                temp = temp -> next;
            }
        }
        EndDrawing();
    }
    CloseWindow();
    if(head_cube != NULL){
        cube *the_cube = head_cube;
        while(the_cube != NULL){
            cube *temp = the_cube;
            the_cube = the_cube -> next;
            free(temp);
        }
    }
    return 0;
}
void cube_Update(cube *self){
    if(IsMouseButtonDown(1)){
        if(GetMousePosition().x > self -> position.x && 
        GetMousePosition().x < (self -> position.x + 50) && 
        GetMousePosition().y > self -> position.y &&
        GetMousePosition().y < self -> position.y + 50){
            self -> is_die = 1;
        }
    }
    return;
}
Enter fullscreen mode Exit fullscreen mode

图片效果2

Top comments (0)