使用Python的tkinter庫建立遊戲專案

2021-09-27 07:16:26 字數 3802 閱讀 6712

因為家教alex的原因,接觸到了tkinter,於是把過程記錄在此

第一階段,建立了兩個正方形,遊戲規則是,雙人遊戲控制正方形,只要碰撞到對方,遊戲結束。

第一階段**

import time

import random

from tkinter import

*from tkinter import colorchooser

def hello()

:print

('hello there'

)classp:

def __init__

(self, x1, y1, x2, y2, canvas, color)

: self.canvas = canvas

self.id = canvas.

create_rectangle

(x1, y1, x2, y2, fill=color)

# self.canva.

move

(self.id,

200,

300)

self.x =

0 self.y =

0 self.canvas_width = self.canvas.

winfo_width()

self.canvas_height = self.canvas.

winfo_height()

canvas.

bind_all(''

, self.turn_up)

canvas.

bind_all(''

, self.turn_down)

canvas.

bind_all(''

, self.turn_left)

canvas.

bind_all(''

, self.turn_right)

def draw

(self)

: self.canvas.

move

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

pos = self.canvas.

coords

(self.id)

if pos[0]

<=0:

self.x =

0if pos[1]

<=0:

self.y =

0if pos[2]

>= self.canvas_width:

self.x =

0if pos[3]

>= self.canvas_height:

self.y =

0 def turn_left

(self, evt)

: self.x =-5

self.y =

0 def turn_right

(self, evt)

: self.x =

5 self.y =

0 def turn_up

(self, evt)

: self.x =

0 self.y =-5

def turn_down

(self, evt)

: self.x =

0 self.y =

5# 建立基本視窗

tk =tk(

)tk.

title

("game"

)tk.

resizable(0

,0)tk.

wm_attributes

("-topmost",1

)# 建立畫布

canvas =

canvas

(tk, width=

500, height=

500, bd=

0, highlightthickness=0)

canvas.

pack()

# update tk

(more beautiful)

tk.update()

# canvas.

create_line(0

,0,500

,500

)# 設定第乙個正方形

color = colorchooser.

askcolor()

p1 =p(

10,10,

50,50, canvas, color[1]

)# 設定第二個正方形

# color = colorchooser.

askcolor()

# p2 =p(

450,

450,

490,

490, canvas, color[1]

)# 顯示文字

canvas.

create_text

(250

,400

, text=

'game over'

, font=

('courier',30

))# 建立按鈕

btn =

button

(tk, text=

"click me"

, command=hello)

btn.

pack()

while1:

p1.draw()

# p2.

draw()

tk.update_idletasks()

tk.update()

time.

sleep

(0.03

)tk.

mainloop

()

import time

import random

from tkinter import

*class

ball

: def __init__

(self, canvas, color)

: self.canvas = canvas

self.id = canvas.

create_oval(10

,10,25

,25, fill=color)

self.canvas.

move

(self.id,

245,

100)

def draw

(self)

: pass

# 建立基本視窗

tk =tk(

)tk.

title

("game"

)tk.

resizable(0

,0)tk.

wm_attributes

("-topmost",1

)# 建立畫布

canvas =

canvas

(tk, width=

500, height=

500, bd=

0, highlightthickness=0)

canvas.

pack()

# update tk

(more beautiful)

tk.update()

ball =

ball

(canvas,

'red'

)tk.

mainloop

()

python元類的使用 python使用元類

原文 type 動態語言和靜態語言最大的不同,就是函式和類的定義,不是編譯時定義的,而是執行時動態建立的。比方說我們要定義乙個hello的class,就寫乙個hello.py模組 當python直譯器載入hello模組時,就會依次執行該模組的所有語句,執行結果就是動態建立出乙個hello的class...

python的應用環境 python的使用環境總結

python在linux上執行,使用的是vim,每次都是敲四個空格進行縮排,真尼瑪的蛋疼,書本果然是個好東西,從而根據書本python高階程式設計中的設定配置而來 1 進行自動補全的指令碼 root python cat pythonstartup python startup file impor...

Python時間的使用

usr bin python coding utf 8 import time import datetime 系統時間戳 print time.time 1450682521.0920722 localtime返回的是當前時區的值 print time.localtime time.struct ...