void monster::monstermove(player * player1) {
if(abs(player1->m_x - m_x) < 10 && abs(player1->m_y - m_y) < 10)
return;
if(!CanMove())
return;
float tangent;
m_yvel = 4;
m_xvel=4;
if(abs(player1->m_y - this->m_y) < 10) m_yvel=0;
if(abs(player1->m_x - this->m_x) < 10) m_xvel=0;
tangent = (player1->m_y - this->m_y) / (player1->m_x - this->m_x) ;
this->m_yvel *= (abs(sinf(atan(tangent))));
this->m_xvel *= (abs(cosf(atan(tangent))));
if(player1->m_x - this->m_x > 0 && player1->m_y - this->m_y < 0) {
m_yvel*=-1;
}else if(player1->m_x - this->m_x < 0 && player1->m_y - this->m_y> 0) {
m_xvel*=-1;
}else if(player1->m_x - this->m_x < 0 && player1->m_y - this->m_y< 0) {
m_yvel*=-1;
m_xvel*=-1;
}
this->m_y += m_yvel;
this->m_x += m_xvel; //* (player1->m_x - this->m_x>0 && m_xvel >0 ?1:-1);
}
16 Kasım 2013 Cumartesi
3 Kasım 2013 Pazar
SDL Library - Tetris Game
#include "SDL.h"
#include "glut.h"
#include <iostream>
#include <string>
#include <Windows.h>
#include <SDL_opengl.h>
#include "ball.h"
#include <SDL_image.h>
struct brick{
float x;
float y;
float width;
float height;
bool alive;
};
GLuint loadTexture( const std::string &fileName )
{
SDL_Surface *image = IMG_Load( fileName.c_str() );
SDL_DisplayFormatAlpha(image);
unsigned object(0);
glGenTextures(1, &object);
glBindTexture(GL_TEXTURE_2D, object);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image->w, image->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);
//Free surface
SDL_FreeSurface(image);
return object;
}
bool iscollission(float ballx,float bally,float ballH,float ballL,float ourx,float oury,float ourH,float ourL);
int main( int argc, char* args[] )
{
//initialize SDL
SDL_Init(SDL_INIT_EVERYTHING);
//Set OpenGL memory usage
SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute( SDL_GL_BUFFER_SIZE, 32);
SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
//Caption of the window
SDL_WM_SetCaption( "Our first game", NULL );
//Size of the window
SDL_SetVideoMode(600,400,32, SDL_OPENGL );
//Specific the clear color
glClearColor(1,1,1,1); //RED,GREEN,BLUE,ALPHA
//What portion of the screen we will display
glViewport(0,0,600,400);
//Shader model - Use this
glShadeModel(GL_SMOOTH);
//2D rendering
glMatrixMode(GL_PROJECTION);
//"Save" it
glLoadIdentity();
//Disable depth checking
glDisable(GL_DEPTH_TEST);
std::cout << "OpenGL is running\n";
std::cout << "Main loop has started\n";
//Handles the main loop
bool isRunning = true;
//For handling with event
SDL_Event event;
float myX = 300; //starting x position of rectangle
float myY = 370; //starting y position of rectangle
float width = 80; //width of the rectangle
float height = 20; //height of the rectangle
bool left = false,right = false; //we save in which state the button is
//The ball variables
float ballX = 50; //x position
float ballY = 150; //y position
float ballWH = 30; //width and height of the ball
float vellX = 0.2; //x speed
float vellY = 0.2; //y speed
const int brCOUNT = 45;
brick bricks[brCOUNT];
for(int i=0,x=4,y=4;i<brCOUNT;i++,x+=66) {
if(x>560) {
x=4;
y+=30;
}
bricks[i].x = x; //Set currents bricks x position
bricks[i].y = y; //Y position
bricks[i].width = 60; //Width
bricks[i].height = 20; //Heigh
bricks[i].alive = true;
}
//Create an texture
unsigned int pad_texture = 0;
//Load the image into the texture using the function
pad_texture = loadTexture("line.png");
//Main game loop
while ( isRunning )
{
//EVENTS
while ( SDL_PollEvent(&event) )
{
//if the window was closed
if ( event.type == SDL_QUIT )
{
isRunning = false;
}
//If a button was released and the button is escape
if ( event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_ESCAPE )
{
isRunning = false;
}
if ( event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_r )
{
glClearColor(1,0,0,1);
}
if ( event.type == SDL_KEYDOWN ) //Check for presed down buttons
{
if ( event.key.keysym.sym == SDLK_LEFT ) //Check left key
{
left = true; //Set the boolean value for left key to true (it is pressed)
}
else if ( event.key.keysym.sym == SDLK_RIGHT ) //Check Right key
{
right = true; //Set the boolean value for right key to true (it is pressed)
}
}
else if ( event.type == SDL_KEYUP ) //Checking for released buttons
{
if ( event.key.keysym.sym == SDLK_LEFT ) //Left key
{
left = false; //Set the value to false (key is released)
}
else if ( event.key.keysym.sym == SDLK_RIGHT ) //Right key
{
right = false; //Key is released
}
}
//logic that should happen for a certain event
}
//LOGIC
if ( left == true ) //If left key is pressed
{
myX -= 0.5; //Move left
}
if ( right == true ) //If right key is pressed
{
myX += 0.5; //Move right
}
if ( myX < 0 ) //If the left border of the pad is over the left part of the screen
{
myX = 0; //Put the pad back so it isn't over the border
}
if ( myX+width > 600 ) //If the pad is over the right border of the screen
{
myX = 600-width; //Move it back so it only touches the right border
}
//The ball logic
ballX += vellX; //Move the ball on x axis
ballY += vellY; //move the ball on y axis
if ( ballX < 0 ) //Check if the ball hit the left edge of screen
{
vellX = -vellX; //negate the x velocity
}
else if ( ballX+ballWH>600 )
{
vellX = -vellX;
}
if ( ballY < 0 )
{
vellY = -vellY;
}
else if ( ballY+ballWH > 400 ) //if the ball hit the bottom edge of screen
{
isRunning = false; //close game
}
if(iscollission(ballX,ballY,ballWH,ballWH,myX,myY,height,width)) {
vellY = -vellY;
}
for(int i=0;i<brCOUNT;i++) {
if(bricks[i].alive) {
if(iscollission(bricks[i].x,bricks[i].y,bricks[i].height,bricks[i].width,ballX,ballY,ballWH,ballWH)) {
bricks[i].alive = false;
vellY = -vellY;
break;
}
}
}
//RENDERING to the screen
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix(); //Start rendering phase
glOrtho(0,600,400,0,-1,1); //Set the matrix
glColor4ub(255,255,255,255); //White color
//Enable textures when we are going to blend an texture
glEnable(GL_TEXTURE_2D);
//What texture we are going to use
glBindTexture(GL_TEXTURE_2D,pad_texture);
glBegin(GL_QUADS); //Start drawing the pad
//We set the corners of the texture using glTexCoord2d
glTexCoord2d(0,0); glVertex2f(myX,myY); //Upper-left corner
glTexCoord2d(1,0); glVertex2f(myX+width,myY); //Upper-right corner
glTexCoord2d(1,1); glVertex2f(myX+width,myY+height); //Down-right corner
glTexCoord2d(0,1); glVertex2f(myX,myY+height); //Down-left corner
glEnd(); //End drawing
glColor4ub(255,0,0,255); //Red color
glBegin(GL_QUADS); //Render of the ball, same method as for the pad
glVertex2f(ballX,ballY);
glVertex2f(ballX+ballWH,ballY);
glVertex2f(ballX+ballWH,ballY+ballWH);
glVertex2f(ballX,ballY+ballWH);
glEnd();
for(int i=0;i<brCOUNT;i++) {
if(bricks[i].alive != true) continue;
if(i%9==0) glColor4ub(i*5,i*5,0,1);
glBegin(GL_QUADS);
glVertex2f(bricks[i].x,bricks[i].y);
glVertex2f(bricks[i].x+bricks[i].width,bricks[i].y);
glVertex2f(bricks[i].x+bricks[i].width,bricks[i].y+bricks[i].height);
glVertex2f(bricks[i].x,bricks[i].y+bricks[i].height);
glEnd();
}
glPopMatrix(); //End rendering phase
SDL_GL_SwapBuffers();
SDL_Delay(1); //Delay / pause
}
SDL_Quit();
return 0;
}
bool iscollission(float ballx,float bally,float ballH,float ballW,float ourx,float oury,float ourH,float ourW) {
if(bally+ballH < oury) return false;
else if(ourx>ballx+ballW) return false;
else if(ourx+ourW < ballx) return false;
else if(oury+ourH < bally) return false;
return true;
}
Kaydol:
Yorumlar (Atom)
