Games with Python

With game development, you often have quite a bit of logic, mathematics, physics, artificial intelligence, and other things, all of which come together for game creation

Many times people like to visualize the programs they are creating, as it can help people to learn programming logic quickly. Games are fantastic for this, as your are specifically programming everything you see.

Installation may vary by operating system. Macs historically have had trouble with PyGame, so you may need to get an earlier version of Python and PyGame.

Nowadays, pip is probably the best method for installing PyGame, since it comes with the latest versions of Python 2 and Python 3.

import pygame

pygame.init() # This will initiate PyGame

Above, we've imported PyGame, which is obviously necessary to make use of the module! Then, we run pygame.init(), which is integral to every single PyGame application that you will ever write. This will initiate PyGame, and allow you to then make various commands with PyGame and our game.

gameDisplay = pygame.display.set_mode((800,600))
#we define our game's display,
# we want the resolution of our game to be 800 px wide and 600 px tall
pygame.display.set_caption('A bit Racey')
#The title of the window.

Next, we define our game's display, which is the main "display" for our game. You may also see this referred to as a "surface," as this is basically our canvas that we will draw things to, and the function literally returns a pygame.Surface object. We are saying right now that we want the resolution of our game to be 800 px wide and 600 px tall. Take note that this is a tuple as a function argument. If you do not make this a tuple with parenthesis, then 600 and 800 will be treated as separate parameters and the function will blow up. It's a big deal.

After that, we define our display's "caption." To me, it's more like a title, and is the title of the window. We've decided to call our game "A bit Racey." (tm!)

clock = pygame.time.Clock()

Simple enough, this is a our game clock. We use this to track time within the game, and this is mostly used for FPS, or "frames per second." While somewhat trivial seeming, FPS is very important, and can be tweaked as we will see later. For the most part, the average human eye can see ~30 FPS. It's important to note, however, that this is only a very general statement, since every human eye is slightly different, and the human eye does not process things in "frames." The better way to put it is that after about 30 FPS, people generally cannot tell the difference.

Take that 60 FPS YouTube. Anyway, we can increase FPS to literally speed up the game, or slow them down to slow down the game. This isn't ideal, especially when speeding up FPS, as the entire game loop is run per frame, and might be a massive waste of processing. More on this later though!

Okay, a bit more here. I don't like to separate loops and functions if I don't have to, since it can cause people to get indentation wrong. So, first, we've got a crashed = False statement, which is just a variable that we set initially. Then, we run our "game loop," which will run until we crash. Currently, the only way we're saying crashed = True is if the user exits out of the window, however.

You'll notice here that we have a for loop within this while loop. This is going to be present in most PyGame scripts, where events are constantly being logged. It is shown in the video, but not here, but you can still try it: Try adding "print event" above the if statement. You will see in your console everything you do within the PyGame window. Pretty neat!

After our if statement. you'll see that we run a pygame.display.update. It's important to note the difference between display.update and display.flip. Display.flip will update the entire surface. Basically the entire screen. Display.update can just update specific areas of the screen. That said, if you do not pass a parameter, then update will update the entire surface as well, bascially making flip() pointless for our interests. There might come times when you want to use flip for very specific tasks, however.

The last thing within this while loop is clock.tick(60). Basically, this is how many frames per second we are running. In this case, we are running 60 FPS.

pygame.quit() # pygame.quit(). This will end our pygame instance.
quit() # Then we can run a simple quit(), which will exit Python and the application.

Once we have broken our game loop, we want to run a pygame.quit(). This will end our pygame instance. Then we can run a simple quit(), which will exit Python and the application.

The functioning here is the same as before, but this time we're not hard coding the resolution. We want to use this method so that we can reference the variables. We're going to see how useful that can be already in this script, but even more later on.

Then, here:

black =(0,0,0)
white =(255,255,255)

We're defining our colors, using RGB formatting. If you want to know a bit more about RGB, check out the video.

Next up:

carImg = pygame.image.load('racecar.png')

Here, we load the racecar.png image to our carImg variable. racecar.png is an image that I have personally created. pull one from Google Images, or make your own. Chances are, you will do better than I did!

def car(x,y):
gameDisplay.blit(carImg,(x,y))

Now, we define our car function, which really just places the car to the display. "Blit" basically just draws the image to the screen, but we still have yet to fully show it to the display. In graphics, generally, there is a lot done in the background, and only when every update is done is when the screen is visually updated.

x =(display_width *0.45)
y =(display_height *0.8)

Here, we've defined the starting points for our car. See how we used out previously-defined variables? We're already seeing how this could be useful, especially if we EVER want to change our resolution. A lot of people start out writing programs, thinking that some of the variables will just never be changed. I've done it a few times. Always wrong and regretful.

Now, within our while loop, we see the addition of:

gameDisplay.fill(white)
car(x,y)

Here, we've filled our display with a color, white. What this does is cover everything in white. Paint the game white, so-to-speak. This will cover over any existing stuff. After that, we run our car function to draw the car to the screen. Always keep in mind the order here. If you drew the car first, then called the gameDisplay.fill function, you would have just covered your car and it would not show up.

Contact

Reviews

I visited many institutes to lean data structures for interviews. All of them are teaching DS at very basic level. I finally reached the right place ( Algorithm Class at KPHB ) and learnt interview oriented DS. I got enough confidence to attend product based MNC interviews. Thanks Algorithm Class for giving me great confidence.

Sarath Google Review

