弹跳小球

jump_ball_1

# include <stdio.h>

# include <stdlib.h>
# include <conio.h>
# include <windows.h>
/*
弹跳小球:
画布 20*10
小球位置 10*5
画布 left top right bottom
速度 velocity_x velocity_y
*/
int main()
{
int ball_x = 0, ball_y = 5;
int left = 0, top = 0, right = 20, bottom = 10; //画布
int position_x = bottom / 2, position_y = right / 2; //小球的位置
int velocity_x = 1, velocity_y = 1;//速度

int bottom_black, right_black; //小球上面的空行和左面的空格

while (1)
{
ball_x = ball_x + velocity_x;
ball_y = ball_y + velocity_y;
system("cls");
for (bottom_black = 0; bottom_black <ball_x; bottom_black++)
printf("\n");
for (right_black = 0; right_black < ball_y; right_black++)
printf(" ");
printf("o");
/* 不能这样写的原因是因为小球的位置是不断变化的 
//画出小球
for (bottom_black = 0; bottom_black <ball_x ; bottom_black++)
{

for (right_black = 0; right_black < ball_y; right_black++)
{
if ((bottom_black == position_x) && (right_black == position_y))
printf("o");
else printf(" ");
}
printf("\n");
}*/

Sleep(50);


if ((ball_x == top) || (ball_x == bottom))
velocity_x = -velocity_x;
if ((ball_y == left) || (ball_y == right))
velocity_y = -velocity_y;
}
_getch();
return 0;

}


此程序来自学习知乎童星老师的作品,本人由于在一个不太好的学校,所以只能靠自学来度日。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python弹跳小球是一个简单的游戏,通过使用Python编程语言来模拟小球在不同条件下的弹跳行为。在这个游戏中,你可以控制小球的起始位置、速度和重力等参数,然后观察小球在屏幕上的弹跳运动。 要实现Python弹跳小球,你可以使用Pygame库来创建游戏窗口和处理游戏逻辑。以下是一个简单的实现步骤: 1. 导入Pygame库并初始化游戏: ```python import pygame pygame.init() ``` 2. 设置游戏窗口的大小和标题: ```python width = 800 height = 600 window = pygame.display.set_mode((width, height)) pygame.display.set_caption("Python弹跳小球") ``` 3. 定义小球的属性和初始状态: ```python ball_radius = 20 ball_color = (255, 0, 0) ball_x = width // 2 ball_y = height // 2 ball_speed_x = 5 ball_speed_y = 0 gravity = 0.5 ``` 4. 创建游戏主循环,处理事件和更新小球位置: ```python running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False ball_speed_y += gravity ball_x += ball_speed_x ball_y += ball_speed_y if ball_y + ball_radius > height: ball_speed_y = -ball_speed_y * 0.8 ball_y = height - ball_radius window.fill((255, 255, 255)) pygame.draw.circle(window, ball_color, (ball_x, ball_y), ball_radius) pygame.display.flip() ``` 5. 游戏结束后,退出Pygame: ```python pygame.quit() ``` 这只是一个简单的示例,你可以根据自己的需求和想法来扩展和改进这个游戏。希望对你有帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值