Monday, April 20, 2015

Player movement and a second video!

Now it is time for me to expand the game a little further than a menu screen. In the following video, I use object-oriented programming to create a player object, which appears as a simplistic blue space ship on the screen, and I write event handles to allow the player to move the ship with the arrow keys. (Note: I recommend that you watch the video in full-screen.)

Edit: I realize that you may be interested in seeing the code separately:


import os, pygame, sys
from pygame.locals import *

if not pygame.font: print('Warning, fonts disabled')
if not pygame.mixer: print('Warning, sound disabled')

UP = 0
RIGHT = 1
DOWN = 2
LEFT = 3

MENU_SCREEN = 0
GAME_SCREEN = 1
LOAD_SCREEN = 2

class GameObject:
    def __init__(self, image, size, speed, x, y, rot):
        self.image = image
        self.size = size
        self.speed = speed
        self.pos = image.get_rect().move(x,y)
        self.rot = rot
        
    def move(self,surface,direction):
        if direction == UP:
            self.pos = self.pos.move(0,-1)
        if direction == RIGHT:
            self.pos = self.pos.move(1,0)
        if direction == DOWN:
            self.pos = self.pos.move(0,1)
        if direction == LEFT:
            self.pos = self.pos.move(-1,0)
        if self.pos.right > surface.get_size()[0]:
            self.pos.left = 0
        if self.pos.top < 0:
            self.pos.bottom = surface.get_size()[1]

#Initialize screen
pygame.init()
DISPLAY_SURFACE = pygame.display.set_mode((960, 540),RESIZABLE)
pygame.display.set_caption('Space Shooter!') #I'll think of a more creative name later.

screen_indicator = MENU_SCREEN

size = DISPLAY_SURFACE.get_size()
width = size[0]
height = size[1]

title = pygame.Rect(((width/16)+1, (height/8)+1, 7*width/8, height/4))
menu1 = pygame.Rect(((width/4)+1, (height/2)+1, 2*width/4, height/10))
menu2 = pygame.Rect(((width/4)+1, (height/2)+1+(3*height/20), 2*width/4, height/10))
menu3 = pygame.Rect(((width/4)+1, (height/2)+1+(6*height/20), 2*width/4, height/10))

#Draws dark blue rectangles.
pygame.draw.rect(DISPLAY_SURFACE, (0,0,150), menu1)
pygame.draw.rect(DISPLAY_SURFACE, (0,0,150), menu2)
pygame.draw.rect(DISPLAY_SURFACE, (0,0,150), menu3)

#Draws blue ovals on top of the rectangles.
pygame.draw.ellipse(DISPLAY_SURFACE, (0,0,150), title)
pygame.draw.ellipse(DISPLAY_SURFACE, (0,0,255), menu1)
pygame.draw.ellipse(DISPLAY_SURFACE, (0,0,255), menu2)
pygame.draw.ellipse(DISPLAY_SURFACE, (0,0,255), menu3)

#pygame.font.Font takes in a font name and an integer for its size.
#Free Sans Bold comes with Pygame (Sweigart 2012 p. 30).
font_title = pygame.font.Font('freesansbold.ttf', 64)
font_menu = pygame.font.Font('freesansbold.ttf', 32)

#Creates and draws the text.
surface_title = font_title.render('SPACE SHOOTER!', True, (0,255,0))
rect_title = surface_title.get_rect(center=(width/2,(height/4)+3)) #get_rect() is my favorite Pygame function.
surface_new_game = font_menu.render('NEW GAME', True, (0,0,0))
rect_new_game = surface_new_game.get_rect(center=(width/2,(height/2)+(height/20)+3))
surface_load_game = font_menu.render('LOAD GAME', True, (0,0,0))
rect_load_game = surface_load_game.get_rect(center=(width/2,(height/2)+(4*height/20)+3))
surface_exit = font_menu.render('EXIT', True, (0,0,0))
rect_exit = surface_exit.get_rect(center=(width/2,(height/2)+(7*height/20)+3))

