欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實(shí)現(xiàn)的彈球小游戲示例

 更新時(shí)間:2017年08月01日 11:13:37   作者:peerlessbloom  
這篇文章主要介紹了Python實(shí)現(xiàn)的彈球小游戲,可實(shí)現(xiàn)類似乒乓球游戲的鍵盤控制底部擋板移動(dòng)碰撞小球的游戲功能,需要的朋友可以參考下

本文實(shí)例講述了Python實(shí)現(xiàn)的彈球小游戲。分享給大家供大家參考,具體如下:

彈球

1. Ball 類

draw負(fù)責(zé)移動(dòng)Ball
碰撞檢測(cè),反彈,Ball檢測(cè)Paddle

2.Paddle類

draw負(fù)責(zé)移動(dòng)Paddle
碰撞檢測(cè),確定能不能繼續(xù)
監(jiān)聽鍵盤事件

3.主循環(huán)

繪制Ball和Paddle
update
sleep

代碼

from Tkinter import *
import random
import time
class Ball:
  def __init__(self, canvas, paddle, color):
    self.canvas = canvas
    self.paddle = paddle
    self.id = canvas.create_oval(10, 10, 25, 25, fill=color)
    self.canvas.move(self.id, 245, 100)
    startx = [-3, -2, -1, 1, 2, 3]
    random.shuffle(startx)
    self.x = startx[0]
    self.y = -3
    self.canvas_height = self.canvas.winfo_height()
    self.canvas_width = self.canvas.winfo_width()
    self.hit_bottom = False
  def draw(self):
    self.canvas.move(self.id, self.x, self.y)
    pos = self.canvas.coords(self.id)#top-left bottom-right
    if (pos[1] <= 0 or self.hit_paddle(pos) == True):
      self.y = -self.y
    if (pos[0] <= 0 or pos[2] >= self.canvas_width):
      self.x = -self.x
    if (pos[3] >= self.canvas_height):
      self.hit_bottom = True
  def hit_paddle(self, pos):
    paddle_pos = self.canvas.coords(self.paddle.id)
    if (pos[2] >= paddle_pos[0] and pos[0] <= paddle_pos[2]):
      if (pos[3] >= paddle_pos[1] and pos[3] <= paddle_pos[3]):
        return True
    return False
class Paddle:
  def __init__(self, canvas, color):
    self.canvas = canvas
    self.id = canvas.create_rectangle(0, 0, 100, 10, fill = color)
    self.x = 0
    self.canvas.move(self.id, 200, 300)
    self.canvas_width = self.canvas.winfo_width()
    self.canvas.bind_all("<Key-Left>", self.turn_left)
    self.canvas.bind_all("<Key-Right>", self.turn_right)
  def draw(self):
    pos = self.canvas.coords(self.id)
    if (pos[0] + self.x >= 0 and pos[2] + self.x <= self.canvas_width):
      self.canvas.move(self.id, self.x, 0)
    #self.x = 0
  def turn_left(self, event):
    self.x = -4
  def turn_right(self, event):
    self.x = 4
tk = Tk()
tk.title("Game")
tk.resizable(0, 0)#not resizable
tk.wm_attributes("-topmost", 1)#at top
canvas = Canvas(tk, width = 500, height = 500, bd = 0, highlightthickness = 0)
canvas.pack()
tk.update()#init
paddle = Paddle(canvas, 'blue')
ball = Ball(canvas, paddle, 'red')
while 1:
  if (ball.hit_bottom == False):
    ball.draw()
    paddle.draw()
  tk.update_idletasks()
  tk.update()
  time.sleep(0.01)

運(yùn)行效果如下圖:

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python編碼操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程

希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • Django單元測(cè)試中Fixtures的使用方法

    Django單元測(cè)試中Fixtures的使用方法

    這篇文章主要介紹了Django單元測(cè)試中Fixtures用法詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • 基于Python實(shí)現(xiàn)語(yǔ)音識(shí)別和語(yǔ)音轉(zhuǎn)文字

    基于Python實(shí)現(xiàn)語(yǔ)音識(shí)別和語(yǔ)音轉(zhuǎn)文字

    這篇文章主要為大家詳細(xì)介紹了如何利用Python實(shí)現(xiàn)語(yǔ)音識(shí)別和語(yǔ)音轉(zhuǎn)文字功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2022-09-09
  • python 解決print數(shù)組/矩陣無(wú)法完整輸出的問題

    python 解決print數(shù)組/矩陣無(wú)法完整輸出的問題

    這篇文章主要介紹了關(guān)于python 解決print數(shù)組/矩陣無(wú)法完整輸出的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • 如何在pycharm中安裝第三方包

    如何在pycharm中安裝第三方包

    這篇文章主要介紹了如何在pycharm中安裝第三方包,本文分步驟通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-10-10
  • Python有序容器的 sort 方法詳解

    Python有序容器的 sort 方法詳解

    這篇文章主要介紹了Python有序容器的 sort 方法,容器.sort(key=選擇排序依據(jù)的函數(shù), reverse=True|False) 可以將有序容器進(jìn)行排序,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-09-09
  • PyQt5+python3+pycharm開發(fā)環(huán)境配置教程

    PyQt5+python3+pycharm開發(fā)環(huán)境配置教程

    這篇文章主要介紹了PyQt5+python3+pycharm開發(fā)環(huán)境配置教程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-03-03
  • python 如何利用argparse解析命令行參數(shù)

    python 如何利用argparse解析命令行參數(shù)

    這篇文章主要介紹了python 利用argparse解析命令行參數(shù)的步驟,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下
    2020-09-09
  • java直接調(diào)用python腳本的例子

    java直接調(diào)用python腳本的例子

    有時(shí)需求使用JAVA直接調(diào)用python腳本,執(zhí)行一些服務(wù)器監(jiān)控的事情。 本文給出一個(gè)java直接調(diào)用python腳本的例子
    2014-02-02
  • Pandas讀取MySQL數(shù)據(jù)到DataFrame的方法

    Pandas讀取MySQL數(shù)據(jù)到DataFrame的方法

    今天小編就為大家分享一篇Pandas讀取MySQL數(shù)據(jù)到DataFrame的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • python中利用numpy.array()實(shí)現(xiàn)倆個(gè)數(shù)值列表的對(duì)應(yīng)相加方法

    python中利用numpy.array()實(shí)現(xiàn)倆個(gè)數(shù)值列表的對(duì)應(yīng)相加方法

    今天小編就為大家分享一篇python中利用numpy.array()實(shí)現(xiàn)倆個(gè)數(shù)值列表的對(duì)應(yīng)相加方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-08-08

最新評(píng)論