求哪位高手告诉我用VB做一个反d球的程序!谢谢了!

求哪位高手告诉我用VB做一个反d球的程序!谢谢了!,第1张

我只会左右乱乱销祥反哗斗档d球。 S1是Shape

Private Sub Timer1_Timer()

Static s

If S1.Left >= Form1.Width - S1.Width And s = 2 Then s = 0

If S1.Left <= 0 And s = 0 Then s = 2

If s = 0 Then S1.Left = S1.Left - 100

If s = 2 Then S1.Left = S1.Left + 100

End Sub

用python  tkinter模块做一个击打反d球和球拍的游戏。球在屏幕上飞,玩家需要把它击打回去,只要球落到屏幕底部,游戏就结束。

首先我们做一个在屏幕上到处移动的小球

#coding:utf-8

from tkinter import *

import random

import time

class Ball:

    #创建一个球类

    def __init__(self, canvas, color):

        self.canvas = 袜蚂滑canvas

        self.id = canvas.create_oval(10, 10, 25, 25, fill=color)

        #返回刚好划小球的id,create_oval创建一个椭圆

        self.canvas.move(self.id, 245, 100)

        #把椭圆移动到画布

        starts = [-3, -2, -1, 1, 2, 3]

        random.shuffle(starts)

        #随机排列

        self.x = starts[0]

        self.y = -3

        self.canvas_height = self.canvas.winfo_height()

        #获取画布当前高度

        self.canvas_width = self.canvas.winfo_width()

        #获取画布当前宽度

    def draw(self):

        self.canvas.move(self.id, self.x, self.y)

        #让小球水平和垂直移动

        pos = self.canvas.coords(self.id)

        #coords返回画布上画好的x和y坐标

        #判断小球是否撞到画布顶部或者底部,保证小球反d回去,不消失

        告腊if pos[1] <= 0:

            self.y = 3

        if pos[3] >= self.canvas_height:

            self.y = -3

        if pos[0] 物缓<= 0:

            self.x = 3

        if pos[2] >= self.canvas_width:

            self.x = -3

tk = Tk()

tk.title("Game")

tk.resizable(0, 0)

#窗口大小不可调整

tk.wm_attributes("-topmost", 1)

#使画布窗口置于所有窗口之前

canvas = Canvas(tk,width=500, height=400, bd=0, highlightthickness=0)

#bd和highlighttthickness是为了保证画布没有边框

canvas.pack()

tk.update()

#动画初始化

ball = Ball(canvas, 'red')

while 1:

    #画布一出现会马上消失,为了防止画布消失,用tkinter一直重画

    ball.draw()

    tk.update_idletasks()

    tk.update()

    time.sleep(0.01)

一个会动的小球就做好啦

效果图如下,只是它会动<( ̄3 ̄)>!


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

原文地址:https://www.54852.com/yw/12449097.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存