hey bitfogav,
i ported your code to mikro c
but for the ball prototype your created in swordfish with the GLCD.square function takes only 3 parameters, x1,y1 and size of the square, in mikro C the square function takes 4 parameters so i just added the x and y parameters again since it's a square.
the problem is that when i compile the code in mikro c, it gives an error on the Glcd_Init(&PORTB,0,1,2,3,5,4,&PORTD); routine, which is a first, as the other times i have used it in other programs it worked just fine.
this is the ported code by the way
Code:
//header file for mapping the buttons to port pins
#include "button_check.h"
#define Pressed 0
//*****************************************************************************
//* Name : CAPTONG - *
//* Author : [Captalex and his minions] *
//* Notice : Copyright (c) 2011 [select VIEW...EDITOR OPTIONS] *
//* : All Rights Reserved *
//* Date : 10/june/2011 *
//* Version : 0.0.1 *
//* Notes : a one player pong game , my first try at graphics *
//* : microcontroller programming. *
//* : *
//*****************************************************************************
/**ALL Constants, Variables and Identifiers **/
//use ints
int topScreen = 2; // set paddle max travel (top display)
int bottomScreen = 41; // set paddle max travel (bottom display)
int wall_Left = 1; // set left wall position for hit detection
int wall_Bottom = 60; // set bottom wall position for hit detection
int wall_Top = 1; // set top wall position for hit detection
int ballMissPaddle = 130; // set max value for ball missing paddle
//how do i change Dim to match mikro C format and use Byte and word??? can i just use int instead for Byte ?
// ball data variables
int ballX; // ball initial x coordinate
int ballY; // ball initial y coordinate
int ballX1; // hold previous x values into a new varaiable
int ballY1; // hold previous y values into a new varaiable
int ballSize; // initial size for the ball
int ballSpeed; // ball speed
// ball direction ints ,I set as ints, because mikroc has no bool variable
int Ball_Hit_Top;
int Ball_Direct_Up;
int Ball_Direct_Left;
int Ball_Hit_Left;
// paddle position data
int paddleX1; //paddle initial x coordinate
int paddleY1; //paddle initial y coordinate
int paddleX2; //paddle final x coordinate
int paddleY2; //paddle final x coordinate
//Function prototypes for each of the games functions
/* draws a border for the game */
void init_border()
{
// draws line (top of screen)
Glcd_Line(0,0,127,0,1);
// draws line (left of screen)
Glcd_Line(0,0,0,63,1);
// draws line (bottom of screen)
Glcd_Line(0,63,127,63,1);
} //for init_border();
/*initailizes the game event variables*/
void init_variables()
{
ballX = 64 ; // starting position X for ball
ballY = 36 ; // starting position Y for ball
ballX1 = ballX+1; //set old x values into a new varaiable
ballY1 = ballY+1; //set old y values into a new varaiable
ballSize = 2 ; // ball size fixed
ballSpeed = 30 ; // ball speed
// Boder wall flags
Ball_Hit_Top = 0; // Ball_Hit_Top = 0;
Ball_Direct_Up = 0; // Ball_Direct_Up = 0;
Ball_Direct_Left = 0; // Ball_Direct_Left = 0;
Ball_Hit_Left = 0; // Ball_Hit_Left = 0;
// starting point of paddle X,Y
paddleX1 = 120 ; //
paddleY1 = 15 ; // starting point of paddle X,Y
paddleX2 = 121 ; //
paddleY2 = 35 ; //
} //for init_variables();
void ball (int ballX1 , int ballY1, int ballX1, int ballY1)
{ //help here how do i implement the ball function using word in mikro c
// draw ball on screen X,Y (ball size)
Glcd_Box(ballX1,ballY1,ballX1,ballY1,1);
} //for ball();
/*the 4 possible ball's directions (left-right-up-down) */
void ballDirect()
{
if (Ball_Direct_Left == 1)
{ // move the ball left
ballX--; }
else
{ ballX++; //move the ball right
} //for first if
if (Ball_Direct_Up == 1)
{ // move the ball direction up
ballY--; }
else
{ ballY++; //move the ball down
} //for second if
} //for ballDirect();
/*checks if ball has hit bottom or the top if the screen */
void checkWalls()
{
if (( ballX == wall_Left ) && (Ball_Hit_Left == 1))
{ // check wall left
Ball_Direct_Left = 0; //Ball_Direct_Left = 0;
Ball_Hit_Left = 0; //Ball_Hit_Left = 0;
} //for first if
else if ((ballY == wall_Top) && (Ball_Hit_Top ==1))
{ // check wall top
Ball_Direct_Up = 0; //Ball_Direct_Up = 0;
Ball_Hit_Top = 0; //Ball_Hit_Top = 0;
} //for second if
// else ((ballY = wall_Bottom) && (Ball_Hit_Top = false))
// { // check wall bottom
// Ball_Direct_Up = 1; //Ball_Direct_Up = 1;
// Ball_Hit_Top = 1; //Ball_Hit_Top = 1;
// } //for third if
} //for checkWalls();
/* draws the ball on the lcd screen*/
void drawBall()
{
ball(ballX,ballY,ballX1,ballY1); // draw new ball on screen
Glcd_Set_Side(ballX+1);
Glcd_Set_X(ballX+1);
//Glcd_Set_Page(ballY+1); // set pixel in middle of ball
//delay_ms(ballSpeed); // delay so we can see the ball on the screen, also see the ball's speed
Glcd_Box(ballX,ballY,ballX1,ballY1,0);
ball(ballX,ballY,ballX1,ballY1); // clears ball previous pos (can only be done by deleting pixels)
Glcd_Box(ballX,ballY,ballX1,ballY1,1);
ball(ballX,ballY,ballX1,ballY1); // draw new ball on screen
// set pixel in middle of ball
Glcd_Set_Side(ballX+1);
Glcd_Set_X(ballX+1);
Glcd_Set_Page(ballY+1);
delay_ms(10); // delay so we see ball on screen, also ball speed
Glcd_Box(ballX,ballY,ballX1,ballY1,0);
ball(ballX,ballY,ballX1,ballY1); // clears ball previous pos (can only be done by deleting pixels)
Glcd_Box(ballX,ballY,ballX1,ballY1,1);
} //for drawBall();
void update_paddle1()
{
//button 2 is up
if ((Switch_up == Pressed) && ( paddleY1 >= topScreen))
{ // check button up & check paddle max travel top
Glcd_Rectangle(paddleX1,paddleY1,paddleX2,paddleY2,0);
Glcd_Rectangle(paddleX1,paddleY1,paddleX2,paddleY2,1);
paddleY1 = paddleY1 -1; // set paddle to go up on display
paddleY2 = paddleY2 -1; //
delay_ms(3); // delay to slow down paddle movement
} //for up if condition
//button1 is down
if ((Switch_down == Pressed) && ( paddleY1 <= bottomScreen))
{ // check button down & check paddle max travel bottom
Glcd_Rectangle(paddleX1,paddleY1,paddleX2,paddleY2,0); // clear paddle previous data
Glcd_Rectangle(paddleX1,paddleY1,paddleX2,paddleY2,1);
paddleY1 = paddleY1 +1; // set paddle to go down on display
paddleY2 = paddleY2 +1; //
delay_ms(3); // delay to slow down paddle movement
} //for down if condition
Glcd_Rectangle(paddleX1,paddleY1,paddleX2,paddleY2,1); // update our NEW paddle pos data
} //for update_paddle1();
/*when ball hits paddles select the direction it goes in*/
void checkBallHitPaddle()
{
// check to see if our ball has hit the paddle
if (ballX >= paddleX1-2) && (ballX <= paddleX2-2) && (ballY >= paddleY1) && (ballY <= paddleY2)
{
Ball_Direct_Left = 1; // change ball direction Ball_Direct_Left = 1;
Ball_Hit_Left = 1; // change ball direction Ball_Hit_Left = 1;
}
else if (ballX = ballMissPaddle)
{ // check to see if ball missed our paddle
// reset game
init_variables(); // setup variables , init_variables();
Glcd_Fill(0); // Glcd_Fill(0); clear display before we start main prog
init_border(); // setup display border , init_border();
} // for first else if }
// for if
} //for checkBallHitPaddle();
void main()
{
ADCON1 = 0x0F; // set MCU pins as digital and Set AN pins to Digital I/O
Glcd_Init(&PORTB,0,1,2,3,5,4,&PORTD); // (CONTROL PORT,cs1,cs2,[(d/i)/rs],r/w,rst,en,DATAPORT)
Glcd_Fill(0); // clear display before we start main prog
TRISA = 0xFF; // set PORTA bits 1-5 Inputs (buttons) ,TRISA = 0xFF; // RA0 input
TRISC = 0; // PORTC outputs, for led indicator to show game is on
PORTC.F2 = 0; // Turn OFF LED indicator on port RC2
/*Welcome message*/
Glcd_Write_Text("A",8,1,1);
Glcd_Write_Text("PONG",12,2,1);
Glcd_Write_Text("GAME",15,3,1);
Glcd_Write_Text("BY",8,4,1);
Glcd_Write_Text("CAPTALEX",15,5,1);
Glcd_Write_Text("ENJOY",15,6,1);
// Main Game loop
while (1)
{
init_variables(); // setup game variables
init_border(); // setup display border
ballDirect(); // ball direction
checkWalls(); // check to see if ball hit any walls
drawBall(); // draws ball on display
update_paddle1(); // update paddle on display
checkBallHitPaddle(); // check to see if ball has hit our paddle
} // for end of main game while condition
} // for void main
and the header for Button config
Code:
//header file for mapping the buttons to port pins in mikro c
#define Switch_up PORTA.F0 // button2
#define Switch_right PORTA.F1
#define Switch_down PORTA.F2 // button1
#define Switch_left PORTA.F3
#define Switch_Start PORTA.F4
do help.