![]() In that case, the snake would reappear on the other side. Borders : at the moment the game is lost is the snake goes outbound, but in the snake I remember you could also choose to disable the borders (it even was the default setting wasn’t it?).Try your new build : it’s starting to look like something isn’t it ? One more step and we’ll be ready to package ! Adding the options SnakeApp.screen_manager.current = "welcome_screen" # if it happens to be the case, reset the game and switch back to In order to keep things simple, we’ll just call a change of screen if the game is lost. One more thing : how do we get back to the main menu when we’re in game ? Indeed, at that point we’re stuck on the playground as soon as we reach it. Don’t forget the button of the options popup : it will simply dismiss its parent widget. Play will tell the screen manager to switch to the playground and Options will trigger the show_popup() method we just defined. We’re going to specify a behavior for the on_press() event of the buttons. Give it a run : the welcome screen appears but hey, nothing happens if we press the buttons ! Maybe we should think of adding some bindings. # register the screens in the screen manager Ps = PlaygroundScreen(name="playground_screen") Ws = WelcomeScreen(name="welcome_screen") SnakeApp.screen_manager = ScreenManager() # declare the ScreenManager as a class property Here we’re going to declare the screen manager at the class level because we’ll need to call it without any direct reference to the parent holding it (we’ll be able to call the parent class, but not the object directly). When we wanted to interact with an object after its instanciation, we usually declared it as an instance property of the object holding it. What we want now is to add a ScreenManager to the App and to register the two screens. We have to anyway otherwise the bindings we’re going to make in a few steps wont work properly. ![]() We might as well prepare the OptionsPopup class right now. # we screen comes into view, start the game Thus you can delete the on_start() method from the main class and transfer the afferent logic here : We don’t want that anymore : the Welcome screen should be shown on App start, and the game should start only when the Playground comes into view. Recall how in the last part we started the game on App start. Yay Kivy ! Since we’ll act on the OptionsPopup, its instance will be stored as a property. We don’t have to define anything else for the Play button because it will use the ability of its parent to access the screen manager. The Welcome screen requires only one method : show_popup() that will be called when the Options button is pressed on the main screen. Both screens will inherit from Screen (what a surprise) and the OptionsPopup from Popup (again, how baffling). Let’s add the Python counterparts of the classes we defined in the kivy language, and the behavior we want to give them. It will contain the widgets needed to interact on the parameters and a Save button. ![]() The option popup will occupy 3/4 of the welcome screen. Layouts will help us organize these elements in term of dimensions and positioning. The main screen will be composed of several internal widgets that will help us organize the three main elements : the title of the App (I chose Ouroboros but feel free to call it what you want), a Play button that triggers the entry of the PlaygroundScreen and an Option button calling the Option popup. The PlaygroundScreen is the easiest : it only contains the playground ! : kv file, and only then write the corresponding classes in Python. First we will specify the layout of our widgets in the. I don’t count the widget where we’re going to display the options as a screen because it will be a popup placed on the welcome screen. Our application requires two screens : one for the welcome screen and one for the playground. It’s a good front-end exercise because this time we’ll rely a lot more on the kivy language. We’re just going to make a few arrangements here before packaging the app. The game engine is at 90% done at that point, and handling screens is very straightforward in kivy. If you got through it, rest assured that the hardest part is behind us. I hope you enjoyed the first two sections of the tutorial.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |