简单的使用键盘控制方块移动

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>键盘控制方块移动</title>
    <style>
    #box{
        width: 50px;
        height: 50px;
        background-color: green;
        position: relative;
        left: 50px;
        top: 50px;
    }
    </style>
</head>
<body>
    <div id="box"></div>
    <script>
        // var box=document.getElementById('box');//此部分代码的效果会用小小的停顿,下面的为改进代码
        // var l=0;
        // var t=0;
        // document.οnkeydοwn=function(){
        //     var e=window.event||ev;
        //     // console.log(e.keyCode);
        //     if (e.keyCode==37) {
        //         l-=10;
        //         box.style.                               
        //         left=l+'px';
        //     }
        //     if (e.keyCode==38) {
        //         t-=10;
        //         box.style.top=t+'px';
        //     }
        //     if (e.keyCode==39) {
        //         l+=10;
        //         box.style.left=l+'px';
        //     }
        //     if (e.keyCode==40) {
        //         t+=10;
        //         box.style.top=t+'px';
        //     }
        // }
        var div=document.getElementById('box');
        var s=0,left=0,right=0,bottom=0;//用top不可以,为保留字;
        var timer=null,x=50,y=50;
        timer=setInterval(function(){
            if (left==1) {
                x-=5;
                div.style.left=x+'px';
            }
            if (right==1) {
                x+=5;
                div.style.left=x+'px';
            }
            if (s==1) {
                y-=5;
                div.style.top=y+'px';
            }
            if (bottom==1) {
                y+=5;
                div.style.top=y+'px';
            }
        },20)
        document.onkeydown=function(ev){
            var e=ev||window.event;
            switch(e.keyCode){
                case 37:
                    left=1;
                    break;
                case 38:
                    s=1;
                    break;
                case 39:
                    right=1;
                    break;
                case 40:
                    bottom=1;
                    break;
                default:
                    alert('请按方向键');
            }
        }
        document.onkeyup=function(ev){
            var e=ev||window.event;
            switch(e.keyCode){
                case 37:
                    left=0;
                    break;
                case 38:
                    s=0;
                    break;
                case 39:
                    right=0;
                    break;
                case 40:
                    bottom=0;
                    break;
                default:
                    alert('请按方向键');
            }
        }
    </script>
</body>

</html>

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个示例代码,可以通过键盘控制方块下落: ```python import pygame # 初始化 Pygame pygame.init() # 定义颜色 BLACK = (0, 0, 0) WHITE = (255, 255, 255) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255) # 设置屏幕大小 SCREEN_WIDTH = 700 SCREEN_HEIGHT = 500 screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) # 设置标题 pygame.display.set_caption("方块下落") # 定义方块大小和速度 BLOCK_SIZE = 20 BLOCK_SPEED = 5 # 定义方块初始位置和速度 block_x = SCREEN_WIDTH / 2 - BLOCK_SIZE / 2 block_y = 0 block_speed_x = 0 block_speed_y = BLOCK_SPEED # 游戏循环 done = False clock = pygame.time.Clock() while not done: # 处理事件 for event in pygame.event.get(): if event.type == pygame.QUIT: done = True elif event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: block_speed_x = -BLOCK_SPEED elif event.key == pygame.K_RIGHT: block_speed_x = BLOCK_SPEED # 移动方块 block_x += block_speed_x block_y += block_speed_y # 检测方块是否超出边界 if block_x < 0: block_x = 0 elif block_x + BLOCK_SIZE > SCREEN_WIDTH: block_x = SCREEN_WIDTH - BLOCK_SIZE # 绘制屏幕 screen.fill(WHITE) pygame.draw.rect(screen, BLUE, [block_x, block_y, BLOCK_SIZE, BLOCK_SIZE]) pygame.display.flip() # 控制帧率 clock.tick(60) # 退出 Pygame pygame.quit() ``` 这个示例代码使用 Pygame 库来创建一个窗口,并在窗口中绘制一个蓝色的方块。通过监听键盘事件来控制方块的左右移动,每次循环更新方块的位置并重新绘制屏幕。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值