background = pygame.image.load('background.png').convert() #http://www.pygame.org/docs/tut/MoveIt.html
player = pygame.image.load('player.png').convert_alpha() #player.png is in the same folder as this module, which is C:\Python32 for me.
p = GameObject(player, 100, 1, 400, 400, 1)

while True: #main game loop
    for event in pygame.event.get():
        if screen_indicator == MENU_SCREEN:
            DISPLAY_SURFACE.blit(surface_title, rect_title)
            DISPLAY_SURFACE.blit(surface_new_game, rect_new_game)
            DISPLAY_SURFACE.blit(surface_load_game, rect_load_game)
            DISPLAY_SURFACE.blit(surface_exit, rect_exit)
            if event.type == VIDEORESIZE:
                #This line creates a new display in order to clear the screen.
                #I figured this out myself, so if you know of a better way to do this, let me know.
                DISPLAY_SURFACE = pygame.display.set_mode((event.w, event.h),RESIZABLE)

                width = event.w
                height = event.h

                #Creates the rectangle objects that will be behind the title and menu buttons.
                title = pygame.Rect(((width/16)+1, (height/8)+1, 7*width/8, height/4))
                menu1 = pygame.Rect(((width/4)+1, (height/2)+1, 2*width/4, height/10))
                menu2 = pygame.Rect(((width/4)+1, (height/2)+1+(3*height/20), 2*width/4, height/10))
                menu3 = pygame.Rect(((width/4)+1, (height/2)+1+(6*height/20), 2*width/4, height/10))

                #Draws dark blue rectangles.
                pygame.draw.rect(DISPLAY_SURFACE, (0,0,150), menu1)
                pygame.draw.rect(DISPLAY_SURFACE, (0,0,150), menu2)
                pygame.draw.rect(DISPLAY_SURFACE, (0,0,150), menu3)

                #Draws blue ovals on top of the rectangles.
                pygame.draw.ellipse(DISPLAY_SURFACE, (0,0,150), title)
                pygame.draw.ellipse(DISPLAY_SURFACE, (0,0,255), menu1)
                pygame.draw.ellipse(DISPLAY_SURFACE, (0,0,255), menu2)
                pygame.draw.ellipse(DISPLAY_SURFACE, (0,0,255), menu3)

                #Creates and draws the text.
                rect_title = surface_title.get_rect(center=(width/2,(height/4)+3)) #get_rect() is my favorite Pygame function.
                rect_new_game = surface_new_game.get_rect(center=(width/2,(height/2)+(height/20)+3))
                rect_load_game = surface_load_game.get_rect(center=(width/2,(height/2)+(4*height/20)+3))
                rect_exit = surface_exit.get_rect(center=(width/2,(height/2)+(7*height/20)+3))

            #When you let go of the left mouse button in the area of a button, the button does something.
            if event.type == MOUSEBUTTONUP:
                if event.button == 1:
                    if (menu1.left < event.pos[0] < menu1.right) and (menu1.top < event.pos[1] < menu1.bottom):
                        screen_indicator = GAME_SCREEN
                        DISPLAY_SURFACE = pygame.display.set_mode((width, height),RESIZABLE)
                    if (menu2.left < event.pos[0] < menu2.right) and (menu2.top < event.pos[1] < menu2.bottom):
                        print("LOAD GAME") #Placeholder
                    if (menu3.left < event.pos[0] < menu3.right) and (menu3.top < event.pos[1] < menu3.bottom):
                        pygame.event.post(pygame.event.Event(QUIT)) #Exits the game

            #When you mouse-over a button, the text turns green.
            if event.type == MOUSEMOTION:
                if (menu1.left < event.pos[0] < menu1.right) and (menu1.top < event.pos[1] < menu1.bottom):
                    surface_new_game = font_menu.render('NEW GAME', True, (0,255,0))
                else:
                    surface_new_game = font_menu.render('NEW GAME', True, (0,0,0))
                if (menu2.left < event.pos[0] < menu2.right) and (menu2.top < event.pos[1] < menu2.bottom):
                    surface_load_game = font_menu.render('LOAD GAME', True, (0,255,0))
                else:
                    surface_load_game = font_menu.render('LOAD GAME', True, (0,0,0))
                if (menu3.left < event.pos[0] < menu3.right) and (menu3.top < event.pos[1] < menu3.bottom):
                    surface_exit = font_menu.render('EXIT', True, (0,255,0))
                else:
                    surface_exit = font_menu.render('EXIT', True, (0,0,0))           
        if screen_indicator == GAME_SCREEN:
            DISPLAY_SURFACE.blit(background, (0, 0))
            #For now, the controls are built-in.  Later controls will be changeable via a settings menu.
            if event.type == KEYDOWN:
                while pygame.event.peek(KEYUP) == False:
                    print(event.key)
                    if event.key == 273:
                        DISPLAY_SURFACE.blit(background, (0, 0))
                        p.move(DISPLAY_SURFACE,UP)
                        DISPLAY_SURFACE.blit(p.image, p.pos)
                        pygame.display.update()
                        pygame.time.delay(2)
                    if event.key == 275:
                        DISPLAY_SURFACE.blit(background, (0, 0))
                        p.move(DISPLAY_SURFACE,RIGHT)
                        DISPLAY_SURFACE.blit(p.image, p.pos)
                        pygame.display.update()
                        pygame.time.delay(2)
                    if event.key == 274:
                        DISPLAY_SURFACE.blit(background, (0, 0))
                        p.move(DISPLAY_SURFACE,DOWN)
                        DISPLAY_SURFACE.blit(p.image, p.pos)
                        pygame.display.update()
                        pygame.time.delay(2)
                    if event.key == 276:
                        DISPLAY_SURFACE.blit(background, (0, 0))
                        p.move(DISPLAY_SURFACE,LEFT)
                        DISPLAY_SURFACE.blit(p.image, p.pos)
                        pygame.display.update()
                        pygame.time.delay(2)
            DISPLAY_SURFACE.blit(p.image, p.pos)
            if event.type == VIDEORESIZE:
                DISPLAY_SURFACE = pygame.display.set_mode((event.w, event.h),RESIZABLE)
                DISPLAY_SURFACE.blit(background, (0, 0))

        if event.type == QUIT:
            pygame.quit()
            sys.exit()            
        pygame.display.update()

