Friday, December 19, 2014

Project 1 - Moving the Sprite

The rect (instance) of the sprite is now able to move across the screen - this was done in the main loop - just like checking whether we had closed the window (discussed in post 1) in a for loop - we did the same thing, but instead looked for the keydown event.


for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.gameloop = False
               
                   
                if (event.type == pygame.KEYDOWN):
                    if (event.key==pygame.K_RIGHT):
                        self.movex= 5
                        self.player.x += self.movex
                    elif (event.key==pygame.K_LEFT):
                        self.movex = -5
                        self.player.x += self.movex
                       

                if (event.type == pygame.KEYUP):
                    if (event.key==pygame.K_RIGHT):
                        self.movex= 0
                        self.player.x += self.movex
                    elif (event.key==pygame.K_LEFT):
                        self.movex= 0
                        self.player.x += self.movex


Though - this code could be more efficient - that being the fact that instead of being in the mainloop - it should be in a method in the player class - hence enforcing data independence. The next thing that must be worked upon is the gravity, at the moment, the 'sprite' is just stuck up the top of the screen, with the user able to move him back and forwards.

Wednesday, December 17, 2014

Project 1 - Rendering - Player Class & Sprite Sheet

I thought it suitable to start the Player Class and was able to render a rectangle onto the screen. This was done relatively easy - when initializing the player class, I needed to include an x & y coordinate as well as an image, gravity and velocity. Two methods were also created, an update method and a render method, each called by the main-loop. The render method was relatively simple -

pygame.draw.rect(window, (150, 150, 150), (self.x, self.y, self.width, self.height))

This basically draws a rectangle on the screen. Note - the sprite has not been rendered yet.


The sprite I'm using is from spriters resource - an online archive of a bunch of sprites and maps etc. I didn't want to spend too long choosing one, so I just picked above. To select a sprite from this sheet, the most suitable way is to use the set_clip and get_clip functions of Pygame as well as some simple mathematics. 


Monday, December 15, 2014

Project 1 - Pygame Platformer & Introduction

Coming out of CSSE1001 at the University of Queensland, equipped with the knowledge of Python, I wanted to try and implement in a game. My idea being a platformer. Jumping straight into development, I'm using a python module called Pygame.

http://www.pygame.org/news.html
http://www.reddit.com/r/pygame

I found numerous tutorials and felt that a lot of them covered bad programming habits - not using classes and using numerous global variables. Though I found that with a little tinkering I was able to figure out an approach to using the module.

I've created this blog to not only track my progress in this project, but for numerous other projects that I'll be creating and adding to throughout my IT career.

Not really knowing where to start with the project, I decided it to be a good idea to create the game loop. For this, I created an app class, and upon initialization, will run the main game loop. The game loop is a simple while statement, and when the application is closed - or when the event pygame.QUIT is caught, then the gameloop runs false and terminates.

It now comes down to me needing to figure out the game mechanics and overall planning of classes. In the past with projects like this, I've usually just jumped into them headfirst, but I want to take the time to plan and create a game that will be the best it can be. 

Stay tuned for more updates!