This page was last updated on March 13th, 2020(UTC) and it is currently December 1st, 2020(UTC).
That means this page is 263 days, 6 hours, 38 minutes and 52 seconds old. Please keep that in mind.

40 - Classes for Stars Now, that which makes C++ more popular than C for most projects. C++ is a constantly evolving language, unfortunately, which means you might've run into some snags if you tried this on a modern C++ compiler. Ultimately, you'll want to leave what you learned here behind on the DOS emulator, and learn the modern equivalent, reading from a reference book. That said, I expect most of this to stay the same. While C has structs and enums, which C++ still has, C++ has classes, which can do some things that structs cannot. Down the rabbit hole we go...

#include <stdio.h> //------------------------------------------------------------------------- //Defines are compiletime only variables. #define xsize 320 #define ysize 200 #define starnum 200 //------------------------------------------------------------------------- extern "C" void srand(int seed); extern "C" int rand(); extern "C" int time(int ptr); //------------------------------------------------------------------------- extern "C" int easymode(); extern "C" char buffer[0xfa00]; //------------------------------------------------------------------------- class star { int x, y; public: star(){ x = rand() % xsize; y = rand() % ysize; } void draw(int px, int py){ if((y - py) < 0){ y = ysize + py; x = rand() % xsize - px; } else if((y - py) > ysize){ y = py; x = rand() % xsize - px; } if((x - px) < 0) { x = xsize + px; y = rand() % ysize - py; } else if ((x - px) > xsize){ x = px; y = rand() % ysize - py; } buffer[(y - py) * xsize + (x - px)] = 0x0f; } }; //------------------------------------------------------------------------- star starfield[starnum]; int scroll = 0; //------------------------------------------------------------------------- int main(){ return easymode(); } //------------------------------------------------------------------------- extern "C" int init(){ srand(time(0)); for(int i = 0; i < starnum; i++) starfield[i] = star(); return 0; } //------------------------------------------------------------------------- extern "C" int cleanup(){ return 0; } //------------------------------------------------------------------------- extern "C" int draw(){ for (int i = 0; i < starnum; i++) starfield[i].draw(scroll, 0); scroll++; return 1; }

Yep, we have the impossibly fast moving stars, 'cause we're in space and moving fast, even though we don't have a ship or anything on the screen. This is to show classes at their most basic before moving on. The idea is a "class" allows you to create your own datatypes, with their own properties. Functions from a class are identified via the object (viariable instance of the class) then a dot, then the function (and paramters). There's also "this->" ("this" is actually a pointer to the object from within the defining class) for referring to variables from the class, but it's OK as long as there's no conflicts to omit the "this->", but feel free to experiment. I'm honestly not sure why they insisted on making "->" separate from "." but note that it's not advised to try to use them interchangeably.

Get your own web kitty here!
┬ęCopyright 2010-2020. All rights reserved.