Sunday, April 19, 2015

Interview with Ahmet Sezgin Duran!

Sharon: Hello, and thank you for agreeing for this interview.  I'm Sharon Lougheed, and I'm a student at UTD.  Could you tell us a little bit about yourself?

Ahmet: Hello. My name is Ahmet Sezgin Duran and I'm a B.Sc student at Ankara University Computer Engineering. I'm a GNU/Linux user (also free software supporter) for 7 years and for the past 3.5 years I'm developing softwares with free software (e.g. open source) technologies. For the last 1.5 years, I worked in lab2023 - information technologies as a web/system developer, using open source technologies such as Ruby (Rails), Python and GNU/Linux. Currently I'm interested in artificial intelligence and working in a government supported project which is related to swarm intelligence.

Sharon: How and when did you first learn Python?

Ahmet: Back in 2012, before starting B.Sc education, I studied English for 1 year in Ankara University's prep school. Funny thing is that I actually failed in prep school passing exam accidentally, didn't see the last page and failed at border. This situation gave me an great opportunity: Lots of free time.

I was going to study computer engineering after prep school and I asked myself that why I'm not learning a programming language. Then I looked for programming language books. I looked for Python books because, I always encountered Python in GNU/Linux distros. I found http://www.istihza.com website, a person named Fırat Özgül was (still is) writing down Python 2.x and Python 3.x tutorials as a book, also free. I downloaded Python 2.x book and studied it. Also I studied several books too: "Dive Into Python" and "Invent Your Own Computer Games With Python". Those 3 books are still my favorites for Python.

