键盘控制小球的移动

 键盘控制小球的移动

 

 

 

 

 1 <!DOCTYPE html>
 2 <html lang="zh-cn">
 3 <head>
 4     <meta charset="utf-8">
 5     <title>7-78 课堂演示</title>
 6     <link rel="stylesheet" type="text/css" href="style.css">
 7     <style type="text/css">
 8     div{
 9             /*background: orange;*/
10             width: 100px;
11             height: 100px;
12             padding: 30px;
13             border-radius: 200px;
14             position: absolute;
15             background-image: url(../img/sc16.png);
16         }
17     </style>
18 </head>
19 <body>
20     <div id="ball" style="top:50px;left: 50px"></div>
21     <script>
22         var key={
23             W:87,
24             S:83,
25             A:65,
26             D:68
27         }
28         function keymove(e){
29             var ball=document.getElementById('ball')
30             var top=parseInt(ball.style.top);
31             var left=parseInt(ball.style.left);
32             // alert(top+50+'px')
33             switch(e.keyCode){
34                 case key.W:
35                 ball.style.top=top-50+'px';
36                 break;
37                 case key.S:
38                 ball.style.top=top+50+'px'
39                 break;
40                 case key.A:
41                 ball.style.left=left-50+'px'
42                 break;
43                 case key.D:
44                 ball.style.left=left+50+'px'
45                 break;
46 
47             }
48         }
49         document.onkeydown=keymove
50 
51     </script>
52 </body>
53 </html>

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MATLAB_GUI控制小球加减速运动 btn.start.m function btn_start() global hball ipos delt isforward npos ispaused x y ispaused = false; while (1) set(hball, 'xdata', x(ipos), 'ydata', y(ipos)); drawnow; pause(delt); ipos = ipos-(-1)^isforward; if ipos==npos+1 ipos = 1; elseif ipos == 0 ipos = npos; end if ispaused break end end ball.m hfigure = figure('name', '控制小球运动 ver 1.0', 'menubar', 'none', 'numbertitle', 'off', 'position', [600 200 350 400], 'visible', 'off'); global hball ipos delt isforward npos ispaused x y haxes = axes('position', [0.25 0.45 0.5 0.5], 'visible', 'off'); axis equal t = 0:0.1:2*pi+0.1; x = sin(t); y = cos(t); line(x, y, 'linewidth', 2); set(0, 'defaultuicontrolbackgroundcolor', get(hfigure, 'color')); set(0, 'defaultuicontrolfontsize', 12); set(0, 'defaultuicontrolunits', 'points'); hball = line('xdata', 0, 'ydata', 1, 'marker', 'o', 'markerfacecolor', 'r', 'markersize', 15); npos = length(t); ipos = 1; delt = 0.01; ispaused = false; isforward = true; uicontrol('string', '开始', 'position', [30 50 50 20], 'callback', 'btn_start;'); uicontrol('string', '停止', 'position', [100 50 50 20], 'callback', 'ispaused = true;'); uicontrol('string', '换向', 'position', [170 50 50 20], 'callback', 'isforward = ~isforward;'); uicontrol('string', '退出', 'position', [100 20 50 20], 'callback', ['ispaused = true;', 'closereq;', ... 'clear delt hball ipos ispaused t y haxes hfigure isforward npos x;']); uicontrol('style', 'slider', 'value', 0.5, 'position', [40 90 190 15], 'callback', 'delt = get(gcbo, ''value'')/100+0.01;'); uicontrol('style', 'text', 'position', [40 110 190 20], 'fontsize', 12, 'string', 'speed'); % newicon = javax.swing.imageicon('1.png'); % figframe = get(hfigure, 'javaframe'); % figframe.setfigureicon(newicon); set(hfigure, 'visible', 'on');

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值