这里的一切都在Windows中正常运行,但是一旦我尝试在Mac上进行开发,我就会遇到很多错误。目前,我正在研究这个问题:
/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 /Users/dlee/PycharmProjects/Pong/pong_5_intro_oop.py
Traceback (most recent call last):
File "/Users/dlee/PycharmProjects/Pong/pong_5_intro_oop.py", line 261, in <module>
main()
File "/Users/dlee/PycharmProjects/Pong/pong_5_intro_oop.py", line 219, in main
text = Text()
File "/Users/dlee/PycharmProjects/Pong/pong_5_intro_oop.py", line 53, in __init__
self.pong = pygame.font.SysFont(pygame.font.get_default_font(), 250)
File "/Library/Python/2.7/site-packages/pygame/sysfont.py", line 572, in SysFont
initsysfonts()
File "/Library/Python/2.7/site-packages/pygame/sysfont.py", line 522, in initsysfonts
fonts = initsysfonts_darwin()
File "/Library/Python/2.7/site-packages/pygame/sysfont.py", line 419, in initsysfonts_darwin
fonts = initsysfonts_unix()
File "/Library/Python/2.7/site-packages/pygame/sysfont.py", line 456, in initsysfonts_unix
close_fds=True).communicate()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 751, in communicate
return self._communicate(input)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1291, in _communicate
stdout, stderr = self._communicate_with_poll(input)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1345, in _communicate_with_poll
ready = poller.poll()
KeyboardInterrupt
Process finished with exit code 1
您可以查看下面的代码块,我已将其更改为默认字体。更改类Text中的字体,从特定字体更改为默认字体已修复了我的Mac桌面上的问题,但仍然在我的Mac笔记本电脑上导致上述错误。
class Text(Size):
def __init__(self):
Size.__init__(self)
self.pong = pygame.font.SysFont(pygame.font.get_default_font(), 250)
self.clickHere = pygame.font.SysFont(pygame.font.get_default_font(), 65)
self.darkGreen = (179, 224, 161)
self.lastFPS = pygame.time.get_ticks()
两者都在运行优胜美地。这些行导致程序停止。我正在运行yosemite,pygame和python 2.7
以下是游戏的完整代码:
__author__ = 'daniel'
import sys, pygame, os
# Force static position of screen
os.environ['SDL_VIDEO_CENTERED'] = '1'
# to get rid of sound lag
pygame.mixer.pre_init(44100, -16, 2, 2048)
# Runs imported module
pygame.init()
class Size:
def __init__(self):
self.size = self.width, self.height = 920, 570
class Sound:
def __init__(self):
self.intro = pygame.mixer.Sound('sound/intro.ogg')
self.select = pygame.mixer.Sound('sound/select.ogg')
self.easy = pygame.mixer.Sound('sound/easy.ogg')
def soundTrack(self, track):
if track == "intro":
self.intro.play(-1)
elif track == "easy":
self.easy.play(-1)
elif track == "medium":
pass
elif track == "hard":
pass
elif track == "select":
self.select.play(2)
self.select.fadeout(1500)
# Fades out Intro music on game start
def startCheck(self):
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN or pygame.key.get_pressed()[pygame.K_RETURN] != 0:
self.soundTrack("select")
self.intro.fadeout(1500)
pygame.time.wait(2300)
return True
elif event.type == pygame.QUIT:
sys.exit()
class Text(Size):
def __init__(self):
Size.__init__(self)
self.pong = pygame.font.SysFont(pygame.font.get_default_font(), 250)
self.clickHere = pygame.font.SysFont(pygame.font.get_default_font(), 65)
self.darkGreen = (179, 224, 161)
self.lastFPS = pygame.time.get_ticks()
def click(self, gScreen, back):
# Blinking start text
if (pygame.time.get_ticks() - self.lastFPS)%1000 < 500:
gScreen.screen.blit(back.introBack, back.introrect)
# Centering text
start = self.clickHere.render("- Click here to start! -", 1, self.darkGreen)
startpos = start.get_rect()
startpos = startpos.move(0, self.height/1.7)
startpos.centerx = gScreen.screen.get_rect().centerx
gScreen.screen.blit(start, startpos)
else:
gScreen.screen.blit(back.introBack, back.introrect)
def title(self, gScreen):
# Print title
title = self.pong.render("Pong", 1, self.darkGreen)
titlepos = title.get_rect()
titlepos = titlepos.move(0, self.height/4)
titlepos.centerx = gScreen.screen.get_rect().centerx
gScreen.screen.blit(title, titlepos)
def end(self):
pass
class GameScreen(Size):
def __init__(self):
Size.__init__(self)
self.black = (0, 0, 0)
self.fps = 30
self.counter = 0
self.clock = pygame.time.Clock()
self.screen = pygame.display.set_mode(self.size, pygame.SRCALPHA)
pygame.display.set_caption('Pong')
def refresh(self, back, ball, lpaddle, rpaddle):
# Print background
self.screen.fill(self.black)
# Limits frames per iteration of while loop
self.clock.tick(self.fps)
# Print images
self.screen.fill(self.black)
self.screen.blit(back.background, back.backrect)
self.screen.blit(ball.ball, ball.ballrect)
self.screen.blit(rpaddle.rpaddle, rpaddle.rpaddlerect)
self.screen.blit(lpaddle.lpaddle, lpaddle.lpaddlerect)
class Background(Size):
def __init__(self):
Size.__init__(self)
# Intro background
self.introBack = pygame.image.load("image/introBackground.bmp").convert()
self.introBack = pygame.transform.scale(self.introBack, (self.width, self.height))
self.introrect = self.introBack.get_rect()
self.introrect = self.introrect.move(0,0)
#Game background
self.background = pygame.image.load("image/background.bmp").convert()
self.background = pygame.transform.scale(self.background, (self.width, self.height))
self.backrect = self.background.get_rect()
self.backrect = self.backrect.move(0,0)
class Paddle(Size):
def __init__(self):
Size.__init__(self)
self.paddleSpace = 10
self.paddlespeed = 15
self.paddlewidth = 40
self.paddleheight = 150
self.buffer = 10
class RightPaddle(Paddle):
def __init__(self):
Paddle.__init__(self)
self.rpaddle = pygame.image.load("image/paddle.bmp").convert_alpha()
self.rpaddle = pygame.transform.scale(self.rpaddle, (self.paddlewidth, self.paddleheight))
self.rpaddlerect = self.rpaddle.get_rect()
self.rpaddlerect = self.rpaddlerect.move((self.width - self.rpaddlerect.width - self.paddleSpace, self.height/2-(self.rpaddlerect.height/2)))
def move(self):
if pygame.key.get_pressed()[pygame.K_UP] != 0 and self.rpaddlerect.y-self.buffer > 0:
self.rpaddlerect = self.rpaddlerect.move((0, -self.paddlespeed))
elif pygame.key.get_pressed()[pygame.K_DOWN] != 0 and self.rpaddlerect.y < self.height-self.rpaddlerect.height-self.buffer:
self.rpaddlerect = self.rpaddlerect.move((0, self.paddlespeed))
class LeftPaddle(Paddle):
def __init__(self):
Paddle.__init__(self)
self.lpaddle = pygame.image.load("image/paddle.bmp").convert_alpha()
self.lpaddle = pygame.transform.scale(self.lpaddle, (self.paddlewidth, self.paddleheight))
self.lpaddlerect = self.lpaddle.get_rect()
self.lpaddlerect = self.lpaddlerect.move((self.paddleSpace, self.height/2-(self.lpaddlerect.height/2)))
def move(self):
# Moves paddles with W/S and UP/DOWN
if pygame.key.get_pressed()[pygame.K_w] != 0 and self.lpaddlerect.y-self.buffer > 0:
self.lpaddlerect = self.lpaddlerect.move((0, -self.paddlespeed))
elif pygame.key.get_pressed()[pygame.K_s] != 0 and self.lpaddlerect.y < self.height-self.lpaddlerect.height-self.buffer:
self.lpaddlerect = self.lpaddlerect.move((0, self.paddlespeed))
class Ball(Size):
def __init__(self):
Size.__init__(self)
self.speed = [10, 10]
#convert_alpha() for transparent images
self.ball = pygame.image.load("image/ball.bmp").convert_alpha()
self.ball = pygame.transform.scale(self.ball, (60, 60))
self.ballrect = self.ball.get_rect()
self.ballrect = self.ballrect.move((self.width/2 - (self.ballrect.width/2), self.height/2-(self.ballrect.height/2)))
# Sound effects
self.blipwall = pygame.mixer.Sound('sound/beep1.ogg')
self.blippaddle = pygame.mixer.Sound('sound/blip1.wav')
# Moves the ball on start
def move(self):
self.ballrect = self.ballrect.move(self.speed)
# Bounces off walls
def walls(self):
if (self.ballrect.top - 20) < 0 or (self.ballrect.bottom + 20) > self.height:
self.blipwall.play()
self.speed[1] = -self.speed[1]
def paddles(self, lpaddle, rpaddle):
# If ball hits paddles
if self.ballrect.colliderect(lpaddle.lpaddlerect) or self.ballrect.colliderect(rpaddle.rpaddlerect):
self.blippaddle.play()
self.speed[0] = -self.speed[0]
def main():
# Create screen
gScreen = GameScreen()
# Create Background
back = Background()
# Loads Sound
sound = Sound()
# Create Paddles
lpaddle = LeftPaddle()
rpaddle = RightPaddle()
# Create Ball
ball = Ball()
# Plays intro
sound.soundTrack("intro")
text = Text()
# Intro
while True:
# Prints blinking text
text.click(gScreen, back)
# Prints title
text.title(gScreen)
pygame.display.flip()
# Checks game start
if sound.startCheck():
break
# Gameplay
while True:
# Checks if window exit button pressed
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# Writes to surface
gScreen.refresh(back, ball, lpaddle, rpaddle)
# Writes changes to main surface
pygame.display.flip()
if gScreen.counter == 0:
pygame.time.wait(1500)
gScreen.counter = 1;
# Ball movement
ball.move()
# Ball hits walls
ball.walls()
# When Ball hits Paddles
ball.paddles(lpaddle, rpaddle)
# Move Paddles
lpaddle.move()
rpaddle.move()
if __name__ == "__main__":
main()