0
所以我有我的遊戲結束後更改我的屏幕的問題。 我試過這樣做:從python代碼改變屏幕Kivy
Manager.current = 'endmenu'
但它不起作用。這是我的代碼形式千伏的片段和PY文件
main.py,功能,我使用開關()
def bounce_ball(self, ball, score):
"""Check for collision between a ball and a circle"""
dx = ball.center_x - self.center_x
dy = ball.center_y - self.center_y
#print("Ball: ",ball.center)
#print("Center: ",self.center)
distance = Vector(ball.center).distance(self.center) - 50
if distance >= 100 + 30 and distance <= 100 + 30 + 4:# circle radius + ball size
vx, vy = ball.velocity
centertoballangle = Vector(0,1).angle((dx,dy))
if centertoballangle <= 0: #angle 0-360 not 0-180 to -180-0
centertoballangle += 360
ball.angle = Vector(vx,vy).angle((dx,dy))
if self.checkcolor(ball, centertoballangle, self.angle%360):
bounced = Vector(vx,vy)
if ball.angle > 90:
ball.angle-=90
elif ball.angle < -90:
ball.angle+=90
ball.angle = 180-2*ball.angle # set new angle off ball after bounce
ball.touchspeed*=2 # make changing angle a bit more aggresive
if ball.angle + ball.touchspeed < 350 or ball.angle + ball.touchspeed > 40:
ball.angle += ball.touchspeed # ball.touchspeed - change the angle!
bounced = bounced.rotate(ball.angle) # finally, rotate the ball
if score<12:
vel = bounced * 1.1 # make ball move faster
else:
vel = bounced
ball.velocity = vel.x , vel.y
ball.touchspeed=0
return True # add +1 to score
else:
Manager.switch()
return False
編輯的屏幕管理類:
class Manager(ScreenManager):
def __init__(self, *args, **kwargs):
super(Manager, self).__init__(*args, **kwargs)
self.current = 'menu'
def switch(self, where):
self.current = str(where)
sm = Manager(transition=WipeTransition())
class ColorPongApp(App):
def build(self):
return sm
和我的KV文件:
<Manager>
Screen:
name: 'menu'
BoxLayout:
Button:
text: 'Play Color Pong'
halign: 'center'
valign: 'middle'
font_size: 50
text_size: self.size
on_release: root.current = 'game';game.serve_ball()
Button:
text: 'Quit'
halign: 'center'
valign: 'middle'
font_size: 50
text_size: self.size
on_release: game.quit()
Screen:
name: 'game'
ColorPongGame:
id: game
Screen:
name: 'endmenu'
id: endmenu
BoxLayout:
Button:
text: 'Replay'
halign: 'center'
valign: 'middle'
font_size: 50
text_size: self.size
on_release: root.current = 'game';game.serve_ball()
Button:
text: 'Quit'
halign: 'center'
valign: 'middle'
font_size: 50
text_size: self.size
on_release: game.quit()
我想要做的就是改變屏幕,我認爲它不能做,因爲name'endmenu'不是k在Python文件中出現,即使有了這些知識,我也不知道如何解決這個問題。
當我試圖用我的功能代碼我得到:switch()缺少1個需要的位置參數:'self'。我用它作爲Manager.switch –
@PiotrSnopek你可以告訴我的功能? – EL3PHANTEN
不要使用Manager.switch(),使它成爲一個對象。像sm = Manager(),並在構建中返回sm。然後你可以說'sm.switch()' – EL3PHANTEN