×
Namespaces

Variants
Actions

Um simples jogo de cartas

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata

Artigo
Tradução:
Por valderind4
Última alteração feita por lpvalente em 11 Jan 2014

Contents

Introdução

Este é um jogo de cartas muito simples em Java ME. Na tela existem duas cartas com faces viradas para baixo. O usuário aposta algum montante do seu banco e clica no botão de mostar. Se a carta for maior do que a carta do computador, você ganhará um montante duplo ou perderá o seu. E depois você tira outras duas cartas.

Código fonte

cardcanvas.java

import java.io.IOException;
import java.util.Random;
 
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
 
 
public class cardcanvas extends Canvas implements CommandListener {
 
String[] card=new String[]{"clubs-a-75.png","clubs-k-75.png","clubs-q-75.png","clubs-j-75.png","clubs-2-75.png","clubs-3-75.png","clubs-4-75.png","clubs-5-75.png","clubs-6-75.png","clubs-7-75.png","clubs-8-75.png","clubs-9-75.png","clubs-10-75.png"
,"diamonds-a-75.png","diamonds-k-75.png","diamonds-q-75.png","diamonds-j-75.png","diamonds-2-75.png","diamonds-3-75.png","diamonds-4-75.png","diamonds-5-75.png","diamonds-6-75.png","diamonds-7-75.png","diamonds-8-75.png","diamonds-9-75.png","diamonds-10-75.png"
,"hearts-a-75.png","hearts-k-75.png","hearts-q-75.png","hearts-j-75.png","hearts-2-75.png","hearts-3-75.png","hearts-4-75.png","hearts-5-75.png","hearts-6-75.png","hearts-7-75.png","hearts-8-75.png","hearts-9-75.png","hearts-10-75.png"
,"spades-a-75.png","spades-k-75.png","spades-q-75.png","spades-j-75.png","spades-2-75.png","spades-3-75.png","spades-4-75.png","spades-5-75.png","spades-6-75.png","spades-7-75.png","spades-8-75.png","spades-9-75.png","spades-10-75.png"};
int[] cardrank=new int[]{14,13,12,11,2,3,4,5,6,7,8,9,10,14,13,12,11,2,3,4,5,6,7,8,9,10,14,13,12,11,2,3,4,5,6,7,8,9,10,14,13,12,11,2,3,4,5,6,7,8,9,10};
Image imgcomp=null;
Image imguser=null;
int bet=100;
int ubal=1000;
int cbal=1000;
 
boolean firsttime=true;
boolean bnew=true;
boolean bshow=false;
Command cmdbet=new Command("BET", Command.BACK, 1 );
Command cmdshow=new Command("show", Command.ITEM, 2 );
Command cmdexit=new Command("EXIT", Command.ITEM, 4 );
Command cmdnew=new Command("NEW", Command.ITEM, 3 );
int ucard,ccard;
boolean flag1=true;
boolean flag2=false;
boolean flag3=false;
 
String sUser;
public cardcanvas(String m){
 
sUser = m;
 
this.addCommand(cmdbet);
this.addCommand(cmdshow);
this.addCommand(cmdexit);
this.addCommand(cmdnew);
this.setCommandListener(this);
//System.out.print(m_Midlet);
}
 
public int rancard(){
 
Random generator = new Random();
generator.setSeed(System.currentTimeMillis());
float f = generator.nextFloat();
 
return (int)(f*52.0f)%100;
}
 
protected void paint(Graphics g) {
 
// TODO Auto-generated method stub
g.setColor(0xf0b0b0);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0x0000ff);
 
if(bnew){
try {
imgcomp = Image.createImage("/suit/back.png");
imguser= Image.createImage("/suit/back.png");
 
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
bnew=false;
}
if(bshow){
displaycard();
 
g.drawString("",6 ,115, Graphics.TOP|Graphics.LEFT);
if(ucard>ccard){
g.setColor(0x000000);
g.drawString("u win the game",70 ,150, Graphics.TOP|Graphics.LEFT);
ubal=ubal+2*bet;
repaint();
}
 
if(ucard==ccard){
g.drawString(" ITS A TIE",70 ,150, Graphics.TOP|Graphics.LEFT);
ubal=ubal+bet;
cbal=cbal+bet;
repaint();
}
if(ucard<ccard){
g.setColor(0x000000);
g.drawString(" u lost the game",70 ,150, Graphics.TOP|Graphics.LEFT);
cbal=cbal+2*bet;
repaint();
}
bshow=false;
}
g.drawImage(imgcomp,6,6, Graphics.TOP|Graphics.LEFT);
g.drawImage(imguser,156,6, Graphics.TOP|Graphics.LEFT);
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE));
g.drawString(sUser,6 ,115, Graphics.TOP|Graphics.LEFT);
g.drawString("COMPUTER ",150 ,115, Graphics.TOP|Graphics.LEFT);
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_UNDERLINED, Font.SIZE_SMALL));
g.drawString(sUser+"'s BANK:$"+ubal,5 ,250, Graphics.TOP|Graphics.LEFT);
g.drawString("COMPUTER BANK:$"+cbal,110 ,250, Graphics.TOP|Graphics.LEFT);
g.drawString(sUser+"'s BET:$"+bet,90 ,200, Graphics.TOP|Graphics.LEFT);
}
 
 
public void displaycard(){
 
int index=rancard();
int index1;
ucard=cardrank[index];
try {
imgcomp = Image.createImage("/suit/"+card[index]);
 
while(true){
index1=rancard();
if(index==index1 && cardrank[index]==cardrank[index1])
continue;
else
break;
 
}
//index=rancard();
ccard=cardrank[index1];
imguser= Image.createImage("/suit/"+card[index1]);
 
} catch (IOException e) {
 
System.out.print("image file not found");
}
//cardmidlet cm=new cardmidlet();
//String name=cm.name();
//g.drawString("image demo",getWidth()/2-10 ,getHeight()/2, Graphics.TOP|Graphics.LEFT);
 
}
 
