这是我有史以来的第一个代码,我对它一无所知,但我创造了一个蝙蝠和Ball游戏并受到这9gagger的启发https://9gag.com/gag/aYgb2mN我想创造类似的东西。如何将我的蝙蝠对象双色调成看起来像卷烟?
正如我所说的,这是我的第一个代码,大多数是通过尝试找到的,所以欢迎对我的代码进行任何改进!
import tkinter as TK
import time
VERT,HOREZ=0,1
xTOP,yTOP = 0,1
xBTM,yBTM = 2,3
MAX_WIDTH,MAX_HEIGHT = 640,480
xSTART,ySTART = 50,200
BALL_SIZE=10
RUNNING=True
def close():
global RUNNING
RUNNING=False
root.destroy()
def move_right(event):
if canv.coords(paddle)[xBTM]<(MAX_WIDTH-7):
canv.move(paddle, 20, 0)
def move_left(event):
if canv.coords(paddle)[xTOP]>7:
canv.move(paddle, -20, 0)
def determineDir(ball,obj):
global delta_x,delta_y
if (ball[xTOP] == obj[xBTM]) or (ball[xBTM] == obj[xTOP]):
delta_x = -delta_x
elif (ball[yTOP] == obj[yBTM]) or (ball[yBTM] == obj[yTOP]):
delta_y = -delta_y
root = TK.Tk()
root.title("Bouncing Ball")
root.geometry('%sx%s+%s+%s' %(MAX_WIDTH, MAX_HEIGHT, 100, 100))
root.bind('<Right>', move_right)
root.bind('<Left>', move_left)
root.protocol('WM_DELETE_WINDOW', close)
canv = TK.Canvas(root, highlightthickness=0)
canv.pack(fill='both', expand=True)
top = canv.create_line(0, 0, MAX_WIDTH, 0, fill='blue',
tags=('top'))
left = canv.create_line(0, 0, 0, MAX_HEIGHT, fill='blue',
tags=('left'))
right = canv.create_line(MAX_WIDTH, 0, MAX_WIDTH, MAX_HEIGHT,
fill='blue', tags=('right'))
bottom = canv.create_line(0, MAX_HEIGHT, MAX_WIDTH, MAX_HEIGHT,
fill='blue', tags=('bottom'))
ball = canv.create_rectangle(0, 0, BALL_SIZE, BALL_SIZE,
outline='black', fill='black', tags=('ball'))
paddle = canv.create_rectangle(100, MAX_HEIGHT - 20, 170, 470,
outline='black', fill='white', tags=('rect'))
#row is 64 boxes wide
brick=list()
#trachea
for i in range(32,34):
for row in range(0,5):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
#first line of lungs and trachea
for i in range(26,31):
for row in range(5,6):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
for i in range(32,34):
for row in range(5,6):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
for i in range(35,40):
for row in range(5,6):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
#second line of lungs and trachea
for i in range(24,31):
for row in range(6,7):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
for i in range(32,34):
for row in range(6,7):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
for i in range(35,42):
for row in range(6,7):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
#third line of lungs and trachea
for i in range(23,31):
for row in range(7,8):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
for i in range(32,34):
for row in range(7,8):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
for i in range(35,43):
for row in range(7,8):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
#fourth line of lungs and trachea
for i in range(22,31):
for row in range(8,9):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
for i in range(32,34):
for row in range(8,9):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
for i in range(35,44):
for row in range(8,9):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
#fifth line of lungs and bifurcation of trachea
for i in range(21,45):
for row in range(9,10):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
#sixth line of lungs and bifurcation of trachea
for i in range(20,46):
for row in range(10,11):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
#seventh-ninth lines of lungs
for i in range(19,31):
for row in range(11,14):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
for i in range(35,47):
for row in range(11,14):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
#tenth-fourteenth lines of lungs
for i in range(18,31):
for row in range(14,19):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
for i in range(35,48):
for row in range(14,19):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
#fiftenth-eighteenth lines of lungs
for i in range(17,31):
for row in range(19,23):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
for i in range(36,49):
for row in range(19,23):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
#ninteenth line of lungs
for i in range(17,30):
for row in range(23,24):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
for i in range(37,49):
for row in range(23,24):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
#twentieth line of lungs
for i in range(17,29):
for row in range(24,25):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
for i in range(38,49):
for row in range(24,25):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
#twenty-first line of lungs
for i in range(17,28):
for row in range(25,26):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
for i in range(39,48):
for row in range(25,26):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
#twenty-second line of lungs
for i in range(17,27):
for row in range(26,27):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
for i in range(40,48):
for row in range(26,27):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
#twenty-third line of lungs
for i in range(18,26):
for row in range(27,28):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
for i in range(41,48):
for row in range(27,28):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
#twenty-fourth line of lungs
for i in range(19,25):
for row in range(28,29):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
for i in range(42,47):
for row in range(28,29):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
#twenty-fifth line of lungs
for i in range(19,24):
for row in range(29,30):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
for i in range(43,47):
for row in range(29,30):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
delta_x = delta_y = 1
xold,yold = xSTART,ySTART
canv.move(ball, xold, yold)
while RUNNING:
objects = canv.find_overlapping(canv.coords(ball)[0],
canv.coords(ball)[1],
canv.coords(ball)[2],
canv.coords(ball)[3])
#Only change the direction once (so will bounce off 1st
# block even if 2 are hit)
dir_changed=False
for obj in objects:
if (obj != ball):
if dir_changed==False:
determineDir(canv.coords(ball),canv.coords(obj))
dir_changed=True
if (obj >= brick[0]) and (obj <= brick[len(brick)-1]):
canv.delete(obj)
if (obj == bottom):
text = canv.create_text(330,400,text="OOPS! TRY AGAIN!")
canv.coords(ball, (xSTART,ySTART,
xSTART+BALL_SIZE,ySTART+BALL_SIZE))
delta_x = delta_y = 1
canv.update()
time.sleep(3)
canv.delete(text)
new_x, new_y = delta_x, delta_y
canv.move(ball, new_x, new_y)
canv.update()
time.sleep(0.005)
答案 0 :(得分:0)
例如,您可以创建两个不同颜色的矩形来形成一个桨,并同时移动它们 如果“香烟”会“燃烧”并随着游戏的进行而缩短,那将会很有趣。
也就是说,tkinter
应与mainloop
一起使用,而不是while
循环。使用time.sleep
代替root.after()
import tkinter as TK
import time
VERT, HOREZ = 0, 1
xTOP, yTOP = 0, 1
xBTM, yBTM = 2, 3
MAX_WIDTH, MAX_HEIGHT = 640, 480
xSTART, ySTART = 50, 200
BALL_SIZE = 10
RUNNING = True
def close():
global RUNNING
RUNNING = False
root.destroy()
def move_right(event):
if canv.coords(paddle1)[xBTM]<(MAX_WIDTH-7):
canv.move(paddle1, 20, 0)
canv.move(paddle2, 20, 0) ## here
def move_left(event):
if canv.coords(paddle1)[xTOP]>7:
canv.move(paddle1, -20, 0)
canv.move(paddle2, -20, 0) ## here
def determineDir(ball,obj):
global delta_x,delta_y
if (ball[xTOP] == obj[xBTM]) or (ball[xBTM] == obj[xTOP]):
delta_x = -delta_x
elif (ball[yTOP] == obj[yBTM]) or (ball[yBTM] == obj[yTOP]):
delta_y = -delta_y
root = TK.Tk()
root.title("Bouncing Ball")
root.geometry('%sx%s+%s+%s' %(MAX_WIDTH, MAX_HEIGHT, 100, 100))
root.bind('<Right>', move_right)
root.bind('<Left>', move_left)
root.protocol('WM_DELETE_WINDOW', close)
canv = TK.Canvas(root, highlightthickness=0)
canv.pack(fill='both', expand=True)
top = canv.create_line(0, 0, MAX_WIDTH, 0, fill='blue',
tags=('top'))
left = canv.create_line(0, 0, 0, MAX_HEIGHT, fill='blue',
tags=('left'))
right = canv.create_line(MAX_WIDTH, 0, MAX_WIDTH, MAX_HEIGHT,
fill='blue', tags=('right'))
bottom = canv.create_line(0, MAX_HEIGHT, MAX_WIDTH, MAX_HEIGHT,
fill='blue', tags=('bottom'))
ball = canv.create_rectangle(0, 0, BALL_SIZE, BALL_SIZE,
outline='black', fill='black', tags=('ball'))
## here ##########################################################
paddle1 = canv.create_rectangle(100, MAX_HEIGHT - 20, 140, 470,
outline='black', fill='white', tags=('rect'))
paddle2 = canv.create_rectangle(140, MAX_HEIGHT - 20, 170, 470,
outline='black', fill='yellow', tags=('rect'))
## here ##########################################################
#row is 64 boxes wide
brick=list()
#trachea
for i in range(32,34):
for row in range(0,5):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
#first line of lungs and trachea
for i in range(26,31):
for row in range(5,6):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
for i in range(32,34):
for row in range(5,6):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
for i in range(35,40):
for row in range(5,6):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
#second line of lungs and trachea
for i in range(24,31):
for row in range(6,7):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
for i in range(32,34):
for row in range(6,7):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
for i in range(35,42):
for row in range(6,7):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
#third line of lungs and trachea
for i in range(23,31):
for row in range(7,8):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
for i in range(32,34):
for row in range(7,8):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
for i in range(35,43):
for row in range(7,8):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
#fourth line of lungs and trachea
for i in range(22,31):
for row in range(8,9):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
for i in range(32,34):
for row in range(8,9):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
for i in range(35,44):
for row in range(8,9):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
#fifth line of lungs and bifurcation of trachea
for i in range(21,45):
for row in range(9,10):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
#sixth line of lungs and bifurcation of trachea
for i in range(20,46):
for row in range(10,11):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
#seventh-ninth lines of lungs
for i in range(19,31):
for row in range(11,14):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
for i in range(35,47):
for row in range(11,14):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
#tenth-fourteenth lines of lungs
for i in range(18,31):
for row in range(14,19):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
for i in range(35,48):
for row in range(14,19):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
#fiftenth-eighteenth lines of lungs
for i in range(17,31):
for row in range(19,23):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
for i in range(36,49):
for row in range(19,23):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
#ninteenth line of lungs
for i in range(17,30):
for row in range(23,24):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
for i in range(37,49):
for row in range(23,24):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
#twentieth line of lungs
for i in range(17,29):
for row in range(24,25):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
for i in range(38,49):
for row in range(24,25):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
#twenty-first line of lungs
for i in range(17,28):
for row in range(25,26):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
for i in range(39,48):
for row in range(25,26):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
#twenty-second line of lungs
for i in range(17,27):
for row in range(26,27):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
for i in range(40,48):
for row in range(26,27):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
#twenty-third line of lungs
for i in range(18,26):
for row in range(27,28):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
for i in range(41,48):
for row in range(27,28):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
#twenty-fourth line of lungs
for i in range(19,25):
for row in range(28,29):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
for i in range(42,47):
for row in range(28,29):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
#twenty-fifth line of lungs
for i in range(19,24):
for row in range(29,30):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
for i in range(43,47):
for row in range(29,30):
brick.append(canv.create_rectangle(i*10, row*10,
((i+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))
delta_x = delta_y = 1
xold,yold = xSTART,ySTART
canv.move(ball, xold, yold)
while RUNNING:
objects = canv.find_overlapping(canv.coords(ball)[0],
canv.coords(ball)[1],
canv.coords(ball)[2],
canv.coords(ball)[3])
#Only change the direction once (so will bounce off 1st
# block even if 2 are hit)
dir_changed=False
for obj in objects:
if (obj != ball):
if dir_changed==False:
determineDir(canv.coords(ball),canv.coords(obj))
dir_changed=True
if (obj >= brick[0]) and (obj <= brick[len(brick)-1]):
canv.delete(obj)
if (obj == bottom):
text = canv.create_text(330,400,text="OOPS! TRY AGAIN!")
canv.coords(ball, (xSTART,ySTART,
xSTART+BALL_SIZE,ySTART+BALL_SIZE))
delta_x = delta_y = 1
canv.update()
time.sleep(3)
canv.delete(text)
new_x, new_y = delta_x, delta_y
canv.move(ball, new_x, new_y)
canv.update()
time.sleep(0.005)
答案 1 :(得分:0)
您可以使用for而不是一遍又一遍地重复相同的代码。
for start, stop, row_start, row_stop in ((32, 34, 0, 5),
(26, 31, 5, 6),
(32, 34, 5, 6)): ## etc
for ctr in range(start, stop):
for row in range(row_start, row_stop):
brick.append(canv.create_rectangle(ctr*10, row*10,
((ctr+1)*10)-2, ((row+1)*10)-2,
outline='pink', fill='pink',
tags=('rect')))