Sharon: I understand you've worked on a project using Pygame.  Could you tell us a bit about it, and how would you describe the experience?

Ahmet: My project was a photo booth machine, programmed in Python and game dynamics. It included coin machine programming, DSLR machine controlling, system management, image processing and printer controlling systems, all of them were written in Python by myself. Application's general structure was programmed as a game with Pygame.

The experience was entertaining for quite reasons: I was the only programmer, all parts of the application was coded in my favorite programming language which is Python and Pygame were (still is) quite primitive for developing a game or an application with game mechanics. The last part led me to create lots of abstraction tools such as easy font rendering, position centerizer for drawable elements, state machine manager, application manager for smooth application initialization and application exit, etc. I can say that if I used a high level tool such as Kivy, I wouldn't gain this much experience.

Now the annoying part of the experience: compatibility issues. I had a lot of trouble with Pygame's audio and font rendering system. Mixer library in Pygame were making problems with audio's that has different frequency attributes. I tried finding proper frequency settings for 3 days and found it just by luck. In the mean time, SFML library, Python SFML too, finds proper settings automatically and it doesn't make any problem with frequency (if audio file is not corrupted). Other serious problem: Smooth font rendering. Pygame can render fonts smoothly but it didn't with custom fonts designed by my project's graphics designer. I had to use a standart font and that created a lot of trouble between company and client. Yet, the application is currently in production without a problem for 9 months in a shopping mall, in Denizli, Turkey.

Sharon: What advice do you have for those who are new to Pygame or game development in general?

Ahmet: I've several advices for the people new to Pygame or game development. First of, I need to say that Pygame is actually not a game library. It doesn't provide any game mechanic tool. It's an actually a multimedia library, which SFML says is properly (Simple Fast Multimedia Library)

Transformation from a multimedia library to game library is quite challenging. You will need to implement application manager, entity system, AI elements, state machine manager and so on. Without understanding this topics, you will have lots of troubles while creating a game. My first advice is to learn mechanics and there's an awesome website for learning them:

http://gameprogrammingpatterns.com/

After learning mechanics, it's best to use a multimedia library and create abstract tools for easy programming. SFML is quite better for first time learners, it's mostly based on OOP style. On the other hand, Pygame is relatively harder than SFML, but the idea behind them are quite similar. My second advice is to transform a multimedia library to a game engine, with previous knowledge of mechanics.

The last advice of mine is that: Don't be perfectionist. Don't care if your game looks terrible, just develop it, test it, play it and start another. Cloning well known, popular games (e.g. Mario, Legend of Zelda, etc.) is a good way to practice.

Sharon: Thank you very much for your interview!

Edit: Ahmet sent me this afterwards, in case you are interested:
Also if you need to learn game mechanics by example, check out this page too:
http://gamemechanicexplorer.com/

Interview with Final Parsec!

Sharon: Hello everyone!  My name is Sharon Lougheed, and I'm a student at The University of Texas at Dallas.  And I'm interviewing an awesome team of game developers from a company called Final Parsec.  Would you guys like to introduce yourselves?

TJ: Sure!  My name is TJ Brosnan.

Matt: And I'm Matt Bauer.

Baer: And my name is Baer Bradford.

Sharon: Okay, so what do you guys do at your team?

TJ: We try and make games on our spare time.  It started out as sort of a tool to start learning a new software language called Python.  And from there, we just kind of took on it because we had so much fun to start trying to do new games.

Sharon: Are you guys all programmers?

Baer: Yes, we're all programmers and developers.  We all went to TCU and majored in computer science.  And that's actually been kind of one of the downfalls of our triumvirate I think is that we lack really badly for art and audio kind of work.  But we definitely manage the programming side very well.

Sharon: So how and when did you first learn Python?

Baer: Well, for me, I actually started learning Python for my job.  I was working at a company and we were using it to build websites with the framework called Flask.  And we dug pretty deep into it, but not quite in the same way when I met with Matt and TJ and started working on Space Snakes.  How did you guys-- When did you get into Python?  This was the first time you guys started working with it, right?

