Java Program for working Pacman game

Java Program/source code for working Pacman Game. This program is primarily based on the game pacman….. A running pacman eating piece according to light signals red ,green or yellow just run this java program to see live working pacman game ,
It consist of three (2 java files + 1 html file ) files :
1)PackManAndRedLight.java
2)RedLight.java
3)redlight+packman.html

Steps to run Java Program/source code for working Pacman Game :

Copy the code and save three files in same folder with names as mentioned above

compile two files at a time via running this command on cmd :

javac *.java

after that open redlight+packman.html in your browser

Done 🙂

so here is the source code for running pacman game

import java.awt.Color;
import java.awt.Graphics;
import java.applet.Applet;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JApplet;
import java.applet.AppletContext;

class mythread1 extends Thread
{
PackManAndRedLight pack;
char dir;
mythread1(PackManAndRedLight pack)
{
this.pack=pack;
pack.x=0;
pack.y=0;
pack.ang=315;
pack.stang=23;
dir='r';
start();
}
public void run()
{
try
{
for(;pack.y<=550;)
{
if(pack.rd.clr=='g')
{
pack.repaint();
if(dir=='r')
{
pack.x+=5;
pack.recy=pack.y;
pack.recx=pack.x-25;
pack.repaint();
if(pack.x>760)
{
pack.recx=740;
pack.repaint();
pack.showStatus(""+pack.rd.clr);
dir='d';
}
if(pack.x%50==45)
{
pack.ang=360;
pack.stang=270;
}
else
{
pack.stang=23;
pack.ang=315;
}
}
if((dir=='d'))
{
pack.recx=pack.x;
pack.recy=pack.y-25;
pack.y+=5;
if(pack.y%50==0)
{
if(pack.x>50)
{
pack.repaint();
pack.showStatus(""+pack.rd.clr);
dir='l';
}
else
dir='r';
}
if(pack.y%50==30)
{
pack.ang=360;
pack.stang=270;
}
else
{
pack.stang=293;
pack.ang=315;
}
}
if(dir=='l')
{
pack.recy=pack.y;
pack.recx=pack.x+25;
//pack.stang=203;
pack.x-=5;
if(pack.x<20)
dir='d';
if(pack.x%50==30)
{
pack.ang=360;
pack.stang=0;
}
else
{
pack.ang=315;
pack.stang=203;
}
}
Thread.sleep(50);
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
public class PackManAndRedLight extends JApplet implements MouseMotionListener
{
int x,y,stang,ang;
int recx,recy;
boolean flag;
mythread1 mth;
String str="";
RedLight rd;
@Override
public void init()
{
flag=false;
x=0;
y=0;
stang=23;
ang=315;
addMouseMotionListener(this);
AppletContext ct=getAppletContext();
rd=(RedLight)ct.getApplet("redlight");
System.out.println(""+rd);
}
public void start()
{
flag=false;
mth=new mythread1(this);
}
@Override
public void mouseDragged(MouseEvent e)
{

}
@Override
public void mouseMoved(MouseEvent e)
{
str=(" x = "+e.getX()+" y = "+e.getY());
}
@Override
public void paint(Graphics g)
{
if(flag==false)
{
flag=true;
int x1=75,y1=25;
for(;y1<600;)
{
g.setColor(Color.red);
g.fillOval(x1+10, y1-5, 10, 10);
if(x1>750)
{
x1=-25;
y1+=50;
}
x1+=50;
}
}
g.setColor(Color.white);
g.fillRect(recx, recy, 50,50);
g.setColor(Color.yellow);
g.fillArc(x, y, 50,50, stang, ang);showStatus(str+rd.clr);
}
}

RedLight.java

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
class red_thread extends Thread
{
 char lclr;
 RedLight rl;
 red_thread(RedLight rl)
 {
 this.rl=rl;
 start();
 }
 @Override
 public void run()
 { 
 while(true)
 {
 try
 {
 if(rl.clr=='r')
 {
 Thread.sleep(10000);
 lclr='r';
 rl.clr='y'; 
 }
 else if(rl.clr=='g')
 {
 Thread.sleep(10000);
 lclr='g';
 rl.clr='y';
 }
 else if(rl.clr=='y')
 {
 Thread.sleep(3000);
 if(lclr=='r')
 rl.clr='g';
 else
 rl.clr='r';
 }
 rl.repaint();
 }
 catch(Exception e)
 {
 e.printStackTrace();
 }
 }
 } 
}
public class RedLight extends Applet 
{
 int clr='r';
 public void init() 
 { 
 new red_thread(this); 
 }
 @Override
 public void paint(Graphics g)
 {
 g.fillRect(40,40,120,360);
 g.setColor(Color.DARK_GRAY);
 g.fillOval(50,50,100,100);
 g.fillOval(50,170,100,100);
 g.fillOval(50,290,100,100);
 if(clr=='r')
 {
 g.setColor(Color.red);
 g.fillOval(50,50,100,100);
 }
 else if(clr=='y')
 {
 g.setColor(Color.orange);
 g.fillOval(50,170,100,100);
 }
 else
 {
 g.setColor(Color.green);
 g.fillOval(50,290,100,100);
 } 
 }
}

redlight+packman.html


Output Screenshots of working Pacman game

Java Program for working Pacman Redlight2. 3.

java Program/source code for working PacmanSo, this was all for now about the JAVA Program for working Pacman Game. To know more about programming and more such stuff, check out https://www.worldtopupdates.com/.

Read More
admin February 18, 2014 0 Comments

Write a C program to generate Pascal’s triangle

Examples to  Pascal’s Triangle  in C Programming using control statements.

 

You might have studied while studying Binomial Theorem in Mathematics. Read below for the best Information

In mathematics, Pascal’s triangle is a triangular array of the binomial coefficients. In much of the Western world, it is named after the French mathematician Blaise Pascal, although other mathematicians studied it centuries before him in India,

 

 

/* Write a C program to generate Pascal's triangle. */
#include<stdio.h>
#include<conio.h>
void main()
{
int bin,p,q,r,x;
clrscr();
bin=1;
q=0;
printf("Rows you want to input:");
scanf("%d",&r);
printf("\nPascal's Triangle:\n");
while(q<r)
{
for(p=40-3*q;p>0;--p)
printf(" ");
for(x=0;x<=q;++x)
{
if((x==0)||(q==0))
bin=1;
else
bin=(bin*(q-x+1))/x;
printf("%6d",bin);
}
printf("\n");
++q;
}
getch();}

 

Read More
admin February 18, 2014 0 Comments

java program to print fibonacci series

fibonacci series

java-Fibnocassi mkjgvfhffdfdrdclass Fibonacci
{
public static void main ( String s [] )
{ int n2 =1 ;int n1 =0 ;
System.out.println(n1);
System.out.println (n2);
int sum ;
for ( int i = 0;i <25 ;i++)
{
sum = n1+n2;
n1 = n2 ;
n2 = sum;
System.out.println ( sum );
}


}
}

 

Read More
admin February 18, 2014 0 Comments

java program for checking prime number

checking prime number

import java.util.Scanner ;
class CheckPrime
{
public static void main ( String s[] )
{
System.out.println ( " Enter a number: ");

Scanner value = new Scanner ( System.in );
int number = value.nextInt ( );
int flag =0;
int c = (int) Math.sqrt( number );

 

 

 
for ( int i = 2 ; i <=c ; i++ )
{
if ( (number % i ) == 0 )
{

System.out.println ( " the number" + number + " is not a prime number ");

flag =1 ; break ; }

}

if (flag == 0 )

System.out.println ( " the number is a prime number" );
}
}

Read More
admin February 18, 2014 0 Comments

java program for checking palindrome number

Palindrome Program in Java

In this program, you’ll learn to check whether a number is palindrome or not in Java. This is done by using for and while loop.

Java-program-for-checking-palindrome-number

Palindrome number in java:

A palindrome number is a number that is same after reverse. For example 141, 757, 38583, 747, 171, 52925 are the palindrome numbers. It can also be a string like LOL, NITIN etc.

 

Palindrome number algorithm

  • Get the number to check for palindrome
  • Hold the number in temporary variable
  • Reverse the number
  • Compare the temporary number with reversed number
  • If both numbers are same, print “palindrome number”
  • Else print “not palindrome number”

 

Java program for checking palindrome number :

 

import java.io.* ;
import java.util.Scanner;
class CheckPalindrome
{
public static void main (String s[] )
{

Scanner console = new Scanner(System.in);
int number ;

System.out.println( " Enter the array size " );
int size = console.nextInt();

int array[] = new int[size];
for ( int i=0; i<size;i++)
{
System.out.println ( "Enter the number : array["+i+"]");
number = console.nextInt() ;

array[i] = number ;
}

int c = array.length-1 ;
for ( int i = 0; i < array.length ; i++)
{
if ( array[i] == array[c])
{
c--;
}
}

if (c == (-1) )
{
System.out.println ( " this array is a palindrome ");
}
else
{System.out.println( " this is not a palindrome " );
}
System.out.println( " this is the second method to solve palindrome " );

int flag1 = 1;
for(int i = 0, k = array.length-1; i<array.length&k>(-1);i++,k--)
{
if ( array[i] != array[k])
{
System.out.println ("this is not a palindrome");
flag1 = 0; break ;
}

}
if (flag1==1){
System.out.println( " this is a palindrome " );
}
}
}

 

Read More
admin February 18, 2014 0 Comments

java program for checking even and odd number

Java program for checking even and odd number

import java.util.Scanner;
class Check
{
public static void main ( String []s)
{

Scanner value = new Scanner( System.in);
System.out.println ( " Enter the number : ");
int number = value.nextInt();

if ( number %2 == 0)
{
System.out.println ( " the number "+ number + " is even number ");
}
else
{ System.out.println( " the number is odd " );
}
System.out.println( " using bitwise and operator " );
System.out.println ( " Enter another number ; ");
Scanner abc = new Scanner( System.in );
int number1 = abc.nextInt();
if ( (number1 & 1) == 0)
{
System.out.println ( " it is an even number ");
}
else
{ System.out.println ( " it is an odd number " );
}

}
}

Read More
admin February 18, 2014 0 Comments