Wednesday, January 17, 2018

Building Games With Python 3 and Pygame: Part 3

Overview

This is part three of a five-part series of tutorials about making games with Python 3 and Pygame. In part two, we covered the TextObject class used to render text on the screen, created the main window, and learned how to draw objects like bricks, the ball, and the paddle. 

In this part, we will dive into the heart of Breakout and learn how to handle events, meet the main Breakout class, and see how to move the different game objects.

Handling Events

In Breakout, there are three types of events: key press events, mouse events, and timer events. The main loop in the Game class takes care of the key press and mouse events and delivers them to subscribers (by calling a handler function). 

While the Game class is very generic and has no Breakout specific knowledge, the subscription itself and how to handle each event are very specific.

The Breakout Class

The Breakout class is where most of the domain knowledge about the Breakout game is managed. We will meet the Breakout class several times during this series. Here are the lines that register the various event handlers. 

Note that all key events for both the left and right arrow keys will go to the same handler method of the paddle.

Handling Key Presses

The Game class will call the registered handlers for each key event and pass the key. Note that it doesn't the Paddle class. In Breakout, the only object that is interested in these events is the paddle. When a key is pressed or released, its handle() method is called. 

The Paddle doesn't need to know if it was a key down or key up event because it manages the current state through a pair of boolean variables: moving_left and moving_right. If moving_left is True then it means the left arrow key is pressed and the next event will be key up, which will release it. The same goes for the right arrow key. The logic is as simple as toggling these variables in response to any event. 

Handling Mouse Events

Breakout has a game menu you'll meet soon. The button on the menu handles various mouse events such as movement and clicks (mouse down and mouse up events). In response to these events, the button updates an internal state variable. Here is the mouse handling code:

Note that the handle_mouse_event() method that is registered to receive mouse events checks the type of the event and forwards it to the specific method that handles that event type.

Handling Timer Events

Timer events are not processed through the main loop. However, since the main loop is called every frame, it's easy to check if a certain timed event is due. You will see an example of this later when discussing timed special effects. 

Another situation is when we want to freeze the game—for example, when displaying a message that the player should be able to read without distraction. The show_message() method of the Breakout class uses this approach and calls time.sleep(). Here is the relevant code:

Gameplay

The gameplay part is where the rules of Breakout come to play (see what I did there?). Gameplay is about moving the different objects in response to the events and changing the game state based on their interactions.

Moving the Paddle

You saw earlier that the Paddle class responds to arrow keys by updating its moving_left and moving_right fields. The actual movement happens in the update() method. There is some computation going on here if the paddle is close to the left or right edge of the screen. We don't want the paddle to move beyond the edges of the screen (including a predefined offset). 

So if the movement would have taken it beyond the edge, the code adjusts the movement to stop exactly at the edge. Since the paddle is only moving horizontally, the vertical component of the movement is always zero. 

Moving the Ball

The ball just uses the functionality of the GameObject base class, which moves game objects based on their speed (with horizontal and vertical components). The speed of the ball is determined by many factors in the Breakout class that you'll see soon. Since moving is just adding the speed to the current position, the direction the ball is moving is fully determined by its speed along the horizontal and vertical axes.

Setting the Initial Ball Speed

The ball in Breakout appears out of nowhere at the beginning of the game every time the player loses a life. It just materializes out of the ether and starts dropping either straight down or at a slight angle. When the ball is created in the create_ball() method, it receives a speed with a random horizontal component between -2 and 2 and a vertical component, which is determined in the config.py module (currently set to 3). 

Conclusion

In this part, we covered event handling such as key presses, mouse movement, and mouse clicks. We also dove into some of the gameplay elements of Breakout such moving the paddle, moving the ball, and controlling the ball's speed. 

Remember also to see what we have available for sale and for study in the Envato Market if you're looking to study more Python-related material.

In part four, we will deal with the important topic of collision detection and see what happens when the ball hits various game objects like the paddle, bricks, and even the walls, ceiling, and the floor. 

Then we'll turn our attention to the game menu. We'll create custom buttons that we'll use as a menu we can show and hide as necessary.


by Gigi Sayfan via Envato Tuts+ Code

7 Ways to Use Instagram Video for Business

Wondering how to create Instagram videos that will resonate with your audience? Looking for inspiration? In this article, you’ll discover seven ways to use Instagram video to enhance your stories and timeline with engaging content. #1: Integrate Instagram Stories Video An estimated 250+ million active users view Instagram Stories every day. Because Stories content has [...]

This post 7 Ways to Use Instagram Video for Business first appeared on .
- Your Guide to the Social Media Jungle


by Victor Blasco via

Highly Effective Healthy Habits of Working Online - #Infographic

Working from home is one of the biggest dreams of many employees these days. While it comes with its own challenges, the benefits can vastly outweigh any of these hesitations. If you’ve discovered you work your best outside of an office environment, here are some practices to stay fit when your...

[ This is a content summary only. Visit our website http://ift.tt/1b4YgHQ for full links, other content, and more! ]

by Web Desk via Digital Information World

Leviev Group

Leviev Group is a design & construction company that creates one-of-a-kind projects thought out to the last detail
by via Awwwards - Sites of the day

jQuery Source Viewer

jQuery Source Viewer is an interface that allow you to quickly traverse each method's source.

It allows you to study specific parts of jQuery’s source. You can type in a method name and you’ll see its implementation straight away. It will also linkify all function names within the presented source.


by via jQuery-Plugins.net RSS Feed

Tuesday, January 16, 2018

6 Free Twitter Marketing Tools - #Infographic

Are you looking for ways to improve your Twitter marketing strategy? Want some tools that can help you to build followers, schedule posts and generate engagement? Red Website Design share 6 tools you should try in the infographic below. They all have free versions, you can upgrade them to unlock...

[ This is a content summary only. Visit our website http://ift.tt/1b4YgHQ for full links, other content, and more! ]

by Web Desk via Digital Information World

How to Create a Budget Worksheet in Excel - #infographic

Want to build you own customised budget? this infographic will help you to get started off on the right foot. Use step by step guide to creat your own budget and start working towards your financial goals.

[ This is a content summary only. Visit our website http://ift.tt/1b4YgHQ for full links, other content, and more! ]

by Web Desk via Digital Information World