Moon-Buggy的代码

时间: 2024-01-13 13:02:43 浏览: 33
以下是 Moon-Buggy 的代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <signal.h> #include <curses.h> #include <time.h> #define VERSION "1.0" #define MAX(x,y) ((x) > (y) ? (x) : (y)) #define MIN(x,y) ((x) < (y) ? (x) : (y)) #define WIDTH 64 #define HEIGHT 16 #define PLAYER_POS_X 4 #define PLAYER_POS_Y (HEIGHT-3) #define DISTANCE_PER_FRAME 8 #define OBSTACLE_PERIOD 8 #define JUMP_DURATION 12 #define JUMP_HEIGHT 3 #define GRAVITY 1 #define SPRITE_GAP 2 static char player_up[] = {'O',0,'-','^',0,'|',0,0,'|',0,'^',0,'-',0,'O',0}; static char player_down[] = {'O',0,'-','v',0,'|',0,0,'|',0,'v',0,'-',0,'O',0}; static char obstacle1[] = {'*',' ',' ',0,'*','*',0,'*',' ',0,'*','*',0,'*',' ',0,'*',0}; static char obstacle2[] = {'*',0,0,'*',' ',0,'*','*',0,'*','*',0,'*',' ',0,'*',0,0}; static char *obstacles[] = {obstacle1, obstacle2}; static int sprite_widths[] = {16, 17}; static int obstacle_probabilities[] = {60, 40}; static int obstacle_y[] = {HEIGHT-3, HEIGHT-4}; static char *title[] = { "", " Moon Buggy", "", "", "", " Press SPACE to start", " Press Q to quit", "", " Version " VERSION, "", "", "", "", "", "", NULL }; static char *gameover[] = { "", "", "", " GAME OVER", "", "", "", " Press SPACE to restart", " Press Q to quit", "", "", "", "", "", "", NULL }; static char *score_prefix[] = { "SCORE: ", NULL }; static char score[16]; static int obstacle_pos = WIDTH-1; static int obstacle_type = 0; static int obstacle_speed = 1; static int obstacle_visible = 0; static int player_pos_y = PLAYER_POS_Y; static int player_speed_y = 0; static int player_jump_remaining = 0; static int distance = 0; static int score_length; static int frame_count = 0; static int frame_rate = 20; static int frame_period = 1000/frame_rate; static int screen_width = WIDTH+1; static int screen_height = HEIGHT+4; static WINDOW *win; static void draw_sprite(int x, int y, char *sprite, int width) { int i, j; for (i = 0; i < width; i++) { if (sprite[i]) { mvwaddch(win, y, x+i, sprite[i]); } } } static void draw_title() { int i; int y = (screen_height - sizeof(title)/sizeof(title[0])) / 2; for (i = 0; title[i]; i++) { mvwaddstr(win, y+i, (screen_width-strlen(title[i]))/2, title[i]); } } static void draw_gameover() { int i; int y = (screen_height - sizeof(gameover)/sizeof(gameover[0])) / 2; for (i = 0; gameover[i]; i++) { mvwaddstr(win, y+i, (screen_width-strlen(gameover[i]))/2, gameover[i]); } } static void draw_score() { sprintf(score, "%d", distance); mvwaddstr(win, 0, screen_width-score_length-1, score_prefix[0]); wattron(win, A_REVERSE); mvwaddstr(win, 0, screen_width-score_length-1+strlen(score_prefix[0]), score); wattroff(win, A_REVERSE); } static void draw_player() { if (player_jump_remaining) { draw_sprite(PLAYER_POS_X, player_pos_y, player_up, sprite_widths[0]); } else { draw_sprite(PLAYER_POS_X, player_pos_y, player_down, sprite_widths[1]); } } static void draw_obstacle() { if (obstacle_visible) { draw_sprite(obstacle_pos, obstacle_y[obstacle_type], obstacles[obstacle_type], sprite_widths[obstacle_type]); } } static void draw() { werase(win); draw_score(); draw_player(); draw_obstacle(); wrefresh(win); } static void update_player() { if (player_jump_remaining) { player_pos_y -= player_speed_y; player_speed_y -= GRAVITY; if (player_pos_y >= PLAYER_POS_Y) { player_jump_remaining = 0; player_pos_y = PLAYER_POS_Y; player_speed_y = 0; } } } static void update_obstacle() { if (++frame_count % OBSTACLE_PERIOD == 0) { if (!obstacle_visible) { obstacle_visible = 1; obstacle_type = rand() % 2; obstacle_pos = WIDTH - sprite_widths[obstacle_type]; obstacle_speed = 1 + rand() % 3; } } if (obstacle_visible) { obstacle_pos -= obstacle_speed; if (obstacle_pos < -sprite_widths[obstacle_type]) { obstacle_visible = 0; } } } static void update_distance() { distance += DISTANCE_PER_FRAME; score_length = strlen(score_prefix[0]) + strlen(score); } static void handle_input() { int c = getch(); if (c == ' ') { if (player_pos_y == PLAYER_POS_Y) { player_jump_remaining = JUMP_DURATION; player_speed_y = JUMP_HEIGHT; } else if (!obstacle_visible) { distance = 0; } } else if (c == 'q') { endwin(); exit(0); } } static void init() { initscr(); cbreak(); noecho(); curs_set(0); nodelay(stdscr, TRUE); win = newwin(screen_height, screen_width, 0, 0); srand(time(NULL)); } static void cleanup() { delwin(win); endwin(); } static void game_loop() { for (;;) { update_player(); update_obstacle(); update_distance(); draw(); handle_input(); if (player_pos_y == PLAYER_POS_Y && obstacle_visible && obstacle_type == 0 && obstacle_pos == PLAYER_POS_X && obstacle_y[0] == PLAYER_POS_Y) { draw_gameover(); wrefresh(win); while (getch() != ' ') { /* wait for space */ } distance = 0; obstacle_visible = 0; player_pos_y = PLAYER_POS_Y; player_speed_y = 0; player_jump_remaining = 0; frame_count = 0; } napms(frame_period); } } int main(int argc, char *argv[]) { init(); draw_title(); wrefresh(win); while (getch() != ' ') { /* wait for space */ } game_loop(); cleanup(); return 0; } ``` 这个代码是一个使用 curses 库的 Moon-Buggy 游戏。游戏中,玩家需要控制一辆月球车,躲避障碍物并尽可能地前进,直到撞上障碍物或按下 Q 键退出游戏。

