javad球游戏

javad球游戏,第1张

import java.awt.Color

import java.awt.Dimension

import java.awt.Graphics

import java.awt.Graphics2D

import java.awt.RenderingHints

import java.awt.event.MouseEvent

import java.awt.event.MouseMotionListener

import java.awt.image.BufferedImage

import javax.swing.JFrame

import javax.swing.JPanel

public class CopyOfBallCrash implements Runnable {

public static void main(final String[] args) {

new Thread(new CopyOfBallCrash()).start()

}

private final int           width     = 400

private final int  旦罩         height    = 700

private int                 mouse_X, mouse_Y

private final BufferedImage offscreen = new BufferedImage(width, height,

   桐滑              局迟腊                      BufferedImage.TYPE_4BYTE_ABGR)

private final JPanel        panel     = new JPanel()

private final Shape         ball      = new Shape(100, 100, 1, 1, 20)

private final Shape         rect      = new Shape(0, 100, 20)

public CopyOfBallCrash() {

final JFrame frame = new JFrame()

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

panel.setPreferredSize(new Dimension(width, height))

ball.setBounds(width, height)

rect.setBounds(width, height)

frame.setContentPane(panel)

panel.addMouseMotionListener(new MouseMotionListener() {

@Override

public void mouseDragged(final MouseEvent e) {}

@Override

public void mouseMoved(final MouseEvent e) {

mouse_X = e.getX()

mouse_Y = e.getY()

}

})

frame.pack()

frame.setVisible(true)

}

public void paint(final Graphics g) {

final Graphics2D g2d = offscreen.createGraphics()

g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON)

g2d.fillRect(0, 0, width, height)

g2d.setColor(Color.blue)

rect.drawRect(g2d, mouse_X)

g2d.setColor(Color.red)

ball.drawOval(g2d, rect)

if (Shape.isLose) g2d.drawString("你输了!!!", 100, 300)

g2d.dispose()

g.drawImage(offscreen, 0, 0, null)

}

@Override

public void run() {

while (true)

paint(panel.getGraphics())

}

}

class Shape {

public int            width, height

public int            x, y, vx, vy, r, w, h

public static boolean isLose

public Shape(final int x, final int y, final int vx, final int vy, final int r) {

super()

this.x = x

this.y = y

this.vx = vx

this.vy = vy

this.r = r

}

public Shape(final int x, final int w, final int h) {

super()

this.x = x

this.w = w

this.h = h

}

public final void setBounds(final int width, final int height) {

this.width = width

this.height = height

}

public final void drawOval(final Graphics2D g2d, final Shape shape) {

if (y + h >= height) {

isLose = true

return

}

if (x + vx <= 0 || x + vx + w >= width) vx = -vx

if (y + vy <= 0) vy = -vy

if (isCrashOutside(shape.x, shape.y, shape.w, shape.h) &&y + w >= shape.y)

vy = -vy

x += vx

y += vy

g2d.fillOval(x, y, r, r)

}

public final void drawRect(final Graphics2D g2d, final int mouseX) {

y = height - h

if (x + w <width &&x <mouseX) x++

if (x >0 &&x >mouseX) x--

g2d.fillRect(x, y, w, h)

}

public final boolean isCrashOutside(final int x, final int y, final int w,

final int h) {

return (this.x >x ? this.x <= w + x : x <= r + this.x)

&&(this.y >y ? this.y <= h + y : y <= r + this.y)

}

}

已使用TC2.0 IDE测试,正常运行:

引用:

#include "graphics.h"

#include "math.h"

#define LEFT 0x4b00

#define RIGHT 0x4d00

#define ESC 0x011b

int x,y,x1,y1,key,t1,t2,keyx,keylong,keymove,over=0,i,tt=0,fast

int ballx[9]={53,103,263,120,236,390,410,450,600}

int bally[9]={158,239,120,113,30,313,120,263,231},ballz[9]={0}

float ls

win()

{float lx,ly,ll

for(i=0i<9i++)

{

if(ballz==1)/*如果球体已经消失就不判断*/

continue

lx=(float)(ballx-x)

ly=(float)bally-y

ll=lx*lx+ly*ly

ls=sqrt(ll)

if((int)ls<17)/*判断两球是否碰到*/

{tt++/*记森困镇录消失几个球*/

fast-=50/*速度加快*/

if(i%2==0)/*移动加快*/

keymove+=5

else

{

setcolor(0)

rectangle(keyx,460,keyx+keylong,470)

keylong-=10/*长条变短*/

setcolor(YELLOW)

rectangle(keyx,460,keyx+keylong,470)

}

ballz=1

setcolor(0)

circle(ballx,bally,10)

if(y<bally)

if(x<ballx)

{t1=-1t2=-1}

else

{t1=1t2=-1}

else

if(x<ballx)

{t1=-1t2=1}

else

{t1=1t2=1}

}

}

}

fungo()

{ x=100,y=50,keyx=200,keylong=100,keymove=15,fast=600

for(i=0i<9i++)

{

setcolor(i+1)

circle(ballx,bally,10)

}

sleep(1)

t1=t2=1

while(1)

{

while(!kbhit())

{

setcolor(YELLOW)

rectangle(keyx,460,keyx+keylong,470)

x1=x

y1=y

x+=t1

y+=t2

setcolor(4)

circle(x,y,6)

delay(fast)

setcolor(0)

circle(x,y,6)

win()

if(tt==9)/*胜利*/

{

cleardevice()

over=1

setcolor(RED)

settextstyle(0,0,4)

setusercharsize(3,1,3,1)

outtextxy(200,200," YOU WIN! ")

break

}

if(y>480)/*失此粗败*/

{

cleardevice()

over=1

setcolor(RED)

settextstyle(0,0,4)

setusercharsize(3,1,3,1)

outtextxy(200,200,"GAME OVER")

break

}

else if(x==11&&y==11)/*判尺烂断碰到东西*/

{t1=1t2=1}

else if(x==629&&y==11)

{t1=-1t2=1}

else if(x==11)

{

if(y<y1)

{t1=1t2=-1}

else

{t1=1t2=1}

}

else if(y==11)

{

if(x<x1)

{t1=-1t2=1}

else

{t1=1t2=1}

}

else if(x==629)

{

if(y1>y)

{t1=-1t2=-1}

else

{t1=-1t2=1}

}

else if(y==449)

{

if(x>=keyx&&x<=keyx+keylong)

{

if(x<(keyx+keylong/4))

{t1=-1t2=-1}

else if(x>=(keyx+keylong/4)&&x<(keyx+keylong/2))

{t1=-1t2=-2}

else if(x>=(keyx+keylong/2)&&x<(keyx+keylong/4*3))

{t1=1t2=-2}

else

{t1=1t2=-1}

}

}

}

if(over)

break

key=bioskey(0)

setcolor(0)

rectangle(keyx,460,keyx+keylong,470)

if(key==ESC)

{cleardevice()break}

else if(key==LEFT)

keyx-=keymove

else if(key==RIGHT)

keyx+=keymove

}

}

main()

{int gd=DETECT,gr

initgraph(&gd,&gr,"c:\\tc")

fungo()

getch()

sleep(1)

closegraph()

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12511050.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-26
下一篇 2023-05-26

发表评论

登录后才能评论

评论列表(0条)

保存