• 3

這個程式碼到底哪裡出錯了?

以下的程式碼看了好久都找不出來到底哪裡出錯了,人生真是甘苦啊!

這裡高手眾多,可否出手相救一下?

from gturtle import *
from time import sleep

# Change these values to adapt to your screen
SCREEN_LEFT = -400
SCREEN_RIGHT = -SCREEN_LEFT
SCREEN_TOP = 300
SCREEN_BOTTOM = -SCREEN_TOP

# Here you can design your level!
# Each platform comprises three numbers: x_left, x_right, height(y)
PLATFORMS = [(-280, -200, 36),
(-150, 30, 45),
(60, 165, 60),
(-200, -44, 100),
(6, 288, 120),
(50, 90, 150),
(-100, 30, 180),
(-250, -190, 210),
(-80, 250, 270),
(-350, -150, 320),
(280, 350, 330),
(-120, -10, 360)]
# Specify the coordinates and radius of the goal to reach
GOAL = (50, 400, 30)

# Change these values to influence (key) control
KEYSPEED = 6
MAXSPEED = 15
JUMPSPEED = 12
JUMPFACTOR = 1.5
GRAVITY = -1.25
DAMPING = 0.75
JUMPDAMPING = 0.95
LOOPDELAY = 0.1

# Do not change these!
LEFT = -1
RIGHT = 1
GROUNDLEVEL = SCREEN_BOTTOM + 36

BREAK = False # Used to terminate the program
MARIO_HEIGHT = 7
MARIO_WIDTH = 8

# The position and speed of Mario itself
pos_x = SCREEN_LEFT + 10
pos_y = GROUNDLEVEL
speed_x = 0
speed_y = 0
direction = 0 # Currently not used

def bound(x, min_x, max_x):
"""Return the x-value, bounded to the given min and max."""
return min(max(x, min_x), max_x)

def update():
"""Update the turtle's position."""
global pos_x, pos_y, speed_x, speed_y
# Update the current speed (y)
gl = getGroundLevel(pos_x, pos_y)
if gl < pos_y or speed_y > 0:
speed_y = bound(speed_y + GRAVITY, -MAXSPEED, MAXSPEED)
else:
speed_y = 0
# Update the current speed (x)
if speed_x != 0:
if speed_y == 0:
speed_x = bound(speed_x * DAMPING, -MAXSPEED, MAXSPEED)
else:
speed_x = bound(speed_x * JUMPDAMPING, -MAXSPEED, MAXSPEED)
if -0.5 < speed_x < 0.5:
speed_x = 0
# Update the position
if speed_x != 0 or speed_y != 0:
pos_x = bound(pos_x + speed_x, SCREEN_LEFT, SCREEN_RIGHT)
pos_y = bound(pos_y + speed_y, gl, SCREEN_TOP)
setPos(pos_x, pos_y + MARIO_HEIGHT)

def setSpeed(speedX=None, speedY=None):
"""Set the character's speed and direction.
Use 'None' to keep the current speed."""
global speed_x, speed_y, direction
if speedX != None:
speed_x = speedX
if speedY != None:
speed_y = speedY
if speed_x >= 0:
d = RIGHT
else:
d = LEFT

def getGroundLevel(x, y):
"""Return the ground-level for the given coordinate."""
result = GROUNDLEVEL
mw = MARIO_WIDTH // 2
for x_left, x_right, height in PLATFORMS:
if x_left-mw <= x <= x_right+mw:
gl = GROUNDLEVEL + height
if y >= gl and gl > result:
result = gl
return result

def goalReached():
"""Check if the goal has been reached."""
goal_x, goal_y, radius = GOAL
delta_x = (pos_x - goal_x) ** 2
delta_y = (pos_y - (goal_y+GROUNDLEVEL)) ** 2
return ((delta_x + delta_y) <= (radius ** 2))

def fillRectangle(x1, y1, x2, y2):
"""Draw a filled rectangle using the current pencolor."""
setPos(x1, y1)
fillToPoint()
for p in [(x1, y2), (x2, y2), (x2, y1), (x1, y1)]:
moveTo(p)
fillOff()

def paintPlatforms():
"""Paint all the platforms Mario can stand on."""
penWidth(6)
setPenColor(makeColor("chocolate"))
for x_left, x_right, height in PLATFORMS:
y = GROUNDLEVEL + height - 3
setPos(x_left, y)
moveTo(x_right, y)