protected void keyPressed(int keyCode){
if(keyCode==-1){
bet=bet+5;
repaint();
}
if(keyCode==-2){
bet=bet-5;
repaint();
}
//repaint();
}
 
public void commandAction(Command com, Displayable arg1) {
// TODO Auto-generated method stub
if(com==cmdshow && flag2){
bshow=true;
flag2=false;
flag3=true;
repaint();
 
 
}
if(com==cmdnew & flag3){
bnew=true;
flag3=false;
flag1=true;
repaint();
 
}
if(com==cmdbet && flag1){
ubal=ubal-bet;
cbal=cbal-bet;
flag2=true;
flag1=false;
repaint();
}
 
}
 
}

cardmidlet.java

import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
 
public class cardmidlet extends MIDlet implements CommandListener {
private Display display;
private Form form;
private List menuList ;
String[] elements=new String[]{"New Game","Credit","Help","About"};
private Command selectCommand;
Alert alert;
private Command fnext=new Command("NEXT",Command.ITEM,1);
private Command fback=new Command("BACK",Command.BACK,1);
private TextField uname;
public String nname;
 
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
}
 
protected void pauseApp() {
 
 
}
public cardmidlet() {
menuList();
 
}
 
public void menuList(){
 
display = Display.getDisplay(this);
menuList = new List("Menu", List.IMPLICIT, elements, null);
selectCommand=new Command("open",Command.ITEM,1);
menuList.setSelectCommand(selectCommand);
menuList.setCommandListener(this);
 
}
 
protected void startApp() throws MIDletStateChangeException {
display.setCurrent(menuList);
}
 
public void about(){
alert = new Alert("Option Selected", "This game is created by"+"\n"+" vivart pandey.", null, null);
alert.setTimeout(Alert.FOREVER);
alert.setType(AlertType.INFO);
display.setCurrent(alert);
}
 
public void newgame(){
display=Display.getDisplay(this);
uname=new TextField("enter your name","",50,TextField.ANY);
form =new Form("new user");
form.append(uname);
form.addCommand(fnext);
form.addCommand(fback);
form.setCommandListener(this);
display.setCurrent(form);
 
}
 
public String name(){
nname=uname.getString();
return nname;
}
 
public void commandAction(Command com, Displayable arg1) {
 
if(com==selectCommand){
if(menuList.getSelectedIndex()==3)
about();
if(menuList.getSelectedIndex()==0)
newgame();
 
}
if(com==fback){
display.setCurrent(menuList);
}
if(com==fnext){
display = Display.getDisplay(this);
cardcanvas cd=new cardcanvas(this.name());
 
display.setCurrent(cd);
}
}
 
}

Visite meu site http://www.vimviv.com

This page was last modified on 11 January 2014, at 18:28.
78 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×