Help starting a top-down action game

  • (7 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

99 Replies - 8228 Views - Last Post: 08 November 2011 - 10:04 PM Rate Topic: -----

#1 poker158149  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 70
  • Joined: 29-June 11

Help starting a top-down action game

Posted 22 October 2011 - 08:24 PM

Hey there, everyone.

My friend and I are looking into writing a very simple game in Python using PyGame.

It's a top-down view 2D game where you play as a guy with an ax who kills zombies. There are no different levels; it's all on one level where the zombies continuously come until you die. It'll either score the player based on how many zombies he killed or how long he lasted, we don't know yet; but that's irrelevant for now.

What I needed to know is if anyone knew of any good places for me to start, or any good tutorials for top-down games. I've looked on the PyGame site, but I haven't found anything good. I tried looking at the source of other top-down games from the PyGame site, but they don't have any that are close to what I'm trying to do.

So far I have a simple script that allows me to control a sprite with the keyboard. That's as far as I've gotten. I need help getting zombies to continuously come in and attack the player, and getting the player's character to attack and deal damage to the zombies. Those are my two big focus points right now.

I'm just not sure where to start. We're not worried about the graphics or sounds right now; all we're trying to do is get the skeleton and core engine down.

If anyone has any advice or previous experience with a project like this, all information is welcome and greatly appreciated!

Is This A Good Question/Topic? 1
  • +

Replies To: Help starting a top-down action game

#2 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Help starting a top-down action game

Posted 22 October 2011 - 09:42 PM

Are you familiar with how to program, or do you need to learn as you go?

I have worked with pygame before, quite extensively.

Before you start a project like this, you need to know where it's headed - and what the functionality will be. I think you should decide on the important parts (like the scoring) before you start, you can always change them later if it's not working. You can't code something that you don't deem to be relevant to your project.

First off, I'd start with writing (or typing) down some "if then else" statements just in English, that would provide most of the functionality in a game like this. Then you can begin to evolve them statements, I'll do a few examples:

Take 1:
If the player has more than 0 health, and a zombie touches him, he loses health


Take 2:
if (zombie and player are in the same place) and player is alive:
     decrease the player's health


Take 3:
     if (abs(zombie.x - player.x) > 5 and player.health > 0):
       player.health -= 1


Even if you don't know how to implement it at first, you can always write it in plain English as a comment, and deal with it later. The pieces don't have to match up to begin with but they'll begin to merge into a full game as you develop them.

Once the core details of play have been sorted, you can move on to level design, scoreboards, sound etc.

This Python forum is more for troubleshooting pygame programs rather than game development advice, we do also have a Game Programming forum here you might want to visit. Of course, we'll give you our suggestions here too though :)

This post has been edited by Simown: 22 October 2011 - 09:47 PM

Was This Post Helpful? 1
  • +
  • -

#3 poker158149  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 70
  • Joined: 29-June 11

Re: Help starting a top-down action game

Posted 22 October 2011 - 10:44 PM

I'm pretty familiar with Python, but I've never programmed a game using it before, so I'm going to be learning as I go.
Also, I apologize for posting in the wrong forum; I must not have seen the Game Programming forum.

Anyway, that's really good advice, and it's something I'll definitely have to do.
We kind of have how we want it to work planned out, considering it's very simple and there's not much too it; I'm just having trouble executing it through code because I've never done a project like this before, so I'm unfamiliar with the syntax of game programming.

I've come across the source of a few examples, but it seems like code for games are less reusable than code for application. It seems more... local, if you know what I mean. Customized for that particular game. And because of that, it's difficult to learn from someone else's code.
Was This Post Helpful? 0
  • +
  • -

#4 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Help starting a top-down action game

Posted 23 October 2011 - 06:37 AM

It's not really the wrong forum, it sort of falls in between Game Programming and Python, here's fine.

I'd recommend looking at other people's code to understand how it works, but once you get down to it, write all the code yourself - I imagine you want to learn how pygame works in the process.

If you've not seen them, these are good links to get started:
http://inventwithpython.com/chapters/
http://cs.simpson.ed...pygame_examples
Was This Post Helpful? 1
  • +
  • -

#5 poker158149  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 70
  • Joined: 29-June 11

Re: Help starting a top-down action game

Posted 23 October 2011 - 02:17 PM

The Simpson link I've actually looked at before and got some really good examples from. The first link I have not seen, and will definitely have to look at.

Thanks for your information so far, it'll really come in handy. And yes, I do want to learn PyGame, so I'll be using all of my own code. I'll just have to look at the code of others to get an idea of how to do certain parts.
Was This Post Helpful? 0
  • +
  • -

#6 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Help starting a top-down action game

Posted 23 October 2011 - 02:19 PM

Yeah, that's a good plan. I wish you the best of luck! Come back if you have any more questions (especially regarding pygame) - and remember to show us when you've finished :)
Was This Post Helpful? 1
  • +
  • -

#7 poker158149  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 70
  • Joined: 29-June 11

Re: Help starting a top-down action game

Posted 23 October 2011 - 11:15 PM

Simown, I have a question for you.

How exactly do I make a main menu for my game?

I've found one here (http://pygame.org/pr...menu-1429-.html) that I'd like to try and implement into my game, but I have absolutely no idea how. I've tried importing it into my own script and running it, but it's always a different error. And sometimes it'll work, but then when I try and run it again, it won't work with a new error.

Even if I can't get this to work, how can I just make a simple menu before the game? I don't expect you to do it for me and code it out, but any examples or tutorials you know of would greatly help me out.
Was This Post Helpful? 0
  • +
  • -

#8 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Help starting a top-down action game

Posted 23 October 2011 - 11:59 PM

I am unsure of the functionality of that menu, I don't have the means to test it now - but I'd advocate you writing your own anyway.

Are you familiar with drawing pygame objects and text on the screen yet? That's basically all you need to know, and then the same functionality you'd need to display a console text menu:

First you import pygame, set up the background, screen size and then go into a main loop, like the majority of pygame programs, you could just use simple variables to depict the state in this case, if state == "menu", display the menu. If state == "level1" etc. or use booleans, or even functions as the states. Enclose that all in an infinite loop, and you have your game with menu.

Your game will have this sort of structure:

import pygame

Set the background
Set the screensize

#Infinite loop
while True:
    Handle pygame events such as mousedown events, quitting
    display the menu
    if a menu option is clicked (determined by "collisions" with the button):
          goToThatOption()

    def goToThatOption():
       stay in this state, possibly with a while loop again
       break out to the menu



Admittedly it would be better to use classes and objects to represent the state of things, but I think this way is better to begin with - some may disagree.

Please stop me if I'm going too fast, I am not sure how much pygame you have learnt up to this point. Also it's 8am, so forgive me if I've missed something obvious :)

EDIT: I'll have a look and test out that menu link from the pygame site this evening when I get home.

This post has been edited by Simown: 24 October 2011 - 12:33 AM

Was This Post Helpful? 1
  • +
  • -

#9 poker158149  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 70
  • Joined: 29-June 11

Re: Help starting a top-down action game

Posted 24 October 2011 - 08:02 AM

Haha, no, you're going at the perfect speed for me. And I also can't see anything that would have been left out.
Thank you for keeping up with me. Not many people would.

Anyway, that makes much more sense than anything else I've seen.
Just one question. When you defined goToThatOption(), was it defined inside the infinite while loop? Because I notice you broke from the loop even though thur function was inside the loop

Note: its early for me too, so I apologize if this is obvious and I missed it :P

This post has been edited by poker158149: 24 October 2011 - 08:04 AM

Was This Post Helpful? 0
  • +
  • -

#10 poker158149  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 70
  • Joined: 29-June 11

Re: Help starting a top-down action game

Posted 24 October 2011 - 01:25 PM

I apologize for double-posting, but I can't edit my previous post anymore.

I've been playing around with it and got my text to print out to the screen. The part I'm stuck at now is actually testing collision with the text and what to do after collision is detected. I know that using the keyboard and using the mouse are two different setups, so I assume they'd be done differently. If possible, I'd like to be able to use the mouse and keyboard to select an option, but if it'll be easier, using just the keyboard would be just as fine.

This is the code I have spitting my text out (note: any variables in here were defined before):
f1 = font.Font('321impact.ttf',35)
main1 = f1.render("Main Menu", 1, grey)
r1 = main1.get_rect()
r1.centerx,r1.top = 390,100
screen.blit(main1,r1)

f2 = font.Font('321impact.ttf',30)
main2 = f2.render("Play", 1, grey)
r2 = main2.get_rect()
r2.centerx,r2.top = 390,150
screen.blit(main2,r2)

f3 = font.Font('321impact.ttf',30)
main3 = f3.render("Quit", 1, grey)
r3 = main3.get_rect()
r3.centerx,r3.top = 390,200
screen.blit(main3,r3)



These spit out successfully to the screen and show how I want them to. I'm just lost on what to do to make them functional.

Also, on a side note, I don't know why, but every time I run my script to test out the text, Python freezes. I see my window, then it "stops responding." I don't know if I have something improperly setup, but it's pretty annoying and will be unacceptable for the final project.
Was This Post Helpful? 0
  • +
  • -

#11 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Help starting a top-down action game

Posted 24 October 2011 - 02:29 PM

The freezing could be a number of reasons, one of them is you don't seen to have any code to quit pygame? Did you initialise pygame with pygame.init()? You want all your logic in the place where pygame handles events:

for event in pygame.event.get():
    if event.type == QUIT:
        pygame.exit()



Yeah the code for TheOtherOption() is inside, not 100% sure what I was thinking :P Ok, rethink, we do the state switching with state variables as we discussed.

state = "menu"

while True:
  if event.type == QUIT:
      pygame.exit()
   ...
  if state == "menu":
       menu()
  if state == "otherLevel"
       doOtherLevelStuff()



Ok, that seems it will work. Now, buttons. The buttons aren't actually there, but we can simulate as if they were there with collision detection/mouse position.

Each object has a bounding rectangle, yes? We can use that with the mouse co-ordinates to see if the button was clicked:

while True:
    if event.type == QUIT:
      pygame.exit()
    elif event.type == KEYDOWN and event.key == K_ESCAPE:
        return
    elif event.type == MOUSEBUTTONDOWN:
       if gameOption.colidepoint(pygame.mouse.get_pos()):
            state = "game".
       if creditsOption.colidepoint(pygame.mouse.get_pos()):
            state = "credits"



Remember to include states to change back to the menu from them states. I think that's everything for now :)
Was This Post Helpful? 1
  • +
  • -

#12 poker158149  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 70
  • Joined: 29-June 11

Re: Help starting a top-down action game

Posted 24 October 2011 - 03:24 PM

That just happens to work beautifully. You, sir, are a genius.

I can't thank you enough for the help you've provided me.
If I hadn't had you help me, I'd probably be staring at my screen not knowing what to do.

This is a great start for me. Now I just have to figure out basically everything after the main menu :P.

Thanks again, and I hope you don't mind if I come back to you with any other questions, which I may be doing, haha.

EDIT: Oh, and the reason it was freezing is because I forgot to pygame.quit() at the end.

This post has been edited by poker158149: 24 October 2011 - 03:25 PM

Was This Post Helpful? 0
  • +
  • -

#13 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Help starting a top-down action game

Posted 24 October 2011 - 03:35 PM

That's great! I'm glad it works, once you learn pygame, I don't think you forget it :P That freezing issue is quite common, I'm SO relieved it was an easy fix.

Once you get into the animation, it gets a bit more complicated, but use the documentation on the pygame site, the tutorials too, and you should be set.

We have the reputation system to thank people for their help - although it's mainly for show.

Yeah, I'd be happy to help with any other questions you have, just come back here and you'll find me, every day most likely, it's a true addiction :)
Was This Post Helpful? 1
  • +
  • -

#14 poker158149  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 70
  • Joined: 29-June 11

Re: Help starting a top-down action game

Posted 24 October 2011 - 06:05 PM

You have +6 more reputation now from me :P.

Animation isn't really my strong point, but it can only help to learn it.

However, one more question before I dive into it:
How exactly can I go about doing something with just a mouse over, and not actually clicking? Like, when the user mouses over an option on the menu, how can I change it's color?
I've tried
if r2.collidepoint(pygame.mouse.get_pos()):


without any special event checking and where r2 is the reference to the text's rectangle, but that only works for the current point of the mouse, so if I moved it ever so slightly, whatever commands I have in the if statement re-run, and flickering between the default color and the new color every time the mouse moves doesn't exactly look nice.
Was This Post Helpful? 0
  • +
  • -

#15 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Help starting a top-down action game

Posted 24 October 2011 - 06:43 PM

Haha, Thanks :)

You should look at the type of events pygame can handle by default, what you want to use is a MOUSEMOTION event:

elif event.type == MOUSEMOTION:
   #If the mouse is over the button
   if r2.collidepoint(pygame.mouse.get_pos()):
         Change the buttons colour (redraw the new text)
   else:
   #The mouse is not moving over the button
         Change the button's colour back to default



Hope that helps! :)
Was This Post Helpful? 1
  • +
  • -

  • (7 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »