Học Python qua Game Python

LẬP TRÌNH PYTHON

 

from tkinter import *
from time import sleep
from PIL import ImageTk,Image
img=[0,0,0]
tk=Tk()
tk.title("Game của tui")
tk.resizable(0,0)
can=Canvas(tk,width=600,height=300,background="white")
can.pack()
img[0]=ImageTk.PhotoImage(Image.open("photo/dragon-0.jpg"))
img[1]=ImageTk.PhotoImage(Image.open("photo/may.jpg"))
img[2]=ImageTk.PhotoImage(Image.open("photo/tree.jpg"))

dragon=can.create_image(0,240,anchor=NW,image=img[0])
may=can.create_image(550,50,anchor=NW,image=img[1])
tree=can.create_image(550,250,anchor=NW,image=img[2])
can.update()
#Di chuyển đám mây
def move_may():
    global may
    can.move(may,-5,0)
    if can.coords(may)[0]<-20:
        can.delete(may)
        may=can.create_image(550,50,anchor=NW,image=img[1])
    can.update()
score=0
text_score=can.create_text(530,50,text="SCORE:"+str(score),fill="red",font=("Times",20))
#Di chuyển cây
def move_tree():
    global tree,score,text_score
    can.move(tree,-3,0)
    if can.coords(tree)[0]<-20:
        score=score+1
        can.itemconfig(text_score,text="SCORE:"+str(score))
        can.delete(tree)
        tree=can.create_image(550,250,anchor=NW,image=img[2])
    can.update()
#Di chuyển Khủng long
check_KL_nhay=False
def move_dragon():
    global check_KL_nhay
    if  check_KL_nhay==False:
        check_KL_nhay=True
        for i in range(0,30):
            can.move(dragon,0,-5)
            move_may()
            move_tree()
            can.update()
            sleep(0.01)
        for i in range(0,30):
            can.move(dragon,0,5)
            move_may()
            move_tree()
            can.update()
            sleep(0.01)
        check_KL_nhay=False
#Nhận phiếu nhảy Khủng Long
def keypress(event):
    if event.keysym=="space":
        move_dragon()
can.bind_all("<KeyPress>",keypress)

game_over=False

def check_gameOver():
    global game_over
    toa_do_tree=can.coords(tree)
    toa_do_kh_long=can.coords(dragon)
    if toa_do_kh_long[1]>200 and toa_do_tree[0]<50:
        text_gameOver = can.create_text(300, 100, text="GAME OVER", fill="red", font=("Times", 20))
        game_over=True
    tk.after(100,check_gameOver)
check_gameOver()

while not game_over:
    move_may()
    move_tree()
    sleep(0.01)
print("GAME OVER")
tk.mainloop()

Các bài viết liên quan:

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *