Posted: Thu Oct 28, 2004 7:43 pm
No really, if you ever divide by 0, blitz says "Error Divide By 0" then crashes. You must have changed some of the mechanics.
The Next Generation of 2D Roleplaying Games
http://elysianshadows.com/phpBB3/
Code: Select all
blood->life = rand() % (intensity/12)
Code: Select all
blood/life = Rand(0,intensity/12)
Code: Select all
rect_color = SDL_MapRGB(DisplaySurface->format, 255-(max_bloodlife - blood->life)*8, 0, 0);
Code: Select all
void CreateBlood(int x, int y, int angle, int intensity, unsigned short int color, unsigned short int r, unsigned short int g, unsigned short int b);
Code: Select all
rect_color = SDL_MapRGB(DisplaySurface->format, 255-(max_bloodlife - blood->life)*8, 0, 255-(max_bloodlife - blood->life)*8);
Code: Select all
;Updates goombas that are in existance
Function g_UpdateGoomba(DeleteAll)
;Loop through all goombas
For cur.Goomba = Each Goomba
;lower goomba's invincible
If cur\invincible > 0 Then cur\invincible = cur\invincible - 1
;Check if he is on hard mode
If cur\hard = True
If Distance(Mario\x+16,Mario\y+32,cur\x+16,cur\y) < 220
If cur\x < Mario\x-40 Then cur\xvel = 3
If cur\x > Mario\x+40 Then cur\xvel = -3
If cur\jump = False And solid((cur\x/32),(cur\y/32)+1) = 1
cur\jump = True
cur\jumptimer = Rand(7,9)
EndIf
EndIf
EndIf
;Keep Goomba in map array range
If cur\x < 32 Then cur\x = 32 : cur\xvel = 2
If cur\y < 32 Then cur\y = 32 : cur\yvel = 2
If (cur\x + 32) > (MapWD*32)+32 Then cur\x = (MapWD*32) - 64+32 : cur\xvel = 0
If (cur\y + 32) > (MapHT*32)+32 Then cur\y = (MapHT*32) - 32+32 : cur\yvel = 0
;Check if goomba has hit a wall
If solid(((cur\x-1)/32),(cur\y/32)) = 1 Then cur\xvel = 2
If solid(((cur\x+1+32)/32),(cur\y/32)) = 1 Then cur\xvel = -2
;Check if goomba about to fall off legde
If solid(((cur\x-1)/32),((cur\y+40)/32)) = 0 And cur\jump = False Then cur\xvel = 2
If solid(((cur\x+1+32)/32),((cur\y+40)/32)) = 0 And cur\jump = False Then cur\xvel = -2
;Check if goomba hits another goomba
For temp.Goomba = Each Goomba
If cur\x > temp\x And cur\x < temp\x + 32
If cur\y > temp\y-2 And cur\y < temp\y + 34
If cur\x < temp\x+15 Then cur\xvel = -2 : temp\xvel = 2
If cur\x > temp\x+15 Then cur\xvel = 2 : temp\xvel = -2
EndIf
EndIf
Next
;Cycle through all bullets to see if they are hit
For bul.Bullet = Each Bullet
If bul\x > cur\x And bul\x < cur\x+32
If bul\y > cur\y And bul\y < cur\y+32
cur\xvel = cur\xvel*2
cur\life = cur\life - 1
PlaySound S_BulletHit
;blood spray
g_CreateBlood(bul\x,bul\y,(-bul\xvel)/2,200)
DrawImage P_Bang,bul\x-BaseX,bul\y-BaseY
Delete bul
EndIf
EndIf
Next
;If goomba jumping
If cur\jump = True And cur\jumptimer > 0
cur\yvel = cur\yvel - 0.8
cur\jumptimer = cur\jumptimer - 1
Else
;Simulate Gravity
cur\yvel = cur\yvel + Gravity
EndIf
;Left Right Movement
For i = 1 To Abs(cur\xvel)
BoundLeft = (cur\x + 0)
BoundRight = (cur\x + 32)
BoundTop = (cur\y + 0)
BoundBottom = (cur\y + 32)
For b = BoundTop To BoundBottom-1;
MoveLeft = False
MoveRight = False
If cur\xvel < 0
If Solid((BoundLeft-1)/32,b/32) = 1
cur\xvel = 0
Exit
Else
MoveLeft = True
EndIf
EndIf
If cur\xvel > 0
If Solid((BoundRight+1)/32,b/32) = 1
cur\xvel = 0
Exit
Else
MoveRight = True
EndIf
EndIf
Next
If MoveLeft = True Then cur\x = cur\x - 1
If MoveRight = True Then cur\x = cur\x + 1
Next
;Up and Down movement
For i = 1 To Abs(cur\yvel)
BoundLeft = (cur\x + 0)
BoundRight = (cur\x + 32)
BoundTop = (cur\y + 0)
BoundBottom = (cur\y + 31)
For b = BoundLeft To BoundRight
MoveUp = False
MoveDown = False
If cur\yvel < 0
If Solid(b/32,(BoundTop-1)/32) = 1
cur\yvel = 0
Exit
Else
MoveUp = True
EndIf
EndIf
If cur\yvel > 0
If Solid(b/32,(BoundBottom+1)/32) = 1
cur\yvel = 0
cur\jump = False
Exit
Else
MoveDown = True
EndIf
EndIf
Next
If MoveUp = True Then cur\y = cur\y - 1
If MoveDown = True Then cur\y = cur\y + 1
Next
;Check if Mario has touched the goomba
If Mario\x+32 > cur\x+4 And Mario\x+32 < cur\x+60
;if mario stomps goomba, bounce him
If Mario\y+64 > cur\y+0 And Mario\y+64 < cur\y+9
Mario\y = cur\y-64
Mario\yvel = -Mario\yvel*1.3
g_CreateBlood(cur\x,cur\y+10,-2,Abs(Mario\yvel*18))
g_CreateBlood(cur\x,cur\y+10,2,Abs(Mario\yvel*18))
PlaySound S_Crush
If cur\invincible = 0
cur\xvel = cur\xvel * 2
cur\life = cur\life - 1
cur\invincible = 10
EndIf
EndIf
;if mario tuoches goomba's side, hurt mario
If Mario\y+64 > cur\y+25 And Mario\y+64 < cur\y+65;34
If Mario\x < cur\x+15
Mario\xvel = -cur\xvel
If cur\xvel < 0 Then Mario\xvel = cur\xvel*2 : cur\xvel = -cur\xvel
EndIf
If Mario\x > cur\x+15
Mario\xvel = -cur\xvel
If cur\xvel > 0 Then Mario\xvel = cur\xvel*2 : cur\xvel = -cur\xvel
EndIf
g_CreateBlood(Mario\x+16,Mario\y+32,cur\xvel*2,280)
;Mario only takes damage if he is not invincible
If Mario\invincible = 0
PlaySound S_Doh
Mario\life = Mario\life - 1
Mario\invincible = 10
EndIf
EndIf
EndIf
;kill it if its out of life
If cur\life < 1 Then DeleteThis = True : EnemiesKilled = EnemiesKilled + 1
;Animate goomba
If cur\framecounter < 1
cur\frame = cur\frame + 1
If cur\frame > 1 Then cur\frame = 0
cur\framecounter = 5
Else
cur\framecounter = cur\framecounter - 1
EndIf
;Draw goomba
DrawImage P_Goomba,cur\x-BaseX,cur\y-BaseY,cur\frame
;Delete this coin if need
If DeleteAll = True Then DeleteThis = True
;If coin needs to be deleted do so
If DeleteThis = True Then Delete cur : DeleteThis = False
Next
End Function
Could be something like:JS Lemming wrote:Code: Select all
;Updates goombas that are in existance ;Check if Mario has touched the goomba If Mario\x+32 > cur\x+4 And Mario\x+32 < cur\x+60 ;if mario stomps goomba, bounce him If Mario\y+64 > cur\y+0 And Mario\y+64 < cur\y+9 Mario\y = cur\y-64 Mario\yvel = -Mario\yvel*1.3
Code: Select all
void marioMove(/* ... */) {
/* ... */
char hitDir;
for(vector<Enemies*>::iterator it;it = enemies.begin();it != enemies.end()) {
//Check entity collision
hitDir = entCollide(player, *it);
if(hitDir && hitDir != 'U') {
doDamage(player, it->dmgAmount());
break; //?
}
}
/* ... */
}
char entCollide(Actor* who, Actor* hitWhat) {
int wx = who->xCoord();
int wy = who->yCoord();
int wr = who->rightOffset();
int wl = who->leftOffset();
int wu = who->upOffset();
int wd = who->downOffset();
int hx = hitWhat->xCoord();
int hy = hitWhat->yCoord();
int hr = hitWhat->rightOffset();
int hl = hitWhat->leftOffset();
int hu = hitWhat->upOffset();
int hd = hitWhat->downOffset();
if ((wx + wr) > (hx + hl) && (wx + wl) < (hx + hl))
{ //if x-coords increase as they go right....
return 'L';
}
else if ((wx + wr) > (hx + hr) && (wx + wl) < (hx + hr))
{
return 'R';
}
else if ((wy + wd) > (hy + hu) && (wy + wu) > (hy + hu))
{ //if y-coords increase as they go up....
//some sort of "hitWhat can be hurt by upward attack" flag.
//return uppercase for one and lowercase for another.
return 'U';
}
/*.... etc ....*/
//if all fail:
return 0; //the null character -- character 0. aka '\0'
}
Code: Select all
void CreateBlood(int x, int y, int angle, int intensity, unsigned short int color, unsigned short int r, unsigned short int g, unsigned short int b);
Code: Select all
void CreateBlood(int x, int y, int angle, int intensity, unsigned short int color, unsigned short int r = 0, unsigned short int g = 0, unsigned short int b = 0);
Code: Select all
void CreateBlood(int x, int y, int angle, int intensity, unsigned short int r, unsigned short int g, unsigned short int b, unsigned short int color = 0);
:!!!!:MarauderIIC wrote:Might rather beCode: Select all
void CreateBlood(int x, int y, int angle, int intensity, unsigned short int color, unsigned short int r, unsigned short int g, unsigned short int b);
Code: Select all
void CreateBlood(int x, int y, int angle, int intensity, unsigned short int color, unsigned short int r = 0, unsigned short int g = 0, unsigned short int b = 0);
JS Lemming wrote:Heh, trust me. Almost all the code from my old mario game is really crusty.
I made entCollide() a general function. Now you only need one entity collision function: that one.GyroVorbis wrote:Shouldn't the goomba functions be inside of the goomba class so that they have access to member variables?
Yeah, good idea.MarauderIIC wrote:I made entCollide() a general function. Now you only need one entity collision function: that one.GyroVorbis wrote:Shouldn't the goomba functions be inside of the goomba class so that they have access to member variables?
Code: Select all
//demo of object collision detection by JS Lemming
//requires static linkage to:
//sdl.lib, sdlmain.lib
//requires dynamic linkage to:
//sdl.dll
//include SDL stuff
#include "sdl.h"
//include ability to exit program
#include <stdlib.h>
//screen dimensions
const int SCREEN_WIDTH=640;
const int SCREEN_HEIGHT=480;
//display surface
SDL_Surface* g_pDisplaySurface;
//event structure
SDL_Event g_Event;
//rectangle
SDL_Rect g_Rect;
//color components
Uint8 g_Red, g_Green, g_Blue;
//color value
Uint32 g_Color;
//Box class
class Box
{
public:
int x, y;
int xvel, yvel;
int width, height;
int red, green, blue;
};
//Returns true if the two rectangles are overlapping
bool RectsOverlap(int x1,int y1,int width1,int height1,int x2,int y2,int width2,int height2) {
if((x1+width1 > x2) && (x1 < x2+width2)) {
if((y1+height1 > y2) && (y1 < y2+height2)) {
return true;
}
}
return false;
}
//main function
int main(int argc, char* argv[])
{
//initialize SDL
if (SDL_Init(SDL_INIT_VIDEO)==-1)
{
//error initializing SDL
//report the error
fprintf(stderr,"Could not initialize SDL!\n");
//end the program
exit(1);
}
else
{
//SDL initialized
//report success
fprintf(stdout,"SDL initialized properly!\n");
//set up to uninitialize SDL at exit
atexit(SDL_Quit);
}
//hide mouse
SDL_ShowCursor(SDL_DISABLE);
//create windowed environment
g_pDisplaySurface = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,24,SDL_ANYFORMAT|SDL_FULLSCREEN);
//error check
if (g_pDisplaySurface == NULL)
{
//report error
fprintf(stderr,"Could not set up display surface!\n");
//exit the program
exit(1);
}
//Create 2 instances of class box
Box box1;
box1.x = 40;
box1.y = 30;
box1.xvel = 0;
box1.yvel = 0;
box1.width = 50;
box1.height = 50;
box1.red = 0;
box1.green = 200;
box1.blue = 0;
Box box2;
box2.x = 300;
box2.y = 200;
box2.xvel = 0;
box2.yvel = 0;
box2.width = 80;
box2.height = 80;
box2.red = 0;
box2.green = 0;
box2.blue = 255;
//main game loop
while(1)
{
//look for an event
if(SDL_PollEvent( &g_Event ) == true)
{
//if user closes out of window, exit loop
if(g_Event.type==SDL_QUIT)
break;
//if ESC key
if(g_Event.key.keysym.sym == SDLK_ESCAPE )
{
// if ESC was pressed, quit the program.
SDL_Event quit;
quit.type = SDL_QUIT;
SDL_PushEvent( &quit );
}
//check key input
switch( g_Event.type )
{
//if a key is pressed
case SDL_KEYDOWN:
switch( g_Event.key.keysym.sym )
{
case SDLK_UP:
box1.yvel = -1;
break;
case SDLK_DOWN:
box1.yvel = 1;
break;
case SDLK_LEFT:
box1.xvel = -1;
break;
case SDLK_RIGHT:
box1.xvel = 1;
break;
}
break;
//a key is released
case SDL_KEYUP:
switch( g_Event.key.keysym.sym )
{
case SDLK_UP:
if(box1.yvel < 0)
box1.yvel = 0;
break;
case SDLK_DOWN:
if(box1.yvel > 0)
box1.yvel = 0;
break;
case SDLK_LEFT:
if(box1.xvel < 0)
box1.xvel = 0;
break;
case SDLK_RIGHT:
if(box1.xvel > 0)
box1.xvel = 0;
break;
}
break;
default:
break;
}
}
//Check to see if they are overlapping
if(RectsOverlap(box1.x,box1.y,box1.width,box1.height,
box2.x,box2.y,box2.width,box2.height) == true)
{
//clear the screen with white
SDL_FillRect(g_pDisplaySurface, NULL, SDL_MapRGB( g_pDisplaySurface->format, 255, 255, 255 ));
}
else
{
//clear the screen with black
SDL_FillRect(g_pDisplaySurface, NULL, SDL_MapRGB( g_pDisplaySurface->format, 0, 0, 0 ));
}
//update the first box
//adjust coords based on velocity
box1.x = box1.x + box1.xvel;
box1.y = box1.y + box1.yvel;
//create the rectangle
g_Rect.x = box1.x;
g_Rect.y = box1.y;
g_Rect.w = box1.width;
g_Rect.h = box1.height;
//set the color
g_Red=box1.red;
g_Green=box1.green;
g_Blue=box1.blue;
g_Color=SDL_MapRGB(g_pDisplaySurface->format,g_Red,g_Green,g_Blue);
//fill the rectangle
SDL_FillRect(g_pDisplaySurface,&g_Rect,g_Color);
//update the second box
//create the rectangle
g_Rect.x = box2.x;
g_Rect.y = box2.y;
g_Rect.w = box2.width;
g_Rect.h = box2.height;
//set the color
g_Red = box2.red;
g_Green = box2.green;
g_Blue = box2.blue;
g_Color = SDL_MapRGB(g_pDisplaySurface->format,g_Red,g_Green,g_Blue);
//fill the rectangle
SDL_FillRect(g_pDisplaySurface,&g_Rect,g_Color);
//update the screen
SDL_UpdateRect(g_pDisplaySurface,0,0,0,0);
}
//normal termination
fprintf(stdout,"Terminating normally.\n");
//return to OS
return(0);
}