0 Members and 3 Guests are viewing this topic.
player.position_x=player.position_x_temp-player.drillcount/player.drillmax;
player.position_x=player.position_x_temp-20*player.drillcount/player.drillmax;
player.position_x=player.position_x_temp-1.1*player.drillcount/player.drillmax;
#include <os.h>#include <SDL/SDL.h>#include <ctime>#include <cmath>#include "blocks.cpp"#include "player.cpp"#define score_color 0x8000#define spacing 12uint32_t *money=0;uint16_t tempint;uint16_t chance[18];//for ore generationuint8_t getlength(int score){ uint8_t scorelength; while(1) { if(score>9) { if(score>99) { if(score>999) { if(score>9999) { if(score>99999) { scorelength=6; //gl reaching 7 digits break; } scorelength=5; break; } scorelength=4; break; } scorelength=3; break; } scorelength=2; break; } scorelength=1; break; } return scorelength;}void shownumber(SDL_Surface *screen,uint32_t score,uint8_t scorelength){ uint8_t tempscore; score*=10; for(int i=0;i<scorelength;i++) { score/=10; tempscore=score%10; switch(tempscore) { case 0: for(int z=0;z<15;z++) { nSDL_SetPixel(screen,305-spacing*i,z,score_color); nSDL_SetPixel(screen,315-spacing*i,z,score_color); } for(int z=1;z<11;z++) { nSDL_SetPixel(screen,305-spacing*i+z,0,score_color); nSDL_SetPixel(screen,305-spacing*i+z,14,score_color); } break; case 1: for(int z=0;z<15;z++) { nSDL_SetPixel(screen,310-spacing*i,z,score_color); } break; case 2: for(int z=0;z<11;z++) { nSDL_SetPixel(screen,305-spacing*i+z,0,score_color); nSDL_SetPixel(screen,305-spacing*i+z,7,score_color); nSDL_SetPixel(screen,305-spacing*i+z,14,score_color); } for(int z=0;z<8;z++) { nSDL_SetPixel(screen,315-spacing*i,z,score_color); nSDL_SetPixel(screen,305-spacing*i,7+z,score_color); } break; case 3: for(int z=0;z<15;z++) { nSDL_SetPixel(screen,315-spacing*i,z,score_color); } for(int z=0;z<10;z++) { nSDL_SetPixel(screen,305-spacing*i+z,0,score_color); nSDL_SetPixel(screen,305-spacing*i+z,7,score_color); nSDL_SetPixel(screen,305-spacing*i+z,14,score_color); } break; case 4: for(int z=0;z<15;z++) { nSDL_SetPixel(screen,315-spacing*i,z,score_color); } for(int z=0;z<10;z++) { nSDL_SetPixel(screen,305-spacing*i+z,7,score_color); } for(int z=0;z<7;z++) { nSDL_SetPixel(screen,305-spacing*i,z,score_color); } break; case 5: for(int z=0;z<7;z++) { nSDL_SetPixel(screen,305-spacing*i,z,score_color); nSDL_SetPixel(screen,315-spacing*i,7+z,score_color); } for(int z=0;z<10;z++) { nSDL_SetPixel(screen,305-spacing*i+z,0,score_color); nSDL_SetPixel(screen,305-spacing*i+z,7,score_color); nSDL_SetPixel(screen,305-spacing*i+z,14,score_color); } break; case 6: for(int z=0;z<10;z++) { nSDL_SetPixel(screen,305-spacing*i+z,0,score_color); nSDL_SetPixel(screen,305-spacing*i+z,7,score_color); nSDL_SetPixel(screen,305-spacing*i+z,14,score_color); } for(int z=0;z<15;z++) { nSDL_SetPixel(screen,305-spacing*i,z,score_color); } for(int z=0;z<8;z++) { nSDL_SetPixel(screen,315-spacing*i,7+z,score_color); } break; case 7: for(int z=0;z<15;z++) { nSDL_SetPixel(screen,315-spacing*i,z,score_color); } for(int z=0;z<10;z++) { nSDL_SetPixel(screen,305-spacing*i+z,0,score_color); } break; case 8: for(int z=0;z<10;z++) { nSDL_SetPixel(screen,305-spacing*i+z,0,score_color); nSDL_SetPixel(screen,305-spacing*i+z,7,score_color); nSDL_SetPixel(screen,305-spacing*i+z,14,score_color); } for(int z=0;z<15;z++) { nSDL_SetPixel(screen,305-spacing*i,z,score_color); nSDL_SetPixel(screen,315-spacing*i,z,score_color); } break; case 9: for(int z=0;z<10;z++) { nSDL_SetPixel(screen,305-spacing*i+z,0,score_color); nSDL_SetPixel(screen,305-spacing*i+z,7,score_color); nSDL_SetPixel(screen,305-spacing*i+z,14,score_color); } for(int z=0;z<8;z++) { nSDL_SetPixel(screen,305-spacing*i,z,score_color); } for(int z=0;z<15;z++) { nSDL_SetPixel(screen,315-spacing*i,z,score_color); } break; }; }}enum blocktype{ nothing=0, dirt=1, coal=2, iron=3, tin=4, copper=5, silver=6, gold=7, platinum=8, topaz=9, sapphire=10, emerald=11, ruby=12, opal=13, jade=14, diamond=15, fossil=16, artifact=17, sigsegv=18, lava=19,//visible but does lots of damage rock=20,//you have to blow these up with explosives gas=21//invisible and blows up if you run into it};blocktype map[600][40];//motherload is something like 12 feet a block-1 block is 20 pixels//less than 100kB anyway, no point using push_back so oftenenum currentdirection{ down=0, right=1, left=2, up=3};class theplayer{public: bool is_flying; SDL_Rect rect_player_centered; SDL_Rect rect_player_animation_source; uint16_t drillmax; uint16_t drillcount; uint8_t animationcount; bool is_active; currentdirection direction; int8_t acceleration; int16_t position_x;//in pixels int16_t position_x_temp; int16_t depth;//in pixels int16_t depth_temp; uint8_t hull; uint8_t hullhealth; uint8_t drillspeed; uint8_t drill; uint16_t fueltank; uint16_t fuel;//every loop-1L=100 loops=2 seconds at 50FPS uint8_t engine; uint8_t enginepower;//max pixels/second lift uint8_t cargobay; uint16_t cargocapacity;//in cubic feet uint8_t cargo[18];//each element represents how many of a certain type //for example, element 0 represents coal, which is the second item in the enum theplayer() { acceleration=0; depth_temp=0; drillmax=0; drillcount=0; animationcount=0; is_active=0; direction=right; depth=0; position_x=100; hull=1; hullhealth=10; drill=1; drillspeed=10; fueltank=1; fuel=150; engine=1; enginepower=3; cargobay=1; cargocapacity=10; rect_player_centered.x=156;//player's position drawn on the screen, always centered rect_player_centered.y=106; rect_player_centered.w=28; rect_player_centered.h=28; rect_player_animation_source.x=0; rect_player_animation_source.y=0; rect_player_animation_source.w=28; rect_player_animation_source.h=28; for(int i=0;i<15;i++) { cargo[i]=0; } } void sell_cargo() { *money+=30*cargo[0];//coal is worth 30 *money+=50*cargo[1];//iron is worth 50 *money+=80*cargo[2];//tin is worth 80 *money+=120*cargo[3];//copper *money+=200*cargo[4];//silver *money+=400*cargo[5];//gold *money+=750*cargo[6];//platinum *money+=1000*cargo[7];//topaz *money+=1200*cargo[8];//sapphire *money+=2000*cargo[9];//emerald *money+=3500*cargo[10];//ruby *money+=5000*cargo[11];//opal *money+=8000*cargo[12];//jade *money+=20000*cargo[13];//diamond *money+=10000*cargo[14];//fossils *money+=15000*cargo[15];//artifacts *money+=100000*cargo[16];//sigsegv lol for(int i=0;i<15;i++) { cargo[i]=0; } }};void newrow(uint16_t row){ chance[0]=1000/(abs(pow(row-10,1.1))+50);//1000/(abs(pow(row-row_where_it_is_most_common,range_of_how_common_it_is))+100/percent_of_blocks_in_most_common_row) //coal is found with a frequency of 100/50=2% of blocks in the row in which it is most commonly found //coal is most commonly found in row 10 (actually 11 in the array, but 1 more or less doesn't make a huge difference) //coal is commonly found in a large range of blocks, note the low exponent //note that 0 in this array corresponds to 2 in blocktype chance[1]=1000/(abs(pow(row-30,1.1))+60);//iron is found in 1.667% or 16/1000 blocks in the generator at max, and is also very common chance[2]=1000/(abs(pow(row-50,1.1))+80); chance[3]=1000/(abs(pow(row-80,1.1))+20); chance[4]=1000/(abs(pow(row-110,1.3))+100); chance[5]=1000/(abs(pow(row-150,1.5))+150); for(int i=0;i<40;i++) { tempint=rand()%1000; for(int z=0;z<7;z++) { if(z==6) { if(rand()%8) { map[row][i]=dirt; break; } map[row][i]=nothing; break; } if(chance[z]>tempint) { map[row][i]=(static_cast<blocktype>(z+2)); break; } tempint-=chance[z]; } }}int main(){ srand(time(NULL)); uint8_t intlength; SDL_Init(SDL_INIT_VIDEO); SDL_Surface *screen=SDL_SetVideoMode(320,240,16,SDL_SWSURFACE); nSDL_Font *font=nSDL_LoadFont(NSDL_FONT_VGA,0,64,0); SDL_Surface *sprite_blocks=nSDL_LoadImage(image_blocks); SDL_Surface *sprite_player=nSDL_LoadImage(image_player); SDL_SetColorKey(sprite_blocks,SDL_SRCCOLORKEY,SDL_MapRGB(sprite_blocks->format,0xff,0xff,0xff)); SDL_SetColorKey(sprite_player,SDL_SRCCOLORKEY,SDL_MapRGB(sprite_blocks->format,0xff,0xff,0xff)); SDL_Rect rect_fullscreen{0,0,320,240}; SDL_Rect rect_sky{0,0,320,0}; theplayer player; SDL_Rect sprite_blocks_source{0,0,20,20}; SDL_Rect rect_blocks; rect_blocks.w=20; rect_blocks.h=20; SDL_Rect rect_hud1{0,0,320,20}; SDL_Rect rect_hud2{0,20,20,220}; SDL_Rect rect_blank{0,0,20,20}; for(int i=0;i<600;i++) { newrow(i); } while(1) { if(!player.is_active) { if(isKeyPressed(KEY_NSPIRE_UP)) { player.direction=up; player.is_active=1; } else if(isKeyPressed(KEY_NSPIRE_LEFT) && player.position_x>19) { player.direction=left; player.is_active=1; player.position_x_temp=player.position_x; player.drillmax=player.depth/20+200; } else if(isKeyPressed(KEY_NSPIRE_DOWN) && player.depth>-1) { player.depth_temp=player.depth; player.direction=down; player.is_active=1; player.drillmax=player.depth/20+200; } else if(isKeyPressed(KEY_NSPIRE_RIGHT) && player.position_x<619) { player.direction=right; player.position_x_temp=player.position_x; player.is_active=1; player.drillmax=player.depth/20+200; } } else { if(player.direction==down) { player.depth=player.depth_temp+20*player.drillcount/player.drillmax; } else if(player.direction==right) { player.position_x=player.position_x_temp-player.drillcount/player.drillmax; } else if(player.direction==left) { player.position_x=player.position_x_temp+20*player.drillcount/player.drillmax; } player.animationcount++; player.animationcount%=4; player.rect_player_animation_source.x=static_cast<currentdirection>(player.direction)*112+player.animationcount*28; player.drillcount+=player.drillspeed; if(player.drillcount>=player.drillmax) { if(player.direction==left) { player.position_x=player.position_x_temp-20; } else if(player.direction==right) { player.position_x=player.position_x_temp+20; } map[player.depth_temp/20-1][player.position_x/20+15]=nothing; if(player.direction==down) { player.depth=player.depth_temp+20; } player.drillcount=0; player.is_active=0; } } if(player.direction==up) { if(!isKeyPressed(KEY_NSPIRE_UP)) { if(player.acceleration!=0) { player.acceleration--; } else { player.is_active=0; } } else if(player.acceleration!=player.enginepower) { player.acceleration++; } player.depth-=player.acceleration; } if(isKeyPressed(KEY_NSPIRE_ESC)) { SDL_Quit(); return 0; } SDL_FillRect(screen,&rect_fullscreen,0x7b08); if(player.depth<140) { rect_sky.h=140-player.depth; SDL_FillRect(screen,&rect_sky,0x9edd); } for(int z=0;z<13;z++) { if(player.depth+20*z<120) { continue; } for(int i=0;i<17;i++) { if(player.position_x/20+i<0) { continue; } rect_blocks.y=20*z-player.depth%20+10; rect_blocks.x=player.position_x%20+20*i; if(static_cast<blocktype>(map[player.depth/20+z-7][player.position_x/20+i+7])>1) { sprite_blocks_source.x=20; SDL_BlitSurface(sprite_blocks,&sprite_blocks_source,screen,&rect_blocks); } sprite_blocks_source.x=20*(static_cast<blocktype>(map[player.depth/20+z-7][player.position_x/20+i+7])); SDL_BlitSurface(sprite_blocks,&sprite_blocks_source,screen,&rect_blocks); } } SDL_BlitSurface(sprite_player,&player.rect_player_animation_source,screen,&player.rect_player_centered); //SDL_FillRect(screen,&rect_fullscreen,35362);//brown in case you were wondering-crude graphics for dirt SDL_FillRect(screen,&rect_hud1,0xffff); SDL_FillRect(screen,&rect_hud2,0xffff); intlength=getlength(abs(player.depth)); shownumber(screen,abs(player.depth),intlength); if(player.depth<0) { for(int i=0;i<10;i++) { nSDL_SetPixel(screen,305-intlength*12+i,7,score_color); } } SDL_Flip(screen); } return 0;}