球钟代码

#include<stdio.h>

#include<stdlib.h>

#define ONEMIN 5

#define FIVEMIN 12

#define ONEHOUR 12

#define BALLQUE 28

#define OK 1

#define ERROR 0

typedef int data_t;

typedef struct

{

data_t *data;

int top;//栈顶

int maxlen;

}SqStack;

typedef struct

{

data_t *data;

int front;//队头位置

int rear;//队尾位置

int maxlen;

}SqQueue;

/*************************

*队列部分

**************************/

SqQueue *CreateEmptyQueue(int length)//创建队列

{

SqQueue *sq = (SqQueue*)malloc(sizeof(SqQueue));

if(sq==NULL)

{

printf("CreateEmptyQueue Error\n");

return NULL;

}

sq->data = (data_t*)malloc(sizeof(data_t)*length);

sq->maxlen=length;

sq->front=0;

sq->rear=0;

return sq;

}

int EmptyQueue(SqQueue *Q)//判断队是否为空

{

if(Q==NULL)

{

printf("EmptyQueue Error\n");

return -1;

}

if(Q->rear==Q->front)

return OK;

else

return ERROR;

}

int FullQueue(SqQueue *Q)//判断队是否已满

{

if(Q==NULL)

{

printf("EmptyQueue Error\n");

return -1;

}

if((Q->rear+1)%Q->maxlen==Q->front)

return OK;

else

return ERROR;

}

int EnQueue(SqQueue *Q,data_t e)

{

if(FullQueue(Q)==OK)

{

printf("Queue is Full\n");

return ERROR;

}

Q->data[Q->rear]=e;

Q->rear=(Q->rear+1)%Q->maxlen;

return OK;

}

int DeQueue(SqQueue *Q,data_t *e)

{

if(EmptyQueue(Q)==OK)

{

printf("Queue is Empty\n");

return ERROR;

}

*e=Q->data[Q->front];

Q->front=(Q->front+1)%Q->maxlen;

return OK;

}

/*************************

*栈部分

**************************/

int PushStack(SqStack *s,data_t e)//压栈

{

if(s->top==s->maxlen-1)

{

printf("Stack is Full\n");

return ERROR;

}

s->top++;

s->data[s->top]=e;

return OK;

}

 

data_t PopStack(SqStack *s)//弹栈

{

if(s->top==-1)

{

printf("Stack is Empty\n");

return ERROR;

}

data_t e=s->data[s->top];

s->top--;

return e;

}

SqStack *CreateEmptyStack(int length)//创建栈

{

    SqStack *stack = (SqStack*)malloc(sizeof(SqStack));

    if(stack==NULL)

    {

        printf("CreateEmptyStack Error\n");

        exit(0);

    }

stack->data = (data_t*)malloc(sizeof(data_t)*length);

stack->maxlen=length;

    stack->top=-1;

    return stack;

}

int EmptyStack(SqStack *s)//判断栈是否是空栈

{

    return -1==s->top?OK:ERROR;

}

int FullStack(SqStack *s)//判断栈是否是满栈

{

    return s->maxlen-1==s->top?OK:ERROR;

}

 

void ShowTime(SqStack *one_min,SqStack *five_min,SqStack *one_hour)//计算球钟内3个栈的小球所代表的时间并打印

{

int hour,minute;

minute=(one_min->top+1)+(five_min->top+1)*5;

hour=one_hour->top+1;

printf("time: %d:%d\n",hour,minute);

}

int main()

{

SqStack *one_min = CreateEmptyStack(ONEMIN);//1分钟栈

SqStack *five_min = CreateEmptyStack(FIVEMIN);//5分钟栈

SqStack *one_hour = CreateEmptyStack(ONEHOUR);//1小时栈

SqQueue *ballque = CreateEmptyQueue(BALLQUE);//球队列

int i;

data_t data;

for(i=1;i<=ballque->maxlen-1;i++)

{

EnQueue(ballque,i);

}

while(1)

{

DeQueue(ballque,&data);//出队一个球

PushStack(one_min,data);//压入1分钟栈中

if(FullStack(one_min)==OK)//如果1分钟栈已满

{

for(i=0;i<one_min->maxlen-1;i++)//弹出4个元素到队列中

{

EnQueue(ballque,PopStack(one_min));

}

PushStack(five_min,PopStack(one_min));//第5个压入5分钟栈

}

if(FullStack(five_min)==OK)//如果5分钟栈已满

{

for(i=0;i<five_min->maxlen-1;i++)//弹出11个元素到队列中

{

EnQueue(ballque,PopStack(five_min));

}

PushStack(one_hour,PopStack(five_min));//第12个元素压入1小时栈

}

if(FullStack(one_hour)==OK)//如果1小时栈已满

{

for(i=0;i<one_hour->maxlen;i++)//弹出12个元素到队列中

{

EnQueue(ballque,PopStack(one_hour));

}

}

ShowTime(one_min,five_min,one_hour);//打印时间

sleep(1);//延时1秒

}

return 0;

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是一个简单的Python球球游戏代码: ```python import pygame import random # 初始化 Pygame pygame.init() # 游戏窗口大小 screen_width = 800 screen_height = 600 # 创建游戏窗口 screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption("球球游戏") # 设置游戏时钟 clock = pygame.time.Clock() # 定义颜色 white = (255, 255, 255) black = (0, 0, 0) red = (255, 0, 0) # 定义球类 class Ball: def __init__(self, x, y, radius, color): self.x = x self.y = y self.radius = radius self.color = color self.speed_x = random.randint(-5, 5) self.speed_y = random.randint(-5, 5) def move(self): self.x += self.speed_x self.y += self.speed_y # 碰到边界反弹 if self.x - self.radius < 0 or self.x + self.radius > screen_width: self.speed_x *= -1 if self.y - self.radius < 0 or self.y + self.radius > screen_height: self.speed_y *= -1 def draw(self): pygame.draw.circle(screen, self.color, (self.x, self.y), self.radius) # 创建球列表 balls = [] for i in range(10): x = random.randint(50, screen_width - 50) y = random.randint(50, screen_height - 50) radius = random.randint(20, 50) color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) ball = Ball(x, y, radius, color) balls.append(ball) # 游戏循环 game_over = False while not game_over: # 处理事件 for event in pygame.event.get(): if event.type == pygame.QUIT: game_over = True # 移动球 for ball in balls: ball.move() # 检测球的碰撞 for i in range(len(balls)): for j in range(i + 1, len(balls)): dx = balls[i].x - balls[j].x dy = balls[i].y - balls[j].y distance = ((dx ** 2) + (dy ** 2)) ** 0.5 if distance < balls[i].radius + balls[j].radius: balls[i].speed_x *= -1 balls[i].speed_y *= -1 balls[j].speed_x *= -1 balls[j].speed_y *= -1 # 绘制背景 screen.fill(white) # 绘制球 for ball in balls: ball.draw() # 更新屏幕 pygame.display.update() # 设置帧率 clock.tick(60) # 退出 Pygame pygame.quit() ``` 这个游戏会生成多个随机大小和颜色的球,球会在屏幕中移动,并且会相互碰撞。当球碰撞时,它们会反弹。游戏会一直运行直到用户关闭游戏窗口。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值