TJ: Yeah, I also got into it for a job.  I actually wanted to try and make a game with Python in order to use it as a tool to learn it so I could do some scripting for my internship at one of the companies I was working for at TCU.

Matt: And TJ recruited me shortly after deciding to make a game, and I just wanted to do that.  So that's all-- that was the only reason.  I had no reason to do it for a job.

TJ: Yeah, and then Matt kind of took it away and spent hours and hours doing it.

Matt: Yeah.  I stayed up all night a few nights.

Sharon: So I understand you were using Pygame when you were working on Space Snakes.  Could you tell us a bit about it, and how would you describe the experience?

TJ: I think Matt has a lot to say about Pygame.

Matt: Yeah... So Pygame it was a lot of fun developing.  It really gets very close to the OpenGL type of programming without actually having to do OpenGL.  And you don't get a lot of the hand-holding that other game engines like Unreal and Unity 3D do for you.  But at the same time, you have to actually have to program all those things yourself.  So you're spending a lot of time implementing things that have been done thousands of times by other people.  So I really liked it as a learning tool, and it was great to get into.  But after we actually made the switch over to Unity 3D, we just found all these things that we had actually programmed ourselves were just done automatically for us.  So that was pretty nice, and it was a lot easier than chugging through Pygame.

Sharon: So I'm going to guess that's why you had difficulty finishing the game?

Matt: Yeah.  So we got very far.  We did a whole lot of work, and we finally started to do the multiplayer/networking aspect of our game, and we just realized that it was going to take us probably another year to get anywhere close to what we wanted and what we were going to expect from it.  And that's about whenever I started looking for other engines and saw-- just ran across this tutorial that basically did what we wanted to do in about 10 hours.  And it's like, well... we could do this for another months, or we could learn Unity 3D and could probably be done in a couple of weeks, so that when I started making the switch over.

Sharon: For a team that doesn't have art or music people, Space Snakes looks like a pretty cool game.

TJ: Yeah, well we were actually able to recruit my friend for some of the art on Space Snakes.  Actually most of it he was able to put together for us, but he was pretty unreliable, and his work kind of tapered off towards the end of the project.  So in our newer games we had to dip into free and open game art and stuff like that.

Sharon: Ah, I see.  Alright... What advice do you have for those who are new to Pygame or game development in general?

Baer: My advice would actually be to try to start a smaller scale game than something like Space Snakes.  Don't envision that you're going to build a game that has different types of levels and multiplayer and half a dozen different weapons and lobby systems and all kinds of things like that.  It's probably much better for you to try to start with something smaller and then build your way up in subsequent games.

TJ:  Also, when you do start small like that, you start reusing your code over and over.  So, like, you don't have to rebuild your lobby system or your menu system.  You just go and copy that over and change all the art around and apply all those concepts to your new game, and each game kind of builds on itself after that.

Baer: Yeah definitely.  If you're designing your games right, you'll be getting reusable components that you can use elsewhere for a long time in the future.

TJ: Another piece of advice that I would give for Pygame in particular is to look at the Python...  What did you call it Baer?

Baer: It's PEP8.  And PEP stands for... I can't actually remember off the top of my head, but it's pretty much a Python standard or convention and PEP8 refers to how everything is formatted and organized and it really helps the readability of your code, which is a big advantage of writing in Python to begin with.

TJ: It was, yeah.  Baer came into Space Snakes like a couple months after Matt and I started, and soon as he got here we started switching things over to how he did it at his job to PEP8, and things started to click much faster and it looked better too.

Sharon: Oh, my friend mentioned that, and I was wondering should-- is it a good idea to start programming according to PEP8?

Baer: Absolutely.  I would get into the habit as soon as you can because it's not-- If you're working on a single project by yourself, it's not going to be such a big deal, but as soon as you get into a team environment, following conventions is going to be one of the most valuable things you can do.  And having it as second-nature will make it so much easier.