相关推荐

AddressSanitizer: heap-buffer-overflow on address 0x60200000008c at pc 0x00000034d51c bp 0x7ffd7d4a5d80 sp 0x7ffd7d4a5d78 ================================================================= ==20==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x60200000008c at pc 0x00000034d51c bp 0x7ffd7d4a5d80 sp 0x7ffd7d4a5d78 WRITE of size 4 at 0x60200000008c thread T0 #3 0x7f70e3f18082 (/lib/x86_64-linux-gnu/libc.so.6+0x24082) 0x60200000008c is located 4 bytes to the left of 8-byte region [0x602000000090,0x602000000098) allocated by thread T0 here: #4 0x7f70e3f18082 (/lib/x86_64-linux-gnu/libc.so.6+0x24082) Shadow bytes around the buggy address: 0x0c047fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c047fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c047fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c047fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c047fff8000: fa fa fd fa fa fa fd fa fa fa 00 00 fa fa fd fa =>0x0c047fff8010: fa[fa]00 fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c047fff8020: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c047fff8030: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c047fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c047fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c047fff8060: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa Shadow byte legend (one shadow byte represents 8 application bytes): Addressable: 00 Partially addressable: 01 02 03 04 05 06 07 Heap left redzone: fa Freed heap region: fd Stack left redzone: f1 Stack mid redzone: f2 Stack right redzone: f3 Stack after return: f5 Stack use after scope: f8 Global redzone: f9 Global init order: f6 Poisoned by user: f7 Container overflow: fc Array cookie: ac Intra object redzone: bb ASan internal: fe Left alloca redzone: ca Right alloca redzone: cb Shadow gap: cc ==20==ABORTING

