微信小程序实验二 ——— 弹球游戏

实验内容:

小球按照随机的角度直线运动,如果碰到四壁则反弹。你们不需要做游戏计时、设置小球及背景颜色等,只完成小球在方框内反弹运动的功能。这里主要考查绘图知识,数学计算能力,以及对定时器的应用。

实验效果(最简单版本):

实验代码:

 index.js

// index.js
Page({
    data:{
      cx:200,
      cy:50,
      destinaX:3,
      destinaY:2
    },
    //初次渲染触发
    onReady:function(){
      var c=wx.createCanvasContext("canvasTest");
      // var c=wx.createSelectorQuery('canvasTest')
      var that=this;
      var timer=setInterval(canvasSize,20);
      function canvasSize(){
        //画布数据
        const width=300;
        const height=300;
        const speed=2;
        const r=7;
        //下面是相对左上角基点距离 和画布大小
        c.rect(0,0,width,height);
        c.stroke();
        //球会根据实时位置发生变化
        c.beginPath();
        var ox1=that.datacox;
        var oy1=that.datacoy;
          ox1=that.data.destinaX*speed+that.data.cx;
          oy1=that.data.cy-that.data.destinaY*speed;
          if(ox1>=width||ox1<=0){
            var bounceX=-that.data.destinaX;
            that.setData({destinaX:bounceX});
            ox1=that.data.destinaX*speed+that.data.cx;
          }
          if(oy1>=height||oy1<=0){
            var bounceY=-that.data.destinaY;
            that.setData({destinaY:bounceY});
            oy1=that.data.cy-that.data.destinaY*speed;
          }

     console.log(that.data.ox1,that.data.oy1);
        that.setData({cx:ox1,cy:oy1});
        c.arc(that.data.cx,that.data.cy,r,0,2*Math.PI);
        c.fill();
        c.stroke();
        c.draw();
      }
    }
  })
  

 index.wxml

<!--index.wxml-->
<view class="canvasStyle">
<canvas canvas-id="canvasTest" style="width: 300px;height: 300px;"></canvas> 
</view>

index.wxss

/**index.wxss**/
.canvasStyle{
  display: flex;
  justify-content: center;
  margin: 10px;
  margin-top: 70px;
  background-color: aqua;
  }
  

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是一个简单的Python弹球游戏的例子,代码如下所示: ```python import turtle import time import random # 设置窗口 win = turtle.Screen() win.title("弹球游戏") win.bgcolor("black") win.setup(width=600, height=600) # 创建球拍 A paddle_a = turtle.Turtle() paddle_a.speed(0) paddle_a.shape("square") paddle_a.color("white") paddle_a.shapesize(stretch_wid=1, stretch_len=5) paddle_a.penup() paddle_a.goto(0, -250) # 创建球 ball = turtle.Turtle() ball.speed(40) ball.shape("circle") ball.color("white") ball.penup() ball.goto(0, 0) ball.dx = 3 ball.dy = -3 # 移动球拍 A def move_left(): x = paddle_a.xcor() x -= 20 paddle_a.setx(x) def move_right(): x = paddle_a.xcor() x += 20 paddle_a.setx(x) # 键盘绑定 win.listen() win.onkeypress(move_left, "Left") win.onkeypress(move_right, "Right") # 循环运行游戏 while True: win.update() # 移动球 ball.setx(ball.xcor() + ball.dx) ball.sety(ball.ycor() + ball.dy) # 碰到左右边界 if ball.xcor() > 290: ball.setx(290) ball.dx *= -1 elif ball.xcor() < -290: ball.setx(-290) ball.dx *= -1 # 碰到上下边界 if ball.ycor() > 290: ball.sety(290) ball.dy *= -1 elif ball.ycor() < -290: ball.goto(0, 0) ball.dy *= -1 time.sleep(1) # 碰到球拍 if (ball.ycor() < -240 and ball.ycor() > -250) and (ball.xcor() < paddle_a.xcor() + 50 and ball.xcor() > paddle_a.xcor() - 50): ball.dy *= -1 # 游戏结束 if ball.ycor() < -290: break # 显示游戏结束 game_over = turtle.Turtle() game_over.speed(0) game_over.color("white") game_over.penup() game_over.hideturtle() game_over.goto(0, 0) game_over.write("游戏结束", align="center", font=("Courier", 24, "normal")) # 延迟关闭窗口 time.sleep(3) win.bye() ``` 这个小游戏中,玩家需要控制球拍 A 来接住弹出的球,如果球落地则游戏结束。玩家可以通过键盘左右箭头来移动球拍 A,当球碰到球拍 A 时,球会反弹。这个小游戏可以帮助玩家更好地理解 Python 类的概念,并且也是一个有趣的小游戏

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值