当两个形状在Tkinter中碰撞时添加碰撞检测

时间:2017-11-22 07:43:35

标签: python windows python-3.x tkinter

我试图写一条线来添加两个形状之间的碰撞;如果他们要碰撞,那将是游戏结束。我试图不使用Pygames。这是我的代码:

from tkinter import *
from time import sleep
import random

def myKeyEvent(event):
    global pdx
    global pdy
    if (event.keysym == 'Up'):
        pdx = 0
        pdy = -speed
    if (event.keysym == 'Down'):
        pdx = 0
        pdy = speed
    if (event.keysym == 'Right'):
        pdx = speed
        pdy = 0        
    if (event.keysym == 'Left'):
        pdx = -speed
        pdy = 0        
    if (event.keysym == 'S' or event.keysym == 's'):
        startGame()


def startGame():
    global gameMode
    gameMode = 1
    cvs.itemconfigure(textStart, state="hidden")


def playerCollideBorder(x, y):
    collide = False
    if (x <= 0) or (y <= 0) or (x >= sWidth-20) or (y >= sHeight-20):
        collide = True
    return collide

#Player/monster collision
def playerCollideMonster():
    collide = False
    if player == monster:
            collide = True
    return collide

def redraw():
    global gameMode
    if gameMode == 0:
        cvs.itemconfigure(textStart, state="normal")
    if gameMode == 1:
        moveMonster()
        handleMonsterBorderCollision()
        cvs.move(player, pdx, pdy)
        if playerCollideBorder(cvs.coords(player)[0], cvs.coords(player)[1]):
            gameMode = 2
    if gameMode == 2:
        cvs.itemconfigure(textOver, state="normal")    


def handleMonsterBorderCollision():
    global mdx, mdy
    x = cvs.coords(monster)[0]
    y = cvs.coords(monster)[1]
    if (x <= 0) or (y <= 0) or (x >= sWidth-20) or (y >= sHeight-20):
        mdx = -mdx
        mdy = -mdy
    cvs.move(monster, mdx, mdy)


def moveMonster():
    global mdx, mdy
    num = random.randint(1,6)
    x = cvs.coords(monster)[0]
    y = cvs.coords(monster)[1]
    xp = cvs.coords(player)[0]
    yp = cvs.coords(player)[1]    
    # go up
    if (num == 1):
        mdx = 0
        mdy = -mspeed
    # go right    
    elif (num == 2):
        mdx = mspeed
        mdy = 0
    # go down
    elif (num == 3):
        mdx = 0
        mdy = mspeed
    # go left
    elif (num == 4):
        mdx = -mspeed
        mdy = 0
    # hunting mode x
    elif (num == 5):
        if (x < xp):
            mdx = mspeed
            mdy = 0
        else:
            mdx = -mspeed
            mdy = 0            
    # hunting mode y
    elif (num == 6):
        if (y < yp):
            mdx = 0
            mdy = mspeed
        else:
            mdx = 0
            mdy = -mspeed              
    cvs.move(monster, mdx, mdy)    


tkw = Tk()
tkw.title("My Game Program v1.2")

sWidth = 600
sHeight = 600
sBar = 50
px = sWidth/2
py = sHeight - 50
pdx = 0
pdy = 0
mx = sWidth/2
my = 50
mdx = 0
mdy = 0
speed = 5
mspeed = speed * 2
gameMode = 0

cvs = Canvas(tkw, width=sWidth, height=sHeight+sBar)
cvs.create_rectangle(0,0,sWidth,sHeight,fill="black")
cvs.create_rectangle(0,sHeight,sWidth,sHeight+sBar,fill="darkgreen")
player = cvs.create_rectangle(px,py,px+20,py+20,fill="cyan")
monster = cvs.create_rectangle(mx,my,mx+20,my+20,fill="red")
textStart = cvs.create_text(300,300,fill="lightgreen",font="Times 20 bold",
                            text="Press 'S' to start the game..", state="hidden")
textOver = cvs.create_text(300,300,fill="lightgreen",font="Times 20 bold",
                            text="G A M E   O V E R", state="hidden")

cvs.bind_all('<Key>', myKeyEvent)
cvs.pack()

while True:
    redraw()
    sleep(0.01)
    tkw.update()

cvs.mainloop

我尝试通过复制其他tkinter游戏正在进行的操作来为代码添加一个函数,但我不知道它不起作用。我做错了什么?

#Player/monster collision
    def playerCollideMonster():
        collide = False
        if player == monster:
                collide = True
        return collide

0 个答案:

没有答案