vs2019窗体设计-vs窗体设计移动小球
ja程序:编写一个程序,让一个小球在JFrame中滚动,当碰边缘时则选择一个角度返回.
05年写的,你修改一下吧
/*
* 一个在窗体中来回运动的圆.ja
*
* Created on 2005年10月5日, 下午1:02
*
* To change this template, choose Tools | Options and locate the template under
* the Source Creation and Management node. Right-click the template and choose
* Open. You can then make changes to the template in the Source Editor.
*/
package jalication1;
import ja.let.Applet;
import ja.awt.Color;
import ja.awt.Event;
import ja.awt.Graphics;
import ja.let.*;
import ja.awt.*;
import ja.math.*;
/**
*
* @author Bachelorlrz
*/
public class 在窗体中来回运动的圆 extends ja.let.Applet implements ja.lang.Runnable {
int cx,cy,c1x,c1y; //圆的坐标
int cw,ch; //圆的宽高
int bx,by,bw,bh; //背景的坐标和宽高
int dx,dx1; //圆的运动
int r,rx,ry; //圆的半径和位置
int r1,r1x,r1y;
Thread u_thread;
/** Initialization method that will be called after the let is loaded
* into the browser.
*/
public void init() {
rx=150; ry=160;
bw=500; bh=400;
r1x=bw/2-cw*2; r1y=bh/2-ch;
r=60; r1=200;
cx =rx; cy =ry;
c1x=r1x; c1y=r1y;
cw=30; ch=30;
bx=2; by=2;
dx=1; dx1=2;
// TODO start asynchronous download of hey resources
}
// TODO overwrite start(), stop() and destroy() methods
public void update(ja.awt.Graphics g) {
super.paint(g);
g.setColor(Color.red);
g.drawRect(bx,by,bw,bh);
g.setColor(Color.BLACK);
g.fillRect(bx+2,by+2,bw-4,bh-4);
g.drawString("在窗体中来回运动的圆", bw/2-60, bh/2);
if (cx<rx-r || cx>rx+r) {
dx = -dx;
}
if (c1x<r1x-r1 || c1x>r1x+r1) {
dx1 = -dx1;
}
cx =cx+dx;
cy =(int)(dx*Math.sqrt(r*r-(cx-rx)*(cx-rx)))+ry;
c1x =c1x+dx1;
c1y =(int)(dx1/2*Math.sqrt(r1*r1-(c1x-r1x)*(c1x-r1x))/2);
// g.drawArc(cx, cy, cw, ch, 0, 360);
for(int i=0;i<8;i++){
if (i%5 == 0){
g.setColor(Color.black);
}else if ( i%5== 1) {
g.setColor(Color.GREEN);
}else if(i%5==2){
g.setColor(Color.RED);
}else if( i%5 ==3){
g.setColor(Color.pink);
}else {
g.setColor(Color.orange);
}
g.drawLine(bx,by, cx+10,cy+i+10);
g.drawLine(bx+bw,by+bh,cx+10,cy+i+10);
g.drawLine(bx+bw,by, cx+10,cy+i+10);
g.drawLine(bx,by+bh, cx+10,cy+i+10);
g.drawLine(bx,by,-cx+bw-bx-cw+10,cy+i+10);
g.drawLine(bx+bw,by+bh,-cx+bw-bx-cw+10,cy+i+10);
g.drawLine(bx+bw,by,-cx+bw-bx-cw+10,cy+i+10);
g.drawLine(bx,by+bh,-cx+bw-bx-cw+10,cy+i+10);
g.drawLine(bx,by, c1x+10,c1y+r1y+i+10);
g.drawLine(bx+bw,by+bh,c1x+10,c1y+r1y+i+10);
g.drawLine(bx+bw,by, c1x+10,c1y+r1y+i+10);
g.drawLine(bx,by+bh, c1x+10,c1y+r1y+i+10);
g.drawLine(bx,by, r1x+r1+cw-c1x+10,-c1y+r1y+i+10);
g.drawLine(bx+bw,by+bh,r1x+r1+cw-c1x+10,-c1y+r1y+i+10);
g.drawLine(bx+bw,by, r1x+r1+cw-c1x+10,-c1y+r1y+i+10);
g.drawLine(bx,by+bh,r1x+r1+cw-c1x+10,-c1y+r1y+i+10);
g.drawArc(cx+i, cy+i, cw-i*2, ch-i*2, 0, 360);
g.drawArc(-cx+bw-bx-cw+i, cy+i, cw-i*2, ch-i*2, 0, 360);
g.drawArc(c1x+i, c1y+r1y+i, cw-i*2, ch-i*2, 0, 360);
g.drawArc(r1x+r1+cw-c1x+i, -c1y+r1y+i, cw-i*2, ch-i*2, 0, 360);
}
}
public void start(){
if (u_thread == null)
{
u_thread = new Thread(this);
u_thread.start();
}
}
public void run() {
while(true){
repaint();
try{
u_thread.sleep(10);
}
catch (InterruptedException e){
return;
}
}
}
}
如何使用VB6.0设计一个弹球的游戏,
这个吗 不是很复杂
说下过程吧
先那一个窗体
再建一个小球
然后让小球动起来 用时间函数就行
用直线方程控制运动方向 遇到边界以后判断一下
反弹回 方向也用 直线方程 按反射定律弹回
vb做一个和游戏一样的 在窗体上点哪人(或控件)走到哪。要有运动轨迹哦。
Dim?mx?As?Single,?my?As?Single,?sx?As?Single,?sy?As?Single
Private?Sub?Form_Load()
Timer1.Enabled?=?False
Timer1.Interval?=?50
End?Sub
Private?Sub?Form_MouseUp(Button?As?Integer,?Shift?As?Integer,?X?As?Single,?Y?As?Single)
mx?=?X?-?Shape1.Width?/?2
my?=?Y?-?Shape1.Height?/?2
sx?=?(mx?-?Shape1.Left)?/?20
sy?=?(my?-?Shape1.Top)?/?20
Timer1.Enabled?=?True
End?Sub
Private?Sub?Timer1_Timer()
If?Abs(mx?-?Shape1.Left)?<?Abs(sx)?And?Abs(my?-?Shape1.Top)?<?Abs(sy)?Then
Shape1.Move?mx,?my
Timer1.Enabled?=?False
ElseIf?Abs(mx?-?Shape1.Left)?<?Abs(sx)?Then
Shape1.Move?mx,?Shape1.Top?+?sy
ElseIf?Abs(my?-?Shape1.Top)?<?Abs(sy)?Then
Shape1.Move?Shape1.Left?+?sx,?my
Else
Shape1.Move?Shape1.Left?+?sx,?Shape1.Top?+?sy
End?If
End?Sub
以上代码使用了两个控件:Timer1和Shape1,Shape1可设为小球形状,这样比较形象。
c#中窗体程序设计如何让小球在窗体中沿水平方向左右撞击
只说思路。
给小球添加一个水平速度属性,然后写一个While(true)循环,里面将小球的水平坐标+=速度,并且当小球的水平坐标距离窗体边缘距离小于等于小球半径的时候速度*=-1。
vb小球碰壁游戏加强版思路,向大大们请教下.
设置窗体名称frmZQ,StartUpPosition=屏幕中心,ScaleMode=3
添加两个Shape1,Shape2
一个时钟控件Timer1
代码如下:
Dim D As Integer '发球方向
Dim F As Boolean '发号
Dim V As Integer '移动距离
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case 32
Timer1.Enabled = True
F = True
Case 37
Shape1.Left = Shape1.Left - V
If Shape1.Left <= 0 Then Shape1.Left = 0
If F = False Then Shape2.Left = Shape1.Left + (Shape1.Width - Shape2.Width) / 2: D = 2
Case 39
Shape1.Left = Shape1.Left + V
If Shape1.Left >= frmZQ.ScaleWidth - Shape1.Width Then Shape1.Left = frmZQ.ScaleWidth - Shape1.Width
If F = False Then Shape2.Left = Shape1.Left + (Shape1.Width - Shape2.Width) / 2: D = 4
Case Else
End Select
End Sub
Private Sub Form_Load()
With Shape1
.Left = (frmZQ.ScaleWidth - .Width) / 2
.Top = 260
End With
With Shape2
.Left = (frmZQ.ScaleWidth - .Width) / 2
.Top = 240
End With
V = 10
End Sub
Private Sub Timer1_Timer()
Select Case D
Case -4
Shape2.Left = Shape2.Left + V: Shape2.Top = Shape2.Top + V
If Shape2.Left >= frmZQ.ScaleWidth - Shape2.Width Then D = -2
If Shape2.Top >= 240 And (Shape1.Left - Shape2.Left >= -10 And Shape1.Left - Shape2.Left <= 70) Then D = 4
If Shape2.Top >= frmZQ.ScaleHeight Then Unload Me
Case -3
Case -2
Shape2.Left = Shape2.Left - V: Shape2.Top = Shape2.Top + V
If Shape2.Left <= 0 Then D = -4
If Shape2.Top >= 240 And (Shape1.Left - Shape2.Left >= -10 And Shape1.Left - Shape2.Left <= 70) Then D = 2
If Shape2.Top >= frmZQ.ScaleHeight Then Unload Me
Case -1
Case 0
Case 1
Case 2
Shape2.Left = Shape2.Left - V: Shape2.Top = Shape2.Top - V
If Shape2.Left <= 0 Then D = 4
If Shape2.Top <= 0 Then D = -2
Case 3
Case 4
Shape2.Left = Shape2.Left + V: Shape2.Top = Shape2.Top - V
If Shape2.Left >= frmZQ.ScaleWidth - Shape2.Width Then D = 2
If Shape2.Top <= 0 Then D = -4
Case Else
End Select
End Sub
如果需要源代码,可以发给你。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。