0
게임이 끝난 후 화면을 변경하는 데 문제가 있습니다. 나는 이것을 다음과 같이 시도했다 :파이썬 코드에서 화면 변경 Kivy
Manager.current = 'endmenu'
그러나 작동하지 않는다. 이것은 내가 스위치를()를 사용하여 내 코드 형태의 KV의 조각과 평 파일
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()
내가하고 싶은 것은 화면을 바꾸는 것입니다. 이름 'endmenu'가 k가 아니기 때문에 화면을 바꾸는 것입니다. 파이썬 파일에 nown, 그 지식을 가지고도이 문제를 해결하는 방법을 모른다.
내가 코드 내 함수를 사용하려고 : 당신이 관리자 클래스에있는 경우
sm.switch()
, 당신은
self.current
내가이의 작은 예를 만들어 사용할 수 있습니다 나는 얻는다 : switch() missing 1 개의 필수 위치 인수 : 'self'. 나는 Manager.switch로 사용합니다. –@PiotrSnopek 제게 그 기능을 보여줄 수 있습니까? – EL3PHANTEN
Manager.switch()를 사용하지 말고 객체로 만듭니다. sm = Manager()와 같고 빌드에서 sm을 반환합니다. 그럼,'sm.switch()'라고 말할 수 있습니다. – EL3PHANTEN