我正在尝试学习一些pygame编程,所以我找到了一个关键项目,这应该是学习python / pygame的良好开端。 链接:pivaders
所以我现在不想制作整个游戏。我只是想完成Game-Menu:这意味着我想要开始画面,在推动SPACE后我想要进入游戏画面。
这是我的代码
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()
现在我收到此错误:
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'
我发现了有关该错误和屏幕/自我屏幕的其他问题,但我无法弄清楚如何通过使用新信息来解决我的问题。 我不知道为什么AttributeError:在这里弹出我从未使用过没有自己的屏幕..
我已经在这里:
How do I solve an attribute error?
https://www.daniweb.com/software-development/python/threads/348024/global-name-screen-is-not-definedpygame
答案 0 :(得分:2)
您的__init__
方法中存在拼写错误。
self_screen = pygame.display.set_mode([RES[0], RES[1]])
应该是
self.screen = pygame.display.set_mode([RES[0], RES[1]])
请注意self_screen
和self.screen
之间的区别。
答案 1 :(得分:0)
好了,现在我得到了start_screen,但SPACE不是我认为的那样:3
我删除了“refresh_screen”方法,因为我猜这里没用。我还添加了全局变量“WHITE =(255,255,255)”,因为我忘记了。当然我用self.screen替换了self_screen
。
现在我必须通过推动SPACE来找出改变图片的缺失。如果我发现它,我会写另一个答案。
答案 2 :(得分:0)
好的,现在我有我想要的东西。如果我开始,start_screen进入一个800x600的窗口。按下SPACE后,我进入ingame_screen。使用ESC我回到开始屏幕并再次ESC关闭程序。
我错过了代码的和平:
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()
这里现在已经完整的代码(我得到了图片和Git链接的Orbitracer.ttf在我的问题中):
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()