Sharon: Alright.  Oh... I'm going to have to go back in my code and change it!

Baer: If you look around, there are actually development environments that will highlight your code that doesn't follow that standard.  We all personally use PyCharm.  It's not a free piece of software, but it's one of the better development environments we've found.

TJ: I believe they have a free version out now.

Baer: Oh yeah, I think they might have a community edition, and it'll go through and tell you if you're not following the appropriate conventions and tell you exactly what you're doing wrong, which is really convenient.

Sharon: Well, sweet!  That sounds pretty helpful.

TJ: Did you have any advice Matt?

Matt: Mmm, nah.  I think y'all covered all the good ones.

Sharon: Well I think that's all the questions I have for you today, and thank you so much for agreeing to do this interview.

Be sure to check out Final Parsec!
http://finalparsec.com/
https://twitter.com/Final_Parsec
https://www.youtube.com/channel/UCHcxGunEdEPlgq5JulJ2fYQ

External Resources:
https://www.python.org/dev/peps/pep-0008/
http://www.jetbrains.com/pycharm/

A Graphical Summary...

The following infographic is filled with information this blog has covered so far. I hope that you find it helpful.

Guest post: Taylor Burleson

I think I can help you overcome your fear of Python. Now I’m not talking about the slithery hissing kind but the scarier computer language kind. Python.org assures you that “Python can be easy to pick up whether you're a first time programmer or you're experienced with other languages.” In my introduction I will discuss everything from downloading, non-programmers, and programming regulars in the efforts of learning Python.

Below is a link to the official site’s step by step guide to installing Python for any of the individuals that are first timers or have trust issues with downloading anything from websites you aren’t familiar with. It covers step by step instructions for both PC and Mac users so don’t fret.

Beginner's Guide, Download & Installation

Next, I have a Link to a guide for helping any of the non-programmers looking at learning. If the phrase “for loop” or “if/else statement” don’t ring any bells this guide is the one for you. With time you will be prepared for the programmers guide but until then I recommend sticking with the non-programmers guide.

Beginner's Guide, Non-Programmers

Finally, for those that are very familiar with programing, below is the link to a guide for teaching Python on a more advanced pace. Fair warning though if you haven’t programmed in a while you might want to start off with the Non-Programmers guide above just to get re acquainted to it all again, there may be some concepts or ideas in the programming guide that you may have difficulty jumping back into. No one will think less of you for using it, promise!

Beginner's Guide, Programmers

Thank you for reading, my name is Taylor Burleson and I normally blog about scripting within Unity 3D but Sharon was kind enough to allow me to be her guest blogger for this week.


Python(n.d.) Retrieved April 19,  2015, from https://www.python.org/about/

Friday, April 17, 2015

A Video Review

If you have had any difficulty keeping up, this video review should help! In the video, I explain every line of code in my program.

(Note: I recommend that you watch the video in full-screen.)

Friday, March 20, 2015

Pygame basics

It's now time to start working on the space shooter game, specifically on the menu screen.

The menu screen of "Space Shooter!"  When you mouse-over a button, the text turns green.  The "NEW GAME" and "LOAD GAME" buttons print to the console, and "EXIT" quits the game.
First we need to import the needed modules.  The “os” and “sys” Python modules “allow us to do things like create platform independent file paths” (Shinners 2004).  The “pygame” module lets us use functions “dealing with graphics, sound, and other features that Pygame provides” (Sweigart 2012 p. 9). After we import the pygame module, we need to call pygame.init() “before calling any other Pygame function.”  The function pygame.display.set_mode((800, 600)) sets the window to 800 x 600, makes it resizable, and returns a pygame.Suface object, which is stored in DISPLAYSURF. Then pygame.display.set_caption('Space Shooter!') sets the title for the window.

Before drawing anything, I saw a black window with “Space Shooter!” in the title bar.  The functions print() and input() won’t work with this window because they are for command line interface programs (Sweigart 2012 p. 8), so you have to handle input from the mouse and keyboard through events.