================================================================= ==21==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6020000000a0 at pc 0x00000034c9e6 bp 0x7fff3f973bc0 sp 0x7fff3f973bb8 READ of size 4 at 0x6020000000a0 thread T0 #2 0x7f39d6e8e082 (/lib/x86_64-linux-gnu/libc.so.6+0x24082) 0x6020000000a0 is located 0 bytes to the right of 16-byte region [0x602000000090,0x6020000000a0) allocated by thread T0 here: #6 0x7f39d6e8e082 (/lib/x86_64-linux-gnu/libc.so.6+0x24082) Shadow bytes around the buggy address: 0x0c047fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c047fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c047fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c047fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c047fff8000: fa fa fd fa fa fa fd fa fa fa fd fa fa fa fd fa =>0x0c047fff8010: fa fa 00 00[fa]fa fa fa fa fa fa fa fa fa fa fa 0x0c047fff8020: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c047fff8030: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c047fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c047fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c047fff8060: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa Shadow byte legend (one shadow byte represents 8 application bytes): Addressable: 00 Partially addressable: 01 02 03 04 05 06 07 Heap left redzone: fa Freed heap region: fd Stack left redzone: f1 Stack mid redzone: f2 Stack right redzone: f3 Stack after return: f5 Stack use after scope: f8 Global redzone: f9 Global init order: f6 Poisoned by user: f7 Container overflow: fc Array cookie: ac Intra object redzone: bb ASan internal: fe Left alloca redzone: ca Right alloca redzone: cb Shadow gap: cc ==21==ABORTING

执行出错 ================================================================= ==20==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x602000000040 at pc 0x557dd915fbf4 bp 0x7ffe457c4460 sp 0x7ffe457c4450 WRITE of size 8 at 0x602000000040 thread T0 #1 0x7f1640af6082 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x24082) 0x602000000040 is located 0 bytes to the right of 16-byte region [0x602000000030,0x602000000040) allocated by thread T0 here: #0 0x7f164173ea06 in __interceptor_calloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cc:153 #4 0x7f1640af6082 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x24082) Shadow bytes around the buggy address: 0x0c047fff7fb0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c047fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c047fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c047fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c047fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 =>0x0c047fff8000: fa fa fd fd fa fa 00 00[fa]fa 00 00 fa fa 00 00 0x0c047fff8010: fa fa 00 00 fa fa 00 00 fa fa fa fa fa fa fa fa 0x0c047fff8020: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c047fff8030: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c047fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c047fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa Shadow byte legend (one shadow byte represents 8 application bytes): Addressable: 00 Partially addressable: 01 02 03 04 05 06 07 Heap left redzone: fa Freed heap region: fd Stack left redzone: f1 Stack mid redzone: f2 Stack right redzone: f3 Stack after return: f5 Stack use after scope: f8 Global redzone: f9 Global init order: f6 Poisoned by user: f7 Container overflow: fc Array cookie: ac Intra object redzone: bb ASan internal: fe Left alloca redzone: ca Right alloca redzone: cb Shadow gap: cc ==20==ABORTING

================================================================= ==21==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x607000000068 at pc 0x00000034cf7d bp 0x7ffe25e739d0 sp 0x7ffe25e739c8 READ of size 8 at 0x607000000068 thread T0 #3 0x7fa68d3a7082 (/lib/x86_64-linux-gnu/libc.so.6+0x24082) 0x607000000068 is located 0 bytes to the right of 72-byte region [0x607000000020,0x607000000068) allocated by thread T0 here: #7 0x7fa68d3a7082 (/lib/x86_64-linux-gnu/libc.so.6+0x24082) Shadow bytes around the buggy address: 0x0c0e7fff7fb0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c0e7fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c0e7fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c0e7fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c0e7fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 =>0x0c0e7fff8000: fa fa fa fa 00 00 00 00 00 00 00 00 00[fa]fa fa 0x0c0e7fff8010: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c0e7fff8020: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c0e7fff8030: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c0e7fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c0e7fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa Shadow byte legend (one shadow byte represents 8 application bytes): Addressable: 00 Partially addressable: 01 02 03 04 05 06 07 Heap left redzone: fa Freed heap region: fd Stack left redzone: f1 Stack mid redzone: f2 Stack right redzone: f3 Stack after return: f5 Stack use after scope: f8 Global redzone: f9 Global init order: f6 Poisoned by user: f7 Container overflow: fc Array cookie: ac Intra object redzone: bb ASan internal: fe Left alloca redzone: ca Right alloca redzone: cb Shadow gap: cc ==21==ABORTING