Hi, My name is Hemanth .Im from Nit calicut, btech 3 rd year,eee.First of all,im glad I attended Data structures course under sri hari sir guidance.we gained well knowledge in data structures for interviews in MNC companies.Hope to do well in our placements!!!

hemanth spicer

Hi ,i am lokesh from nit calicu, b.tech 3rd yr eee.I have attend data structures and algorithms in algorithm class. He explains in simple way and easy to under stand. Now , i got confidence to attend interviews for mnc’s .Thanku you sir ,for giving me great confidance.

Kakani Lokesh google review

I am working in USA from last 6 years in top MNCs in Product Development.I am preparing for google and amazon now.So I start taking these classes which helped me a lot.

Training material is really Good for getting selected into big MNCs like Google , Amazon etc along with good conceptual knowledge. Teacher is proficient in Interview questions related to data structure and algorithms.especially the collection of questions we do here in class are good standard and have been asked previously in companies.

Saurav Sharma Google review

I am Pranit Kothari having 6 years experience in IT, mostly C++. I found sir’s class really useful. We try to implement algorithms throughout the class.

Algorithms are not just important for interviews but they built up logic for programming. Sir’s explanation and way he involves people in through discussion is very useful and important to learn algorithm.

I find my problem solving skills much improved after taking class.

Pranit Kothari google review

Hello ,
I am a Sap consultant and took this course to enhance my data structure and algorithm skills to face technical interviews.
I found the online class engaging and interactive and instructor knowledegable and open to suggestions and each problem is explianed in detail and it is implemented by the instructor.
I would recommend taking this course if you are looking to sharpen your algorithm and data structure skills.
Thanks

Sumedh K Google Review

My name is Venkat having 8 years experience as ETL developer & I have attended Python course from algorithmtraining institute by Srihari sir. I have found the course useful, teaching was very good & practical. I would definitely recommend it to any one willing to learn python.

Best regards,
Venkat.

venkat reddy google review

This is very useful for interviews..
As a non CS student i have learnt data structure concepts..
Here they have explained with examples and code..I got confident to do better in my technical aspects.
I strongly recommend it to others who are interested to attend interviews with data structure..

Tarun Pachigolla google review

My name is Sudharsan Reddy. I joined the python course in this institute and Trainer was Srihari. The trainer was very practical and every minute in the class was worth. The class was two way (Duplex) always and tutor was paid attention to each student. After he explained each topic, he has given exercise and asked us to complete the exercise in the class itslef and was helped if any one got stuck. He has given home work everyday which was made me to recap the topic that he was explained in the class.This course is worth if you are serious/passion to learn the python.

sudharsan reddy Google Review

Mr. SriHari sir has been an excellent trainer for Python. He explains topics with relevant programs, while doing programs itself our theoretical part would be covered. Even though I’m not a Computer science background I am able to do lot of programs in class with help of trainer. Batch size is small. There is flexibility to attend the class either online or classroom. here trainer is open to discussions, questions and answers with patience.

Tejaswini Arrob Google Review

I am with industry around 5 years..
I got to know about the Algorithm class through google.
It was good experience with python class from SriHari.
Being a new bee for python, I had good learning experience through online training.

Learned Python during September 2017. I would suggest Algorithm class to anyone who wants to learn Python with hands on approach. The teaching method will be to teach concept first and write a program on it in the class. Trainer explains one program and the students has to write the next programs with some extension of requirement. The class timings are good for working employees.

Ramesh Babu Kadali Google Review

I am rama ,studying btech 2nd year at nit calicut.I have taken python course at algorithm class under Sri Hari sir’s guidance…now I think I am almost perfect with the basics…Sir gave us excellent guidance and his class was very interactive , application oriented,he used to make us practice many example programs which we need to do by ourselves and he also made us to do a project based on python which I think would be useful during my placements…so I finally conclude that the coaching was worth to me and sir’s teaching was excellent

Rahul pulikallu google review

I m Akhil from NIT CALICUT . 2nd year.I have taken PYTHON course in this Institute taught by Mr .SRI HARI sir who has been cooperative throughout the course with sufficient patience to our doubts.
Undoubtedly U can choose this if you are searching for this course as it is entirely different from others concentrating on practical implementation of things we learn which probably is rare.
If you are hungry enough to play with the course then you have reached the ground(destination) you are exploring. A mini project added up to our feast of joy in learning python.

akhil chowdary google review

I am vijaya Lakshmi. I’ve a learnt python in algorithm institute from scratch level being a well approached trainer. He teaches very patiently each and every topic with theory and practial. He is outstanding knowledge in programming. So that now Iam very confidently writing the programmes and executing as well.

Regards,
Vijaya lakshmi

Nagireddy Vijji google review

I am mounika belong to Narasaraopeta institute of techonology in cse department and in alogorithm class I have attended for Python class and it was great experience to learn. good more practical work than theory which all programmers required and I am so much confident in programming knowledge and coding ability is improved so i’m so much thankful to this institute and hope so u the people also like it …

saimounika maram google review

i Dayakar vaddempudi from rvr&jc college of engineering . i took python classes from algorithm institute .this man got the stuff and he knows how to teach programming language in a practical way.writing more programs are really matters than just sitting at looking at white board.here u write programs through out the class, end of the day makes u feel like more confident in programming.

Dayakar Vaddempudi google review

I am Purnima. I attended the Python training in Algorithm training class. Previously i had no knowledge of Python. The institute gives us an fair idea on practically working on python programs with a variety of scenarios. It has highly increased my logical thinking and made me quite thorough with the Python syntax.
The course material provided is very helpful in making concepts strong.