According to Sweigart, the while loop in the second half of the program (labeled as the “main game loop”) does three things in a game:

1. Handles events.

2. Updates the game state.

3. Draws the game state to the screen.

The game state refers to the values of all of the variables at any moment.  This code handles four events: The resizing of the screen, the release of a mouse button, the movement of the mouse, and quitting.  Resizing the screen changes the size of the menu and title accordingly and redraws the screen.  Releasing the left mouse button over the "new game" and "load game" buttons prints "NEW GAME" and "LOAD GAME" respectively in the console.  Doing so over the quit button sets the event to.  Moving a mouse over a button temporarily changes the text to green, and finally, quit closes Pygame and exits.


import os, pygame, sys
from pygame.locals import *

if not pygame.font: print('Warning, fonts disabled')
if not pygame.mixer: print('Warning, sound disabled')

# Initialize screen
pygame.init()
DISPLAYSURF = pygame.display.set_mode((800, 600),RESIZABLE)
pygame.display.set_caption('Space Shooter!') #I'll think of a more creative name later.

size = DISPLAYSURF.get_size()
width = size[0]
height = size[1]

title = pygame.Rect(((width/16)+1, (height/8)+1, 7*width/8, height/4))
menu1 = pygame.Rect(((width/4)+1, (height/2)+1, 2*width/4, height/10))
menu2 = pygame.Rect(((width/4)+1, (height/2)+1+(3*height/20), 2*width/4, height/10))
menu3 = pygame.Rect(((width/4)+1, (height/2)+1+(6*height/20), 2*width/4, height/10))

#Draws dark blue rectangles.
pygame.draw.rect(DISPLAYSURF, (0,0,150), menu1)
pygame.draw.rect(DISPLAYSURF, (0,0,150), menu2)
pygame.draw.rect(DISPLAYSURF, (0,0,150), menu3)

#Draws blue ovals on top of the rectangles.
pygame.draw.ellipse(DISPLAYSURF, (0,0,150), title)
pygame.draw.ellipse(DISPLAYSURF, (0,0,255), menu1)
pygame.draw.ellipse(DISPLAYSURF, (0,0,255), menu2)
pygame.draw.ellipse(DISPLAYSURF, (0,0,255), menu3)

#pygame.font.Font takes in a font name and an integer for its size.
#Free Sans Bold comes with Pygame (Sweigart 2012 p. 30).
font_title = pygame.font.Font('freesansbold.ttf', 64)
font_menu = pygame.font.Font('freesansbold.ttf', 32)

#Creates and draws the text.
surface_title = font_title.render('SPACE SHOOTER!', True, (0,255,0))
rect_title = surface_title.get_rect() #get_rect() is my favorite Pygame function.
rect_title.center = (width/2,(height/4)+3)
surface_new_game = font_menu.render('NEW GAME', True, (0,0,0))
rect_new_game = surface_new_game.get_rect()
rect_new_game.center = (width/2,(height/2)+(height/20)+3)
surface_load_game = font_menu.render('LOAD GAME', True, (0,0,0))
rect_load_game = surface_load_game.get_rect()
rect_load_game.center = (width/2,(height/2)+(4*height/20)+3)
surface_exit = font_menu.render('EXIT', True, (0,0,0))
rect_exit = surface_exit.get_rect()
rect_exit.center = (width/2,(height/2)+(7*height/20)+3)

