Reputation: 1
I am trying to learn some pygame programming so i found the pivader project which should be a good start to learn python/pygame. here the link: pivaders
So I dont want to make the whole game for now. I am just trying to get the Game-Menu done: that means I want the start-screen and after pushing SPACE I want to enter the ingame screen.
Here my Code
import pygame, random
RES = (800, 600)
class GameState:
pass
class Game(object):
def __init__(self):
pygame.init()
pygame.font.init()
self_screen = pygame.display.set_mode([RES[0], RES[1]])
self.intro_font = pygame.font.Font(
'data/Orbitracer.ttf', 72)
self.intro_screen = pygame.image.load(
'/home/pi/pivaders/data/graphics/start_screen.jpg')
self.background = pygame.image.load(
'/home/pi/pivaders/data/graphics/Space-Background.jpg')
GameState.end_game = False
GameState.start_screen = True
def control(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
GameState.start_screen = False
GameState.end_game = True
if event.type == pygame.KEYDOWN \
and event.key == pygame.K_ESCAPE:
if GameState.start_screen:
GameState.start_screen = False
GameState.end_game = True
else:
GameState.start_screen = True
self.keys = pygame.key.get_pressed()
if self.keys[pygame.K_SPACE]:
if GameState.start_screen:
GameState.start_screen = False
def splash_screen(self):
while GameState.start_screen:
self.screen.blit(self.intro_screen, [0, 0])
self.screen.blit(self.intro_font.render(
"PIVADERS", 1, WHITE), (265, 128))
self.screen.blit(self.intro_font.render(
"PRESS SPACE TO PLAY", 1, WHITE), (284, 191))
pygame.display.flip()
self.control()
def refresh_screen(self):
self.all_sprite_list.draw(self.screen)
self.screen.blit(self.background, [0, 0])
self.clock.tick(self.refresh_rate)
def main_loop(self):
while not GameState.end_game:
while not GameState.start_screen:
self.control()
self.refresh_screen()
self.splash_screen()
pygame.quit()
if __name__ == '__main__':
pv = Game()
pv.main_loop()
Now i get this Error:
Traceback (most recent call last):
File "/home/pi/pivaders/MenuTest.py", line 65, in <module>
pv.main_loop()
File "/home/pi/pivaders/MenuTest.py", line 60, in main_loop
self.splash_screen()
File "/home/pi/pivaders/MenuTest.py", line 42, in splash_screen
self.screen.blit(self.intro_screen, [0, 0])
AttributeError: 'Game' object has no attribute 'screen'
I found other questions about that Error and the screen / self.screen but i couldnt figure out how to solve my problem by using that new informations. I dont know why the AttributeError: pops up here I never used the screen without self..
I already was here:
How do I solve an attribute error?
https://www.daniweb.com/software-development/python/threads/348024/global-name-screen-is-not-definedpygame
Upvotes: 0
Views: 1585
Reputation: 1
Ok now i have what i want to have. If i start, the start_screen comes in a window 800x600. After pushing SPACE i enter the ingame_screen. With ESC i come back to the start screen and again ESC to close the programm.
I missed that peace of code:
def refresh_screen(self):
pygame.display.flip()
self.screen.blit(self.background, [0, 0])
def main_loop(self):
while not GameState.end_game:
while not GameState.start_screen:
self.control()
self.refresh_screen() #change picture
self.splash_screen()
pygame.quit()
here now full code (i got the pictures and the Orbitracer.ttf from Git link is in my question):
import pygame, random
RES = (800, 600)
WHITE = (255, 255, 255)
class GameState:
pass
class Game(object):
def __init__(self):
pygame.init()
pygame.font.init()
self.screen = pygame.display.set_mode([RES[0], RES[1]])
self.intro_font = pygame.font.Font(
'data/Orbitracer.ttf', 72)
self.intro_screen = pygame.image.load(
'/home/pi/pivaders/data/graphics/start_screen.jpg')
self.background = pygame.image.load(
'/home/pi/pivaders/data/graphics/Space-Background.jpg')
GameState.end_game = False
GameState.start_screen = True
def control(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
GameState.start_screen = False
GameState.end_game = True
if event.type == pygame.KEYDOWN \
and event.key == pygame.K_ESCAPE:
if GameState.start_screen:
GameState.start_screen = False
GameState.end_game = True
else:
GameState.start_screen = True
self.keys = pygame.key.get_pressed()
if self.keys[pygame.K_SPACE]:
if GameState.start_screen:
GameState.start_screen = False
def splash_screen(self):
while GameState.start_screen:
self.screen.blit(self.intro_screen, [0, 0])
self.screen.blit(self.intro_font.render(
"PIVADERS", 1, WHITE), (175, 128))
self.screen.blit(self.intro_font.render(
"PRESS SPACE TO PLAY", 1, WHITE), (175, 191))
pygame.display.flip()
self.control()
def refresh_screen(self):
pygame.display.flip()
self.screen.blit(self.background, [0, 0])
def main_loop(self):
while not GameState.end_game:
while not GameState.start_screen:
self.control()
self.refresh_screen()
self.splash_screen()
pygame.quit()
if __name__ == '__main__':
pv = Game()
pv.main_loop()
Upvotes: 0
Reputation: 1
Ok now i get the start_screen but the SPACE does not what i thought it does :3
i deleted the "refresh_screen" method because i guess its useless here. i also added the global variable "WHITE = (255, 255, 255)" because i forgott that. and of course i replaced self_screen with self.screen.
.
Now i have to figure out what is missing to change the picture by pushing SPACE. I will write another answer if i found it out.
Upvotes: 0
Reputation: 13232
You have a typo in your __init__
method.
self_screen = pygame.display.set_mode([RES[0], RES[1]])
should be
self.screen = pygame.display.set_mode([RES[0], RES[1]])
Notice the difference between self_screen
and self.screen
.
Upvotes: 2