我是python和kivy的新手。 我一直在尝试用Python开发一个简单的Pong游戏。一切顺利,直到我试图整理菜单。 我使用ScreenManager尝试浏览两个屏幕。但是当我按下按钮时,没有任何反应。 每个答案或想法都很受欢迎!
这是我的.py代码:
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import NumericProperty,
ReferenceListProperty,ObjectProperty
from kivy.vector import Vector
from kivy.clock import Clock
import pygame
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.gridlayout import GridLayout
from kivy.lang import Builder
class Menu(GridLayout,Screen):
def __init__(self, **kwargs):
super(Menu, self).__init__(**kwargs)
pass
class PongSpadi(Widget):
stig = NumericProperty(0)
def skoppa_bolti(self, bolti):
if self.collide_widget(bolti):
vx, vy = bolti.velocity
offset = (bolti.center_y - self.center_y) / (self.height / 2)
skoppadur = Vector(-1 * vx, vy)
vel = skoppadur
bolti.velocity = vel.x, vel.y + offset
class PongBolti(Widget):
velocity_x = NumericProperty(0)
velocity_y = NumericProperty(100)
velocity = ReferenceListProperty(velocity_x, velocity_y)
def move(self):
self.pos = Vector(*self.velocity) + self.pos
class PongGame(Screen, Widget):
bolti = ObjectProperty(None)
spilari1 = ObjectProperty(None)
spilari2 = ObjectProperty(None)
h1 = NumericProperty(310)
h2 = NumericProperty(310)
def serve_bolti(self, vel=(12, 6)):
self.bolti.center = self.center #Staðsetur boltann í miðjuna
self.bolti.velocity = vel
def update(self, dt):
self.bolti.move()
self.spilari1.skoppa_bolti(self.bolti) #skoppar bolta af spöðum
self.spilari2.skoppa_bolti(self.bolti)
if (self.bolti.y < 0) or (self.bolti.top > self.height):
self.bolti.velocity_y *= -1 #Lætur boltan skoppa ef hann snertir vegginn uppi/niðri
if self.bolti.x <self.x:
self.spilari2.stig += 1 #skráir stig ef bolti fer í hliðarnar
self.serve_bolti(vel= (12,6)) #byrjar nýtt uppkast
if self.bolti.x > self.width:
self.spilari1.stig += 1
self.serve_bolti(vel = (-12,6))
keys = pygame.key.get_pressed() #Takka stjórnun fyrir leikmann 1
if self.h1 < 567: #Bannar spaða 1 að fara of hátt
if self.h1 > 25: #Bannar spaða 1 að fara of látt
if keys[pygame.K_z]: #býr til takka fyrir spaða 1
self.h1 -= 10
elif keys[pygame.K_a]:
self.h1 += 10
if self.h1 >= 567: #ýtir honum til baka ef farið er of hátt
self.h1 -= 10
if self.h1 <= 25: #ýtir honum til baka ef farið er of látt
self.h1 += 10
keys = pygame.key.get_pressed() #takka stjórnun fyrir leikmann 2
if self.h2 < 567:
if self.h2 > 25:
if keys[pygame.K_m]:
self.h2 -= 10
elif keys[pygame.K_k]:
self.h2 += 10
if self.h2 >= 567: #ýtir honum til baka ef farið er of hátt
self.h2 -= 10
if self.h2 <= 25: #ýtir honum til baka ef farið er of látt
self.h2 += 10
pass
sm = ScreenManager()
sm.add_widget(Menu(name='menu'))
sm.add_widget(PongGame(name='ponggame'))
class PongApp(App): #ræsir forritið
title = "Menu"
def build(self): #kallar á eiginleika leiksins
sm = Menu()
return sm
sm = PongGame()
sm.serve_bolti() #kallar á uppkast boltans
Clock.schedule_interval(sm.update, 1.0 / 60) #Lætur leikinn uppfærast 60 sinnum á sekúndu.
return sm
if __name__ == '__main__':
PongApp().run()
这是我的.kv代码:
#: kivy 1.9.1
ScreenManager:
Menu:
PongGame:
<Menu>
name: 'menu'
Button:
on_press: app.root.current = 'ponggame'
on_press: print("virkar")
text: "Manneskja á móti manneskju"
font_size: 20
center_x: root.width / 2
top: root.top / 1.5
size: 275, 50
Label: #nafn á leik
font_size: 100
center_x: root.width / 2
top: root.top - 50
text: 'Pong og Gervigreind'
Label:
font_size: 20
center_x: root.width / 2
top: root.top -120
text: 'Lokaverkefni eftir Hróbjart Höskuldsson vor 2018'
<PongBolti>:
size: 20, 20
canvas:
Rectangle:
pos: self.pos
size: self.size
<PongSpadi>:
size: 23, 100
canvas:
Rectangle:
pos:self.pos
size:self.size
<PongGame>:
name: 'ponggame'
bolti: pong_bolti
spilari1: spilari_vinstri
spilari2: spilari_haegri
canvas:
Rectangle:
pos: self.center_x - 5, 0
size: 3, self.height
Label: # staða leikmanns 1
font_size: 70
center_x: root.width / 4
top: root.top - 50
text: str(root.spilari1.stig)
Label: # staða leikmanns 2
font_size: 70
center_x: root.width * 3 / 4
top: root.top - 50
text: str(root.spilari2.stig)
PongBolti:
id: pong_bolti
center: self.parent.center
PongSpadi:
id: spilari_vinstri
x: root.x + 30
center_y: root.h1
PongSpadi:
id: spilari_haegri
x: root.width-self.width - 30
center_y: root.h2
Button:
on_release: app.root.current = 'menu'
on_press: print("virkar")
text: "Manneskja á móti manneskju"
font_size: 20
center_x: root.width / 2
top: root.top / 1.5
size: 275, 50
答案 0 :(得分:1)
这不是一个完整的答案,而是一些让你感动的想法:
您的build()
方法应返回ScreenManager
。我认为它应该是这样的:
def build(self): #kallar á eiginleika leiksins
sm = ScreenManager()
sm.add_widget(Menu())
sm.add_widget(PongGame())
return sm
您的PongGame
不需要展开{{1}}(Widget
是一个小工具)
您的Screen
课程不需要同时延伸Menu
和Screen
。只需在GridLayout
中使用GridLayout
,就像这样:
Menu
您的<Menu>
name: 'menu'
GridLayout:
rows: 3
Button:
on_press: app.root.current = 'ponggame'
on_press: print("virkar")
text: "Manneskja á móti manneskju"
font_size: 20
center_x: root.width / 2
top: root.top / 1.5
size: 275, 50
Label: #nafn á leik
font_size: 100
center_x: root.width / 2
top: root.top - 50
text: 'Pong og Gervigreind'
Label:
font_size: 20
center_x: root.width / 2
top: root.top -120
text: 'Lokaverkefni eftir Hróbjart Höskuldsson vor 2018'
方法中的代码看起来可能正在启动游戏。我认为在build()
的{{1}}方法中可能会更好。