Jump to content

Bullet Hell?


Recoils14
 Share

Recommended Posts

Here you go Recoils14. I added some comments to hopefully help. Just replace your App.cpp with the following. Also, don't forget that this code uses a bullet.tex and a ship.tex from the Models folder so you'll need those there so the code doesn't crash. It also assumes that the bullet texture is 16x16 and the ship is 128x128, as that is hard coded (but it would be easy to change to detect it).

 

#include "App.h"

using namespace Leadwerks;

#define NUMBEROFBULLETS 1000

App::App() : window(NULL), context(NULL), world(NULL), camera(NULL) {}

App::~App() { delete world; delete window; }

Texture *bullettexture = NULL;
Texture *shiptexture = NULL;

float shipx, shipy;
float fps;

struct bulletinfo
{
  float x, y, vx, vy;
} bullet[NUMBEROFBULLETS];

bool App::Start()
{
  window = Leadwerks::Window::Create("BulletHell", 0, 0, 1728, 972, Leadwerks::Window::Titlebar|Leadwerks::Window::Center);

  context = Context::Create(window);

  world = World::Create();

  camera = Camera::Create();
  camera->Move(0, 2, -5);

  window->HideMouse();

  bullettexture = Texture::Load("Models/bullet.tex");
  shiptexture = Texture::Load("Models/ship.tex");

  //  Generate random bullet position
  for(int i=0; i<NUMBEROFBULLETS; i++)
  {
 bullet[i].x=Math::Random(0, 1700);
 bullet[i].y=Math::Random(0, 950);
 bullet[i].vx=Math::Random(-2, 2);
 bullet[i].vy=Math::Random(-2, 2);
  }

  //  Generate random ship position and velocities
  shipx=Math::Random(0, 1650);
  shipy=Math::Random(0, 900);

  return true;
}

bool App::Loop()
{
  //  Exit program if window is closed or Escape button is hit
  if(window->Closed() || window->KeyHit(Key::Escape))
  {
 printf("\n\nfps: %f\n", fps);
 return false;
  }

  Leadwerks::Time::Update();
  world->Update();
  world->Render();

  //  Move ship with arrow keys
  if(window->KeyDown(Key::Left)) shipx-=Time::GetSpeed()*3.0;
  if(window->KeyDown(Key::Right)) shipx+=Time::GetSpeed()*3.0;
  if(window->KeyDown(Key::Up)) shipy-=Time::GetSpeed()*3.0;
  if(window->KeyDown(Key::Down)) shipy+=Time::GetSpeed()*3.0;

  context->SetBlendMode(Blend::Alpha);


  //  Go through all of the bullets, one by one
  for(int i=0; i<NUMBEROFBULLETS; i++)
  {
 //  If bullet is at the edge of the screen, turn it around
 if(bullet[i].x>1700 && bullet[i].vx>0) bullet[i].vx*=-1;
 if(bullet[i].x<0 && bullet[i].vx<0) bullet[i].vx*=-1;
 if(bullet[i].y>970 && bullet[i].vy>0) bullet[i].vy*=-1;
 if(bullet[i].y<0 && bullet[i].vy<0) bullet[i].vy*=-1;

 //  Move bullet
 bullet[i].x+=bullet[i].vx*Time::GetSpeed();
 bullet[i].y+=bullet[i].vy*Time::GetSpeed();

 //  If bullet is by the ship, turn it blue
 if(bullet[i].x>shipx-8 && bullet[i].x<shipx+136 && bullet[i].y>shipy-8 && bullet[i].y<shipy+136) context->SetColor(0,0,1);
 else context->SetColor(1, 1, 1);

 //  Draw bullet
 context->DrawImage(bullettexture, bullet[i].x, bullet[i].y);
  }

  //  Draw ship
  context->DrawImage(shiptexture, shipx, shipy);

  //  Get frames per second and show it
  fps=Time::UPS();
  context->DrawText("FPS: "+String(fps), 10, 10);

  context->Sync(false);

  return true;
}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...