while True: #main game loop
    DISPLAYSURF.blit(surface_title, rect_title)
    DISPLAYSURF.blit(surface_new_game, rect_new_game)
    DISPLAYSURF.blit(surface_load_game, rect_load_game)
    DISPLAYSURF.blit(surface_exit, rect_exit)
    for event in pygame.event.get():
        if event.type == VIDEORESIZE:
            #This line creates a new display in order to clear the screen.
            #I figured this out myself, so if you know of a better way to do this, let me know.
            DISPLAYSURF = pygame.display.set_mode((event.w, event.h),RESIZABLE)

            width = event.w
            height = event.h

            #Creates the rectangle objects that will be behind the title and menu buttons.
            title = pygame.Rect(((width/16)+1, (height/8)+1, 7*width/8, height/4))
            menu1 = pygame.Rect(((width/4)+1, (height/2)+1, 2*width/4, height/10))
            menu2 = pygame.Rect(((width/4)+1, (height/2)+1+(3*height/20), 2*width/4, height/10))
            menu3 = pygame.Rect(((width/4)+1, (height/2)+1+(6*height/20), 2*width/4, height/10))

            #Draws dark blue rectangles.
            pygame.draw.rect(DISPLAYSURF, (0,0,150), menu1)
            pygame.draw.rect(DISPLAYSURF, (0,0,150), menu2)
            pygame.draw.rect(DISPLAYSURF, (0,0,150), menu3)

            #Draws blue ovals on top of the rectangles.
            pygame.draw.ellipse(DISPLAYSURF, (0,0,150), title)
            pygame.draw.ellipse(DISPLAYSURF, (0,0,255), menu1)
            pygame.draw.ellipse(DISPLAYSURF, (0,0,255), menu2)
            pygame.draw.ellipse(DISPLAYSURF, (0,0,255), menu3)

            #Creates and draws the text.
            rect_title = surface_title.get_rect()
            rect_title.center = (width/2,(height/4)+3)
            rect_new_game = surface_new_game.get_rect()
            rect_new_game.center = (width/2,(height/2)+(height/20)+3)
            rect_load_game = surface_load_game.get_rect()
            rect_load_game.center = (width/2,(height/2)+(4*height/20)+3)
            rect_exit = surface_exit.get_rect()
            rect_exit.center = (width/2,(height/2)+(7*height/20)+3)
    
            pygame.display.update() #Necessary to update the screen

        #When you let go of the left mouse button in the area of a button, the button does something.
        if event.type == MOUSEBUTTONUP:
            if event.button == 1:
                if (menu1.left < event.pos[0] < menu1.right) and (menu1.top < event.pos[1] < menu1.bottom):
                    print("NEW GAME") #Placeholder
                if (menu2.left < event.pos[0] < menu2.right) and (menu2.top < event.pos[1] < menu2.bottom):
                    print("LOAD GAME") #Placeholder
                if (menu3.left < event.pos[0] < menu3.right) and (menu3.top < event.pos[1] < menu3.bottom):
                    pygame.event.post(pygame.event.Event(QUIT)) #Exits the game

        #When you mouse-over a button, the text turns green.
        if event.type == MOUSEMOTION:
            if (menu1.left < event.pos[0] < menu1.right) and (menu1.top < event.pos[1] < menu1.bottom):
                surface_new_game = font_menu.render('NEW GAME', True, (0,255,0))
            else:
                surface_new_game = font_menu.render('NEW GAME', True, (0,0,0))
            if (menu2.left < event.pos[0] < menu2.right) and (menu2.top < event.pos[1] < menu2.bottom):
                surface_load_game = font_menu.render('LOAD GAME', True, (0,255,0))
            else:
                surface_load_game = font_menu.render('LOAD GAME', True, (0,0,0))
            if (menu3.left < event.pos[0] < menu3.right) and (menu3.top < event.pos[1] < menu3.bottom):
                surface_exit = font_menu.render('EXIT', True, (0,255,0))
            else:
                surface_exit = font_menu.render('EXIT', True, (0,0,0))           

        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    pygame.display.update()



Shinners, P. (2004, June 17). Line by line chimp. Retrieved from http://www.pygame.org/docs/tut/chimp/ChimpLineByLine.html

Sweigart, A. (2015). Invent your own computer games with Python. Retrieved from http://inventwithpython.com/inventwithpython_3rd.pdf


Sweigart, A. (2012). Making games with Python & Pygame. Retrieved from http://inventwithpython.com/makinggames.pdf