這是我制作的游戲我希望生成的圓圈在沒有實際接觸的情況下穿過線移動,當沒有按鍵時圓圈向右和向下移動,并且在按下空格鍵時應該跳躍,當它與線(我為 hitreg 制作的邏輯)碰撞時,它將以全新的線和起始位置的圓圈重新啟動。當它從一端移動到另一端時,它將以具有更多磁區的新線重新開始,因此當您通過關卡時游戲會變得很困難。我現在唯一遇到的問題是,如果不立即退出代碼,我就無法正確啟動游戲,我認為原因可能是我計算的坐標不夠準確,有時圓圈會越線仍然沒有
游戲代碼
from tkinter import *
from random import randint as rand
import keyboard, math
window = Tk()
window.geometry("1440x720")
canvas = Canvas(window, width=1440, height=720,
bg="white")
l1, l2, l3 = [], [], []
x = 1
b = canvas.create_oval(0, 300, 10, 300 60)
c = b
def genLine():
global x
f = 0
z = 360
y = 1440 / x
while x != 0:
ran = rand(300, 420)
u = canvas.create_line(f, z, y, ran)
r = canvas.coords(u)
l1.append(r)
f = y
x -= 1
y = y
z = ran
def hitReg():
global l2, l3
for i in l1:
grad = ((i[3] - i[1]) / (i[2] - i[0]))
l2.append(grad)
for i in l2:
for f in l1:
x = 0
length = f[2] - f[0]
while x != length:
point = x * i f[1]
l3.append(math.ceil( point))
x = 0.25
def move_b():
global l3, x
canvas.move(c, 1, 1)
y = canvas.coords(c)
if keyboard.is_pressed('space'):
canvas.move(b, 1, -5)
elif y[1] in l3 or y[3] in l3:
exit()
elif y[2] >= 1440:
x = 1
genLine()
hitReg()
move_b()
else:
pass
window.after(10, move_b)
genLine()
hitReg()
move_b()
canvas.pack()
window.mainloop()
uj5u.com熱心網友回復:
主要問題是
elif y[1] in l3 or y[3] in l3:
因為l3具有所有y在線值,并且它與所有值進行比較y[1],但它應該只與最近的 - 與與中心相同的點進行比較。y[3]yxoval
我正在考慮使用canvas.find_overlapping()orcanvas.find_closest()但它需要為在線的每個命中點創建小rectange或小。oval
最后,我創建了生命值,1px而不是0.25px這樣我就可以將它們放在串列中并輕松獲得
point_y = points[ int(oval_center_x) ]
后來檢查oval_y1 < point_y < oval_y2。
我也曾經window.bind("<space>", function)搬家oval,所以我不需要額外的模塊keyboard。此外,我使用 Linux 并且keyboard在 Linux 上需要admin特權。
我還更改了變數和函式的名稱以使其更具可讀性。
至于exit()我創建的函式reset(),它設定level、洗掉所有行canvas.delete(*lines)、移動oval到開頭 canvas.moveto(oval, 0, 330),并生成新行(使用 value level)并生成hit points. 使用reset(level 1)I 可以生成下一級,使用reset(1)I 可以回傳第一級。
import tkinter as tk # PEP8: `import *` is not preferred
from random import randint # PEP8: every module in separted line
# --- functions --- # PEP8: all functions directly after imports
# PEP8: `lower_case_names` for functions and variables
def generate_lines(level):
regions = []
lines = []
x1 = 0
y1 = 360
step = 1440 / level
x2 = step
for i in range(level):
y2 = randint(300, 420) # 360-60, 360 60
region = (x1, y1, x2, y2)
regions.append(region)
line = canvas.create_line(x1, y1, x2, y2)
lines.append(line)
x1 = x2
y1 = y2
x2 = step
return lines, regions
def generate_hit_points(regions):
points = []
for x1,y1,x2,y2 in regions:
steps = int(x2 - x1)
dy = (y2 - y1) / steps
y = y1
for _ in range(steps): # cant use `range(y1, y2, dy)` because `dy` is `float`, not `int`
points.append(y) # I don't need `x`
y = dy
return points
def move_oval():
canvas.move(oval, 1, 1)
x1, y1, x2, y2 = canvas.coords(oval)
oval_center_x = int(x1 10)
point_y = points[oval_center_x]
#print(y1, '<', int(point_y), '<', y2, '?')
#if y1 >= point_y or point_y >= y2: # less readable
if not (y1 < point_y < y2): # more readable
print('end')
reset(1)
elif x2 >= 1440:
print('next level')
reset(level 1)
window.after(25, move_oval)
def on_press_space(event):
canvas.move(oval, 0, -15)
def reset(new_level):
# all globals moved to one function
global lines
global points
global level
level = new_level
canvas.delete(*lines) # remove all lines
canvas.moveto(oval, 0, 330) # move oval to the beginning
lines, regions = generate_lines(level)
points = generate_hit_points(regions)
# --- main ---
lines = [] # PEP8: every variable in separted line
points = [] # PEP8: every variable in separted line
level = 1
window = tk.Tk()
window.geometry("1440x720")
canvas = tk.Canvas(window, width=1440, height=720, bg="white")
canvas.pack()
oval = canvas.create_oval(0, 360-30, 10, 360 30)
window.bind('<space>', on_press_space)
window.bind('q', lambda event:window.destroy())
reset(1)
# start moving
move_oval()
window.mainloop()
PEP 8——Python 代碼風格指南
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/431690.html
