- 20 Tháng bảy 2019
- 375
- 84
- 51
- 20
- Quảng Nam
- THCS Nguyễn Bỉnh Khiêm


Mình có tạo chương trình theo hướng dẫn trên sách, sách có đưa trên mạng bản mẫu, mình tải xuống rồi kiểm tra. Cả 2 bản đều chạy đuộc. Tuy nhiên, khi đóng(đóng khi đang chạy) chương trình thì bản của mình nó hiện lỗi mà mình sửa không được. Giúp với. Cảm ơn.
Mình dùng python nên bạn nào giỏi tin muốn giúp thì biết nha.
Bản của mình:
import tkinter
import time
from tkinter import messagebox
y_height=300
x_width=400
bounceCount=0
window=tkinter.Tk()
canvas=tkinter.Canvas(window, width=x_width, height=y_height, bg="navy")
canvas.pack()
bat=canvas.create_rectangle(0, 0, 50, 20, fill="deepskyblue")
ball=canvas.create_oval(0, 0, 10, 10, fill="fuchsia")
windowClosed=False
def play_game():
while windowClosed==False:
control_bat()
bounce_ball()
window.update()
time.sleep(0.02)
if windowClosed==False:
check_game()
def key_press(event):
global leftPressed, rightPressed
if event.keysym=="Right":
rightPressed=1
elif event.keysym=="Left":
leftPressed=1
def key_release(event):
global leftPressed, rightPressed
if event.keysym=="Right":
rightPressed=0
elif event.keysym=="Left":
leftPressed=0
#tạo_nút_chọn_độ_khó_cho_game:
#cái_vợt:
batSpeed=6
def control_bat():
batMovement=(batSpeed*rightPressed)-(batSpeed*leftPressed)
(batLeft,batTop,batRight,batBottom)=canvas.coords(bat)
if (batLeft>=0 or batMovement>0) and (batRight<=x_width or batMovement<0):
canvas.move(bat,batMovement,0)
#bóng:
boundary_Top=y_height-40
boundary_Bottom=y_height-30
ballMoveX=4
ballMoveY=-4
window.protocol("<WM_DELETE_WINDOW>",quit)
def bounce_ball():
global ballMoveX, ballMoveY
(ballLeft,ballTop,ballRight,ballBottom)=canvas.coords(ball)
if ballMoveX>0 and ballRight>x_width:
ballMoveX=-ballMoveX
if ballMoveX<0 and ballRight<0:
ballMoveX=-ballMoveX
if ballMoveY<0 and ballTop<0:
ballMoveY=-ballMoveY
if ballMoveY>0 and ballBottom<boundary_Bottom and ballBottom>boundary_Top:
(batLeft, batTop, batRight, batBottom)=canvas.coords(bat)
(ballLeft, ballTop, ballRight, ballBottom)=canvas.coords(ball)
if ballRight>batLeft and ballLeft<batRight:
ballMoveY=-ballMoveY
canvas.move(ball,ballMoveX,ballMoveY)
window.protocol("<WM_DELETE_WINDOW>",quit)
def check_game():
(ballLeft, ballTop, ballRight, ballBottom)=canvas.coords(ball)
if ballTop>y_height:
again=tkinter.messagebox.askyesno(message="Chơi lại?")
if again==True:
reset()
else:
quit()
def quit():
global windowClosed
windowClosed=True
window.destroy()
def reset():
global leftPressed, rightPressed, ballMoveX, ballMoveY
leftPressed=0
rightPressed=0
ballMoveX=4
ballMoveY=-4
canvas.coords(bat,10, boundary_Top, 50, boundary_Bottom)
canvas.coords(ball, 20, boundary_Top, 30, boundary_Bottom)
batSpeed=6
window.protocol("<WM_DELETE_WINDOW>",quit)
window.bind("<KeyPress>", key_press)
window.bind("<KeyRelease>", key_release)
reset()
play_game()
Bản mẫu:
#This is the first version of the bat and ball code, up to the end of page 80 (that is, step 19).
#page 72, steps 1 and 2
import tkinter
#The line below is a bug fix for versions of python that are newer than 3.5.2
from tkinter import messagebox
import time
x_width = 750
y_height = 500
window = tkinter.Tk()
canvas = tkinter.Canvas(window, width=x_width, height=y_height, bg="dodgerblue4")
canvas.pack()
#p73, step 3
bat = canvas.create_rectangle(0, 0, 40, 10, fill="dark turquoise")
ball = canvas.create_oval(0, 0, 10, 10, fill="deep pink")
#step 4
windowClosed = True
def main_loop():
while windowClosed == True:
control_bat()
move_ball()
window.update()
time.sleep(0.02)
if windowClosed == True:
check_game()
#p74, step 5
leftPressed = 0
rightPressed = 0
def on_key_press(event):
global leftPressed, rightPressed
if event.keysym == "Left":
leftPressed = 1
elif event.keysym == "Right":
rightPressed = 1
#step 6
def on_key_release(event):
global leftPressed, rightPressed
if event.keysym == "Left":
leftPressed = 0
elif event.keysym == "Right":
rightPressed = 0
#step 7
batSpeed = 6
def control_bat():
batMove = batSpeed*rightPressed - batSpeed*leftPressed
#p75, step 8
(batLeft, batTop, batRight, batBottom) = canvas.coords(bat)
#p76, step 9
if (batLeft > 0 or batMove > 0) and (batRight < x_width or batMove < 0):
canvas.move(bat, batMove, 0)
#p77, step 10
ballMoveX = 4
ballMoveY = -4
setBatTop = y_height-40
setBatBottom = y_height-30
#step 11
def move_ball():
global ballMoveX, ballMoveY
(ballLeft, ballTop, ballRight, ballBottom) = canvas.coords(ball)
#step 12
if ballMoveX > 0 and ballRight > x_width:
ballMoveX = -ballMoveX
if ballMoveX < 0 and ballLeft < 0:
ballMoveX = -ballMoveX
if ballMoveY < 0 and ballTop < 0:
ballMoveY = -ballMoveY
#p78, step 13
if ballMoveY > 0 and ballBottom > setBatTop and ballBottom < setBatBottom:
(batLeft, batTop, batRight, batBottom) = canvas.coords(bat)
if ballRight > batLeft and ballLeft < batRight:
ballMoveY = -ballMoveY
#step 14
canvas.move(ball, ballMoveX, ballMoveY)
#step 15
def check_game():
(ballLeft, ballTop, ballRight, ballBottom) = canvas.coords(ball)
if ballTop > y_height:
again = tkinter.messagebox.askyesno(message="Do you want to play again?")
if again == True:
reset()
else:
close()
#step 16
def close():
global windowClosed
windowClosed = False
window.destroy()
#step 17
def reset():
global leftPressed, rightPressed
global ballMoveX, ballMoveY
leftPressed = 0
rightPressed = 0
ballMoveX = 4
ballMoveY = -4
canvas.coords(bat, 10, setBatTop, 50, setBatBottom)
canvas.coords(ball, 20, setBatTop-10, 30, setBatTop)
#step 18
window.protocol("WM_DELETE_WINDOW", close)
window.bind("<KeyPress>", on_key_press)
window.bind("<KeyRelease>", on_key_release)
#step 19
reset()
main_loop()
Mình dùng python nên bạn nào giỏi tin muốn giúp thì biết nha.
Bản của mình:
import tkinter
import time
from tkinter import messagebox
y_height=300
x_width=400
bounceCount=0
window=tkinter.Tk()
canvas=tkinter.Canvas(window, width=x_width, height=y_height, bg="navy")
canvas.pack()
bat=canvas.create_rectangle(0, 0, 50, 20, fill="deepskyblue")
ball=canvas.create_oval(0, 0, 10, 10, fill="fuchsia")
windowClosed=False
def play_game():
while windowClosed==False:
control_bat()
bounce_ball()
window.update()
time.sleep(0.02)
if windowClosed==False:
check_game()
def key_press(event):
global leftPressed, rightPressed
if event.keysym=="Right":
rightPressed=1
elif event.keysym=="Left":
leftPressed=1
def key_release(event):
global leftPressed, rightPressed
if event.keysym=="Right":
rightPressed=0
elif event.keysym=="Left":
leftPressed=0
#tạo_nút_chọn_độ_khó_cho_game:
#cái_vợt:
batSpeed=6
def control_bat():
batMovement=(batSpeed*rightPressed)-(batSpeed*leftPressed)
(batLeft,batTop,batRight,batBottom)=canvas.coords(bat)
if (batLeft>=0 or batMovement>0) and (batRight<=x_width or batMovement<0):
canvas.move(bat,batMovement,0)
#bóng:
boundary_Top=y_height-40
boundary_Bottom=y_height-30
ballMoveX=4
ballMoveY=-4
window.protocol("<WM_DELETE_WINDOW>",quit)
def bounce_ball():
global ballMoveX, ballMoveY
(ballLeft,ballTop,ballRight,ballBottom)=canvas.coords(ball)
if ballMoveX>0 and ballRight>x_width:
ballMoveX=-ballMoveX
if ballMoveX<0 and ballRight<0:
ballMoveX=-ballMoveX
if ballMoveY<0 and ballTop<0:
ballMoveY=-ballMoveY
if ballMoveY>0 and ballBottom<boundary_Bottom and ballBottom>boundary_Top:
(batLeft, batTop, batRight, batBottom)=canvas.coords(bat)
(ballLeft, ballTop, ballRight, ballBottom)=canvas.coords(ball)
if ballRight>batLeft and ballLeft<batRight:
ballMoveY=-ballMoveY
canvas.move(ball,ballMoveX,ballMoveY)
window.protocol("<WM_DELETE_WINDOW>",quit)
def check_game():
(ballLeft, ballTop, ballRight, ballBottom)=canvas.coords(ball)
if ballTop>y_height:
again=tkinter.messagebox.askyesno(message="Chơi lại?")
if again==True:
reset()
else:
quit()
def quit():
global windowClosed
windowClosed=True
window.destroy()
def reset():
global leftPressed, rightPressed, ballMoveX, ballMoveY
leftPressed=0
rightPressed=0
ballMoveX=4
ballMoveY=-4
canvas.coords(bat,10, boundary_Top, 50, boundary_Bottom)
canvas.coords(ball, 20, boundary_Top, 30, boundary_Bottom)
batSpeed=6
window.protocol("<WM_DELETE_WINDOW>",quit)
window.bind("<KeyPress>", key_press)
window.bind("<KeyRelease>", key_release)
reset()
play_game()
Bản mẫu:
#This is the first version of the bat and ball code, up to the end of page 80 (that is, step 19).
#page 72, steps 1 and 2
import tkinter
#The line below is a bug fix for versions of python that are newer than 3.5.2
from tkinter import messagebox
import time
x_width = 750
y_height = 500
window = tkinter.Tk()
canvas = tkinter.Canvas(window, width=x_width, height=y_height, bg="dodgerblue4")
canvas.pack()
#p73, step 3
bat = canvas.create_rectangle(0, 0, 40, 10, fill="dark turquoise")
ball = canvas.create_oval(0, 0, 10, 10, fill="deep pink")
#step 4
windowClosed = True
def main_loop():
while windowClosed == True:
control_bat()
move_ball()
window.update()
time.sleep(0.02)
if windowClosed == True:
check_game()
#p74, step 5
leftPressed = 0
rightPressed = 0
def on_key_press(event):
global leftPressed, rightPressed
if event.keysym == "Left":
leftPressed = 1
elif event.keysym == "Right":
rightPressed = 1
#step 6
def on_key_release(event):
global leftPressed, rightPressed
if event.keysym == "Left":
leftPressed = 0
elif event.keysym == "Right":
rightPressed = 0
#step 7
batSpeed = 6
def control_bat():
batMove = batSpeed*rightPressed - batSpeed*leftPressed
#p75, step 8
(batLeft, batTop, batRight, batBottom) = canvas.coords(bat)
#p76, step 9
if (batLeft > 0 or batMove > 0) and (batRight < x_width or batMove < 0):
canvas.move(bat, batMove, 0)
#p77, step 10
ballMoveX = 4
ballMoveY = -4
setBatTop = y_height-40
setBatBottom = y_height-30
#step 11
def move_ball():
global ballMoveX, ballMoveY
(ballLeft, ballTop, ballRight, ballBottom) = canvas.coords(ball)
#step 12
if ballMoveX > 0 and ballRight > x_width:
ballMoveX = -ballMoveX
if ballMoveX < 0 and ballLeft < 0:
ballMoveX = -ballMoveX
if ballMoveY < 0 and ballTop < 0:
ballMoveY = -ballMoveY
#p78, step 13
if ballMoveY > 0 and ballBottom > setBatTop and ballBottom < setBatBottom:
(batLeft, batTop, batRight, batBottom) = canvas.coords(bat)
if ballRight > batLeft and ballLeft < batRight:
ballMoveY = -ballMoveY
#step 14
canvas.move(ball, ballMoveX, ballMoveY)
#step 15
def check_game():
(ballLeft, ballTop, ballRight, ballBottom) = canvas.coords(ball)
if ballTop > y_height:
again = tkinter.messagebox.askyesno(message="Do you want to play again?")
if again == True:
reset()
else:
close()
#step 16
def close():
global windowClosed
windowClosed = False
window.destroy()
#step 17
def reset():
global leftPressed, rightPressed
global ballMoveX, ballMoveY
leftPressed = 0
rightPressed = 0
ballMoveX = 4
ballMoveY = -4
canvas.coords(bat, 10, setBatTop, 50, setBatBottom)
canvas.coords(ball, 20, setBatTop-10, 30, setBatTop)
#step 18
window.protocol("WM_DELETE_WINDOW", close)
window.bind("<KeyPress>", on_key_press)
window.bind("<KeyRelease>", on_key_release)
#step 19
reset()
main_loop()