⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ball.java

📁 手机弹球游戏源码
💻 JAVA
字号:
/* * Copyright (c) 2002, 2003 Sun Microsystems, Inc. All Rights Reserved. * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * - Redistribution of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * - Redistribution in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * Neither the name of Sun Microsystems, Inc., 'Java', 'Java'-based * names, or the names of contributors may be used to endorse or promote * products derived from this software without specific prior written * permission. * This software is provided "AS IS," without a warranty of any kind. ALL * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN * MIDROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. * You acknowledge that this software is not designed, licensed or * intended for use in the design, construction, operation or maintenance * of any nuclear facility. $Id: Ball.java,v 1.3 2003/05/29 22:28:05 wildcard Exp $ */package com.sun.j2me.blueprints.jbricks;import javax.microedition.lcdui.Graphics;/** * This class implements the ball, with its radius, * and x and y step size. The ball knows how to draw * itself, and every once in a while it will do a * subtle, pseudo-random path modification to prevent * closed ball paths. */public class Ball extends Sprite {    private int dx;    private int dy;    private int xo;    private int yo;    public static final int RADIUS = Math.max(2, Screen.width / 55);    private ThreeDColor color;    private ThreeDColor brighter;    private ThreeDColor darker;    private int counter;    private int offset;    public Ball(int x, int y, int dx, int dy) {        moveTo(x, y);        setSteps(dx, dy);        offset = 0;        width = 2 * RADIUS;        height = 2 * RADIUS;        this.dx = dx;        this.dy = dy;        xo = yo = 0;        color = ThreeDColor.red;        brighter = color.brighter();        darker = color.darker();    }    public void setSteps(int dx, int dy) {        this.dx = dx;        this.dy = dy;    }     public int getXStep() {        return dx;    }     public int getYStep() {        return dy;    }     public void move() {        x = x + dx + xo;        y = y + dy + yo;        xo = yo = 0;    }     public void bounceHorizontal() {        dx = -dx;        if (Util.getRandomInt(0, 1000) < 70) {            xo = dx < 0 ? -1 : 1;        }     }     public void bounceVertical() {        dy = -dy;        if (Util.getRandomInt(0, 1000) < 70) {            yo = dy < 0 ? -1 : 1;        }     }     public void bounce(Sprite other) {        int cx, cy;        cx = getCenterX();        cy = getCenterY();        if (dx < 0) {            if (cy >= other.y && cy < other.y + other.height                     && x < other.x + other.width) {                dx = Math.abs(dx);            }         } else {            if (cy >= other.y && cy < other.y + other.height                     && x + width >= other.x) {                dx = -Math.abs(dx);            }         }         if (dy < 0) {            if (cx >= other.x && cx < other.x + other.width                     && y < other.y + other.height) {                dy = Math.abs(dy);            }         } else {            if (cx >= other.x && cx < other.x + other.width                     && y + height >= other.y) {                dy = -Math.abs(dy);            }         }         if (Util.getRandomInt(0, 1000) < 70) {            xo = dx < 0 ? -1 : 1;        }         if (Util.getRandomInt(0, 1000) < 70) {            yo = dy < 0 ? -1 : 1;        }     }     public void paintShadow(Graphics g) {        g.setColor(ThreeDColor.black.getRGB());        g.fillArc(x + shadow, y + shadow, width, height, 0, 360);    }     public void paint(Graphics g) {        if (++counter == 6) {            counter = 0;            offset = 1 - offset;        }         g.setColor(color.getRGB());        g.fillArc(x, y, width, height, 0, 360);        g.setColor(brighter.getRGB());        g.drawArc(x + 2, y + 2, width - 4, height - 4, 180 + offset, -90);        g.setColor(darker.getRGB());        g.drawArc(x, y, width, height, 0, -90);    } }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -