Need help making game - Python

Started by
8 comments, last by Durakken 12 years, 9 months ago
Ok so here's the thing. I know how to or mostly know how to do a bunch of the stuff that goes into making what i want but I'm stuck at some of what might be consider more basic things.

For example I can write a text-based RPG battle engine, the walking around part of an RPG, and I'm fairly certain I know how to write a graphical menu, but haven't tried to do it just yet.

The problem is I haven't integrated this all into one and I keep on running into problems(errors in python) that I can't find solutions for... and anything I can think of to try causes more of a problem.

Also I can't find any tutorials that do any basic RPG stuff like open/closing menus, switching to a battle mode, etc... so I'm really winging it and hoping that I've figured out a way that will work.

Here's the code I'm working with so far...


#Global Variables
GameMode = "StartMenu"

#Import Modules
import pygame
from pygame.locals import *

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

#classes for our game objects


def main():
#Initialize Everything
pygame.init()
screen = pygame.display.set_mode((468, 60))
pygame.display.set_caption('Aporia')
pygame.mouse.set_visible(0)

#Create The Backgound
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((250, 250, 250))

#Display The Background
screen.blit(background, (0, 0))
pygame.display.flip()

#Prepare Game Objects
clock = pygame.time.Clock()

#Main Loop
while 1:
clock.tick(60)

if GameMode == "StartMenu":
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
elif GameMode == "InGameMenu":
return
elif GameMode == "Walk":
return
elif GameMode == "Battle":
return
elif GameMode == "SpaceTravel":
return
elif GameMode == "Fighter":
return

#Draw Everything
screen.blit(background, (0, 0))
pygame.display.flip()


#Game Over


#this calls the 'main' function when this script is executed
if __name__ == '__main__': main()



This is causing this error to show up...
error: display Surface quit

I tried moving around the pygame.quit() function, but it gives me the same error or the window stops responding.

Also..
I through the global variable at the top of the code because that's where it is in a lot of languages and i think works for Python too, not sure if that's standard in game programming though
The if..elif thing I used because i can't remember the other thing that may or may not be better there...
My idea for that is that I'm going to have each of those "play modes" in different modules and since they will each have their own actions for same events they the event stuff should be within them. The for loop in the StartMenu is temporary to make the code work till i get the modules there, but I also considered that that should be outside of it cuz that will be the same for every module, but that doesn't work quite right for some reason.

The screen resolution in code is just that way due to the exmple code i'm using and i haven't changed it since i haven't figured out the error yet.


So, am I doing this right? and can someone help me with this error?
also I just noticed something I have main as a regular function (as it was in the original code I altered to work from) isn't it usually a class? Could that be the problem?
Advertisement
so none of the 61 people that looked has any idea as to how i can get rid of that error or if it's normal?
Noone can comment on whether i'm doing the if...elif thing right?
[font="Courier New"] pygame.quit()[/font] doesn't quit your game. It just tells Pygame to get rid of it's resources, it's a cleanup method.

When [font="Courier New"]pygame.QUIT[/font] is received, your program calls [font="Courier New"]pygame.quit()[/font] and it deletes a few things here and there, but your program keeps running. When you get to the line "[font="Courier New"]screen.blit(background, (0, 0))[/font]", the variable [font="Courier New"]screen[/font] is invalid, causing the [font="Courier New"]blit[/font] to fail.

Instead of running everything inside a [font="Courier New"]while 1[/font] loop, keep a variable to tell whether your game is running, and set it in the event loop accordingly:


gameIsRunning = True
while gameIsRunning:
for e in pygame.event.get():
if event.type == QUIT:
gameIsRunning = False
After that, place [font="Courier New"]pygame.quit() [/font][font="Arial"]outside the game loop, so you don't run into invalid data.[/font]
Thank you, can you tell me why a simple break doesn't work where you place gameisRunning = False (hit the close button does nothing)? or why moving the for loop to about the blit it causes the game to stop responding?
Thank you, can you tell me why a simple break doesn't work where you place gameisRunning = False (hit the close button does nothing)?[/quote]That's because [font="Courier New"]break[/font] will put you out of the innermost loop. In this case, the [font="Courier New"]for[/font] loop, not the [font="Courier New"]while[/font] loop.


or why moving the for loop to about the blit it causes the game to stop responding?[/quote]

I can't really tell what do you mean with that, or to where the for loop really goes.



Thank you, can you tell me why a simple break doesn't work where you place gameisRunning = False (hit the close button does nothing)?
That's because [font="Courier New"]break[/font] will put you out of the innermost loop. In this case, the [font="Courier New"]for[/font] loop, not the [font="Courier New"]while[/font] loop.
[/quote]

I feel like an idiot for not getting these questions v.v


or why moving the for loop to about the blit it causes the game to stop responding?[/quote]

I can't really tell what do you mean with that, or to where the for loop really goes.


[/quote]

If I move the for loop out the if...elif structure and put it under it between it and the blit, the #draw everything stuff. And then run it. If I then click on the X instead of closing the window will say "not responding" then go open up a Windows error window.

I don't see any reason code wise why that would happen because that where the code already was before i put in the if...elif stuff and where it is in a number of other programs. I don't see how the if...elif would make a difference to the for loop and break the program.
If I move the for loop out the if...elif structure and put it under it between it and the blit, the #draw everything stuff. And then run it. If I then click on the X instead of closing the window will say "not responding" then go open up a Windows error window.[/quote]

From your description, I've came up with:


[source lang="python"]
while gameIsRunning:
if GameMode == "StartMenu":
pass
elif GameMode == "..."
...

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

screen.blit(background, (0,0))
pygame.display.flip()
pygame.quit()
[/source]


Which will run and quit without issues, so I don't think I know what you've done. Care to show some code?

If I move the for loop out the if...elif structure and put it under it between it and the blit, the #draw everything stuff. And then run it. If I then click on the X instead of closing the window will say "not responding" then go open up a Windows error window.


From your description, I've came up with:


[source lang="python"]
while gameIsRunning:
if GameMode == "StartMenu":
pass
elif GameMode == "..."
...

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

screen.blit(background, (0,0))
pygame.display.flip()
pygame.quit()
[/source]


Which will run and quit without issues, so I don't think I know what you've done. Care to show some code?
[/quote]

Sorta what I am talking about. I haven't tried it with the gameIsRunning variable...

[source lang="python"]
while 1:
if GameMode == "StartMenu":
pass
elif GameMode == "..."
...

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

screen.blit(background, (0,0))
pygame.display.flip()
[/source]

this is what causes it. I don't know why. I'm curious as it should work like that but it doesn't for me.

Sorta what I am talking about. I haven't tried it with the gameIsRunning variable...

[source lang="python"]
while 1:
if GameMode == "StartMenu":
pass
elif GameMode == "..."
...

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

screen.blit(background, (0,0))
pygame.display.flip()
[/source]

this is what causes it. I don't know why. I'm curious as it should work like that but it doesn't for me.


The only problem with that code is the use of pygame.quit() in the event loop, causing the screen variable to be invalid. Other than that, it's ok.




The only problem with that code is the use of pygame.quit() in the event loop, causing the screen variable to be invalid. Other than that, it's ok.



The screen thing just give me an error in the IDLE, however the way this is it the program stops responding. I just figured the screen thing would still be in error though cuz the IDLE doesn't show it when the program messes up like that. Oh well, if it's not the code at fault it might be something on my PC doing it for some weird reason.

Thanks for the help

This topic is closed to new replies.

Advertisement