您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)python3如何實(shí)現(xiàn)彈彈球小游戲的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。
1、云計(jì)算,典型應(yīng)用OpenStack。2、WEB前端開發(fā),眾多大型網(wǎng)站均為Python開發(fā)。3.人工智能應(yīng)用,基于大數(shù)據(jù)分析和深度學(xué)習(xí)而發(fā)展出來的人工智能本質(zhì)上已經(jīng)無法離開python。4、系統(tǒng)運(yùn)維工程項(xiàng)目,自動(dòng)化運(yùn)維的標(biāo)配就是python+Django/flask。5、金融理財(cái)分析,量化交易,金融分析。6、大數(shù)據(jù)分析。
python3實(shí)現(xiàn)彈彈球小游戲的具體內(nèi)容如下
from tkinter import * from tkinter import messagebox import random import time from PIL import Image, ImageTk import sys class Game: def __init__(self): self.tk = Tk() self.times = 0 sw = self.tk.winfo_screenwidth() sh = self.tk.winfo_screenheight() image = Image.open(r'02.jpg') background_image = ImageTk.PhotoImage(image) ww = background_image.width() wh = background_image.height() x = (sw-ww)/2 y = (sh-wh)/2 self.tk.geometry("%dx%d+%d+%d" % (ww, wh, x, y)) self.tk.title('歡迎進(jìn)入彈彈彈') background_label = Label(self.tk, image=background_image) background_label.place(x=0, y=0, relwidth=1, relheight=1) self.tk.resizable(False, False) self.tk.wm_attributes("-topmost", 1) # at top btn1 = Button(self.tk, text='減少難度', background='#FFFF67', activebackground='#3EC23B', command=self.test1) btn2 = Button(self.tk, text='增加難度', background='#FFFF67', activebackground='#3EC23B', command=self.test2) btn1.place(x=380, y=10, width=80, height=30) btn2.place(x=500, y=10, width=80, height=30) self.canvas = Canvas(self.tk, width=500, height=400, bd=0, highlightthickness=0, background='#FFFFFF') self.canvas.place(x=190, y=90, width=500, height=400) self.tk.update() self.xunhuan() def xunhuan(self): try: paddle = Paddle(self.canvas, 'green', 100) ball = Ball(self.canvas, paddle, '#D11C43', 0) while True: if ball.hit_bottom == False: ball.draw() paddle.draw() else: b = messagebox.askyesno( '失敗', message="您的分?jǐn)?shù)為:" + str(ball.score)+"\n是否重新開始游戲?") if bool(b) is True: paddle.canvas.delete(paddle.id) self.sever() else: sys.exit() self.tk.update_idletasks() self.tk.update() time.sleep(0.01+self.times) except: sys.exit('游戲已退出!') def sever(self): self.xunhuan() def test1(self): self.times += 0.001 def test2(self): self.times -= 0.001 class Ball: def __init__(self, canvas, paddle, color, score): self.score = 0 self.canvas = canvas self.paddle = paddle self.id = canvas.create_oval( 20, 20, 35, 35, fill=color, outline='green') 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 b = messagebox.askyesno('game', '游戲是否開始?') if bool(b) is True: self.draw() else: sys.exit() def draw(self): self.canvas.move(self.id, self.x, self.y) pos = self.canvas.coords(self.id) 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]: self.score += 1 return True return False class Paddle: def __init__(self, canvas, color, width): self.canvas = canvas self.id = canvas.create_rectangle(0, 0, width, 10, fill=color) self.x = 0 self.y = 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) def turn_left(self, event): self.x = -4 def turn_right(self, event): self.x = 4 def main(): game = Game() if __name__ == '__main__': main()
感謝各位的閱讀!關(guān)于“python3如何實(shí)現(xiàn)彈彈球小游戲”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。