最新推荐

recommend-type

飞歌G6IV刷机包,恢复出厂解决车机问题

飞歌G6IV刷机包,恢复出厂解决车机问题
recommend-type

人工智能大作业-无人机图像目标检测.zip

无人机最强算法源码,易于部署和学习交流使用
recommend-type

node-v10.9.0-linux-s390x.tar.xz

Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。
recommend-type

Telecord机器人,无electron依赖。.zip

无人机最强源码,无人机算法,易于部署和学习交流使用
recommend-type

中国统计NJ面板数据-(更新至2022年)林业有害生物防治情况.xls

数据来源:中国统计NJ-2023版
recommend-type

RTL8188FU-Linux-v5.7.4.2-36687.20200602.tar(20765).gz

REALTEK 8188FTV 8188eus 8188etv linux驱动程序稳定版本, 支持AP,STA 以及AP+STA 共存模式。 稳定支持linux4.0以上内核。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

系统函数是1+5*z^(-1)+5*z^(-2)+z^(-3) ,给出Matlab中求该系统频率响应的代码

假设系统函数为H(z),则其频率响应为H(w),可以通过以下代码求解: ``` syms z w H = 1 + 5*z^(-1) + 5*z^(-2) + z^(-3); % 定义系统函数 Hw = subs(H, z, exp(1i*w)); % 将z用e^(jw)代替 Hw = simplify(Hw); % 化简 absHw = abs(Hw); % 求幅度响应 angleHw = angle(Hw); % 求相位响应 ``` 其中,`simplify`函数用于化简表达式,`abs`函数用于求绝对值,`angle`函数用于求相位。
recommend-type

c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf

校园超市商品信息管理系统课程设计旨在帮助学生深入理解程序设计的基础知识,同时锻炼他们的实际操作能力。通过设计和实现一个校园超市商品信息管理系统,学生掌握了如何利用计算机科学与技术知识解决实际问题的能力。在课程设计过程中,学生需要对超市商品和销售员的关系进行有效管理,使系统功能更全面、实用,从而提高用户体验和便利性。 学生在课程设计过程中展现了积极的学习态度和纪律,没有缺勤情况,演示过程流畅且作品具有很强的使用价值。设计报告完整详细,展现了对问题的深入思考和解决能力。在答辩环节中,学生能够自信地回答问题,展示出扎实的专业知识和逻辑思维能力。教师对学生的表现予以肯定,认为学生在课程设计中表现出色,值得称赞。 整个课程设计过程包括平时成绩、报告成绩和演示与答辩成绩三个部分,其中平时表现占比20%,报告成绩占比40%,演示与答辩成绩占比40%。通过这三个部分的综合评定,最终为学生总成绩提供参考。总评分以百分制计算,全面评估学生在课程设计中的各项表现,最终为学生提供综合评价和反馈意见。 通过校园超市商品信息管理系统课程设计,学生不仅提升了对程序设计基础知识的理解与应用能力,同时也增强了团队协作和沟通能力。这一过程旨在培养学生综合运用技术解决问题的能力,为其未来的专业发展打下坚实基础。学生在进行校园超市商品信息管理系统课程设计过程中,不仅获得了理论知识的提升,同时也锻炼了实践能力和创新思维,为其未来的职业发展奠定了坚实基础。 校园超市商品信息管理系统课程设计的目的在于促进学生对程序设计基础知识的深入理解与掌握,同时培养学生解决实际问题的能力。通过对系统功能和用户需求的全面考量,学生设计了一个实用、高效的校园超市商品信息管理系统,为用户提供了更便捷、更高效的管理和使用体验。 综上所述,校园超市商品信息管理系统课程设计是一项旨在提升学生综合能力和实践技能的重要教学活动。通过此次设计,学生不仅深化了对程序设计基础知识的理解,还培养了解决实际问题的能力和团队合作精神。这一过程将为学生未来的专业发展提供坚实基础,使其在实际工作中能够胜任更多挑战。