def paintGoal():
"""Paint the goal to reach."""
goal_x, goal_y, radius = GOAL
setPenColor(makeColor("gold"))
setPos(goal_x, goal_y + GROUNDLEVEL)
dot(radius)
setPenColor("black")
dot(radius * 3 // 4)

def paintScene():
"""Paint the entire scene/background."""
clear(makeColor("sky blue"))
setPenColor(makeColor("forest green"))
fillRectangle(SCREEN_LEFT, GROUNDLEVEL, SCREEN_RIGHT, SCREEN_BOTTOM)
paintPlatforms()
paintGoal()
penUp()

def onKeyPressed(code):
"""React to a pressed key."""
global speed_x, speed_y, BREAK
if code == 37: # LEFT
if speed_y == 0:
speed_x += LEFT * KEYSPEED
elif code == 38: # UP
if speed_y == 0:
speed_y = max(JUMPSPEED, JUMPFACTOR * abs(speed_x))
elif code == 39: # RIGHT
if speed_y == 0:
speed_x += RIGHT * KEYSPEED
elif code == 27:
BREAK = True

### MAIN ###

# Setup:
makeTurtle("sprites/mario.png", keyPressed=onKeyPressed)
hideTurtle()
paintScene()
moveTo(pos_x, pos_y + MARIO_HEIGHT)
setSpeed(None, None)
showTurtle()
heading(0)

# Run loop:
repeat:
sleep(LOOPDELAY)
if BREAK or isDisposed():
break
update()
if goalReached():
msgDlg("Congratulations! You won!")
break

# Close the window:
dispose()

### END PROGRAM ###

2017-10-16 10:20 發佈
文章關鍵字 程式碼
這是哪種程式語言?
編譯/執行的錯誤內容呢?

你自己都看不懂的東西
你希望別人能看懂什麼?
這看起來像是遊戲自動練功程式的設定檔吧~連程式碼都算不上
給你一個小忠告
寫程式要結構化
不然連你自己都看不懂
別人也不想看
不管程式寫的多複雜,你要學會如何分段檢查,這是程式最簡單的入門
再來如果是練功程式,很間單的方法就是找別人寫好的比對一下
就可以知道問題在哪邊了
joshwang wrote:
以下的程式碼看了好...(恕刪)


這是用python寫的程式對嗎?
可是我不會....不能幫上什麼忙,
但我知道,python的程式檔案編輯行要對齊的一致,
不然可能會被誤認為不同段函式,邏輯判斷也是這樣,不然會有直譯執行錯誤的問題,
我直覺是邏輯沒錯,但是分段可能有問題,所以是否先考慮把每個函式def跟if都先分行段分好?
也許問題就在這裡....
樓主的程式碼是Python程式語言,
Python程式碼中的空格不能亂加或亂減少,
不然程式會執行錯誤,

例如

C語言為
int a=10;
if(a>10) {
    print("aaaaa");
    print("bbbbb");
}
print("ccccc");

Python語言為
a=10
if a>10:
    print("aaaaa")
    print("bbbbb")
print("ccccc")
joshwang wrote:
以下的程式碼看了好...(恕刪)


請問...螢幕的邊界垂直軸最多跟最少是+300跟-300.
那麼PLATFORMS裡面的list為何有y值超過300的狀況?
我覺得這樣有點怪怪的......
(-350, -150, 320),
(280, 350, 330),
(-120, -10, 360)]
以上三點的y都是超過300....這應該是有問題的吧??

O.K.我懂了....因為
GROUNDLEVEL = SCREEN_BOTTOM + 36
換句話說GROUNDLEVEL = -300+36 = -264
以上再加上y=GROUNDLEVEL+height-3
或是gl=GROUNDLEVEL+height
也不會超過邊界....所以此段宣告是正確的
gn01116107 wrote:
樓主的程式碼是Python...(恕刪)


沒接觸過 Python,正在奇怪這種有 if ,沒有 endif,也沒有括號的語言,怎麼判斷你 if 的範圍。如果是靠縮排位置判斷就可以理解,這樣縮排位置真的就很重要了,多退一格少退一格,邏輯完全不一樣。
沒用過+1,
我只想到如果不小心打到一行空格,
或不小心打到換行,會查到死為止。
  • 3
內文搜尋
X
評分
評分
複製連結
Mobile01提醒您
您目前瀏覽的是行動版網頁
是否切換到電腦版網頁呢?