如何将文本文件中的文本显示到 pygame 屏幕上?
How to display text onto a pygame screen from a text file?
我正在尝试在 pygame 屏幕中显示来自文本文件的游戏说明。在这一点上,我已经编码了:
def instructions():
screen.fill(TEAL)
with open("pongInstructions.txt", encoding = 'utf-8') as f:
for line in f:
instructText = instructionsFont.render(line, True, WHITE)
screen.blit(instructText, ((400 - (instructText.get_width()/2)),(300 - (instructText.get_height()/2))))
pygame.display.update()
当我调用指令功能时,什么也没有发生。有人知道为什么吗?非常感谢任何帮助!
编辑:
#Imports and Initialize Pygame Screen
import pygame
import sys
import time
import random
import codecs
pygame.init()
screenSize = (800,600)
screen = pygame.display.set_mode((screenSize),0)
pygame.display.set_caption("Tasnim's Pong")
instructionsFile = [line.strip('\n')
for line in open('pongInstructions.txt', 'r', encoding = 'utf-8').readlines()]
font = pygame.font.SysFont("americantypewriter", 16)
def instructions(screen):
screen.fill(TEAL)
for n, line in enumerate(instructionsFile):
text = font.render(line, 1, WHITE)
text_rect = text.get_rect()
text_rect.centerx = screenSize[0]//2
text_rect.centery = n*25 + 50
screen.blit(text, text_rect)
pygame.display.update()
#Define Colours
WHITE = (255,255,255)
BLUE = (0,0,255)
BLACK = (0,0,0)
GRAY = (128, 128, 128)
MAROON = (128, 0, 0)
NAVYBLUE = (0, 0, 128)
OLIVE = (128, 128, 0)
PURPLE = (128, 0, 128)
TEAL = (0,128,128)
PINK = (226,132,164)
MUTEDBLUE = (155,182,203)
PLUM = (221,160,221)
#All Fonts
startscreenTitleFont = pygame.font.SysFont("americantypewriter", 120)
startscreenPlayerChoiceFont = pygame.font.SysFont("americantypewriter", 60)
startscreenQuitFont = ("americantypewriter", 60)
startscreenCreditsFont = ("americantypewriter", 60)
instructionsFont = pygame.font.SysFont("americantypewriter",60)
quitFont = pygame.font.SysFont("americantypewriter",60)
maingameP1ScoreFont = pygame.font.SysFont("americantypewriter",70)
maingameP2ScoreFont = pygame.font.SysFont("americantypewriter",70)
maingamePlayerOneFont = pygame.font.SysFont("americantypewriter",50)
maingamescoreFont = pygame.font.SysFont("americantypewriter",25, bold = True)
winnerFont = pygame.font.SysFont("americantypewriter",25)
instructionsFile = [line.strip('\n')
for line in open('pongInstructions.txt', 'r', encoding = 'utf-8').readlines()]
font = pygame.font.SysFont("americantypewriter", 16)
#Introduction Screen
def startscreen():
go = True
choice = 1
screen.fill(MUTEDBLUE)
pygame.display.update()
pongTitle = startscreenTitleFont.render("Pong", True, WHITE)
screen.blit(pongTitle,((400 - (pongTitle.get_width()/2)),0))
playerchoiceTitle = startscreenPlayerChoiceFont.render("Two Player Game", True, WHITE)
screen.blit(playerchoiceTitle, ((400 - (playerchoiceTitle.get_width()/2)),160))
playerchoice2Title = startscreenPlayerChoiceFont.render("One Player Game", True, WHITE)
screen.blit(playerchoice2Title, ((400 - (playerchoice2Title.get_width()/2)),275))
instructionsTitle = instructionsFont.render("How To Play", True, WHITE)
screen.blit(instructionsTitle, ((400 - (instructionsTitle.get_width()/2)),390))
quitgameTitle = quitFont.render("Quit", True, WHITE)
screen.blit(quitgameTitle, ((400 - (quitgameTitle.get_width()/2)),495))
pygame.display.update()
#Choices for Introduction Screen
while go:
for event in pygame.event.get():
if event.type == pygame.QUIT:
go = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
if choice != 1:
choice = choice - 1
elif event.key == pygame.K_DOWN:
if choice != 4:
choice = choice + 1
elif event.key == pygame.K_RETURN:
if choice == 1:
playTwoPlayer()
elif choice == 2:
playOnePlayer()
elif choice == 3:
instructions()
elif choice == 4:
quitGame()
if choice == 1:
screen.fill(MUTEDBLUE)
pongTitle = startscreenTitleFont.render("Pong", True, WHITE)
screen.blit(pongTitle,((400 - (pongTitle.get_width()/2)),0))
playerchoiceTitle = startscreenPlayerChoiceFont.render("Two Player Game", True, NAVYBLUE)
screen.blit(playerchoiceTitle, ((400 - (playerchoiceTitle.get_width()/2)),160))
playerchoice2Title = startscreenPlayerChoiceFont.render("One Player Game", True, WHITE)
screen.blit(playerchoice2Title, ((400 - (playerchoice2Title.get_width()/2)),275))
instructionsTitle = instructionsFont.render("How To Play", True, WHITE)
screen.blit(instructionsTitle, ((400 - (instructionsTitle.get_width()/2)),390))
quitgameTitle = quitFont.render("Quit", True, WHITE)
screen.blit(quitgameTitle, ((400 - (quitgameTitle.get_width()/2)),495))
elif choice == 2:
screen.fill(MUTEDBLUE)
pongTitle = startscreenTitleFont.render("Pong", True, WHITE)
screen.blit(pongTitle,((400 - (pongTitle.get_width()/2)),0))
playerchoiceTitle = startscreenPlayerChoiceFont.render("Two Player Game", True, WHITE)
screen.blit(playerchoiceTitle, ((400 - (playerchoiceTitle.get_width()/2)),160))
playerchoice2Title = startscreenPlayerChoiceFont.render("One Player Game", True, NAVYBLUE)
screen.blit(playerchoice2Title, ((400 - (playerchoice2Title.get_width()/2)),275))
instructionsTitle = instructionsFont.render("How To Play", True, WHITE)
screen.blit(instructionsTitle, ((400 - (instructionsTitle.get_width()/2)),390))
quitgameTitle = quitFont.render("Quit", True, WHITE)
screen.blit(quitgameTitle, ((400 - (quitgameTitle.get_width()/2)),495))
elif choice == 3:
screen.fill(MUTEDBLUE)
pongTitle = startscreenTitleFont.render("Pong", True, WHITE)
screen.blit(pongTitle,((400 - (pongTitle.get_width()/2)),0))
playerchoiceTitle = startscreenPlayerChoiceFont.render("Two Player Game", True, WHITE)
screen.blit(playerchoiceTitle, ((400 - (playerchoiceTitle.get_width()/2)),160))
playerchoice2Title = startscreenPlayerChoiceFont.render("One Player Game", True, WHITE)
screen.blit(playerchoice2Title, ((400 - (playerchoice2Title.get_width()/2)),275))
instructionsTitle = instructionsFont.render("How To Play", True, NAVYBLUE)
screen.blit(instructionsTitle, ((400 - (instructionsTitle.get_width()/2)),390))
quitgameTitle = quitFont.render("Quit", True, WHITE)
screen.blit(quitgameTitle, ((400 - (quitgameTitle.get_width()/2)),495))
if choice == 4:
screen.fill(MUTEDBLUE)
pongTitle = startscreenTitleFont.render("Pong", True, WHITE)
screen.blit(pongTitle,((400 - (pongTitle.get_width()/2)),0))
playerchoiceTitle = startscreenPlayerChoiceFont.render("Two Player Game", True, WHITE)
screen.blit(playerchoiceTitle, ((400 - (playerchoiceTitle.get_width()/2)),160))
playerchoice2Title = startscreenPlayerChoiceFont.render("One Player Game", True, WHITE)
screen.blit(playerchoice2Title, ((400 - (playerchoice2Title.get_width()/2)),275))
instructionsTitle = instructionsFont.render("How To Play", True, WHITE)
screen.blit(instructionsTitle, ((400 - (instructionsTitle.get_width()/2)),390))
quitgameTitle = quitFont.render("Quit", True, NAVYBLUE)
screen.blit(quitgameTitle, ((400 - (quitgameTitle.get_width()/2)),495))
pygame.display.update()
pygame.display.update()
#Two Player Game
def playTwoPlayer():
screen.fill(MUTEDBLUE)
play = True
startballSPACE = True
#Two Player Game- Set Variables
playeroneScore = 0
playertwoScore = 0
ballx = 400
bally = 350
balldx = 0
balldy = 0
xOne = 775
yOne = 275
xTwo = 0
yTwo = 275
dyOne = 0
dyTwo = 0
heightPaddleoneandtwo = 150
paddleheight = 150
go = True
while go:
if playeroneScore == 3:
winnerPlayerOne()
if playertwoScore == 3:
winnerPlayerTwo()
balldxdir = random.choice([-5,5])
balldydir = random.choice([-5,5])
for event in pygame.event.get():
if event.type == pygame.QUIT:
quitGame()
elif event.type == pygame.KEYDOWN:
#Controls for player one- Up and Down key
if event.key == pygame.K_UP:
dyOne = -10
directiony = 3
elif event.key == pygame.K_DOWN:
dyOne = 10
directiony = 4
#Controls for player two- W and S key
elif event.key == pygame.K_w:
dyTwo = -10
directionyTWO = 2
elif event.key == pygame.K_s:
dyTwo = 10
directionyTWO = 1
#Controls to quit
elif event.key == pygame.K_q:
startscreen()
#Controls to start game (press space bar)
elif startballSPACE == True:
if event.key == pygame.K_SPACE:
play = True
balldx = balldxdir
balldy = balldydir
startballSPACE = False
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
dyOne = 0
directiony = 0
elif event.key == pygame.K_w or event.key == pygame.K_s:
dyTwo = 0
directionyTWO = 0
screen.fill(MUTEDBLUE)
#Paddle Positioning
yOne = yOne + dyOne
yTwo =yTwo + dyTwo
#Screen Top and Bottom Collision Check
if yOne > (600 - heightPaddleoneandtwo) and directiony == 4:
dyOne = 0
yOne = (600 - heightPaddleoneandtwo)
if yOne < 100 and directiony == 3:
dyOne = 0
yOne = 100
if yTwo > (600 - heightPaddleoneandtwo) and directionyTWO == 1:
dyTwo = 0
yTwo = (600 - heightPaddleoneandtwo)
if yTwo < 100 and directionyTWO == 2:
dyTwo = 0
yTwo = 100
#Makes Ball Bounce in opposite direction
if play == True:
if ballx == xOne and bally <= (yOne + heightPaddleoneandtwo) and bally >= yOne:
balldx = -balldx
#Makes Paddle Shorter on collisions with ball (makes game tougher)
if heightPaddleoneandtwo >= 10:
heightPaddleoneandtwo = heightPaddleoneandtwo - 7
else:
heightPaddleoneandtwo = heightPaddleoneandtwo
#Makes Ball Bounce in opposite direction
elif bally >= 580:
balldy = -balldy
elif ballx == (xTwo + 25) and bally <= (yTwo + heightPaddleoneandtwo) and bally >= yTwo:
balldx = -balldx
#Makes Paddle Shorter on collisions with ball (makes game tougher)
if heightPaddleoneandtwo >= 10:
heightPaddleoneandtwo = heightPaddleoneandtwo - 7
else:
heightPaddleoneandtwo = heightPaddleoneandtwo
elif bally <= 110:
balldy = -balldy
elif ballx<0 or ballx>800:
#Adds score if ball passes paddle
if ballx>800:
playeroneScore = playeroneScore + 1
startballSPACE = True
elif ballx < 0:
startballSPACE = True
playertwoScore = playertwoScore + 1
ballx = 400
bally = 350
balldx = 0
balldy = 0
yOne = 275
y2Two = 275
heightPaddleoneandtwo = 150
play = False
ballx = ballx + balldx
bally = bally - balldy
#Draw all screen components
pygame.draw.rect(screen, WHITE,(xOne, yOne, 25, heightPaddleoneandtwo),0)
pygame.draw.rect(screen, WHITE,(xTwo, yTwo, 25, heightPaddleoneandtwo),0)
pygame.draw.circle (screen, WHITE, (ballx,bally), 10, 0)
textScore = maingamescoreFont.render("Score", True, WHITE)
screen.blit(textScore, (((800/2)-(textScore.get_width()/2),0)))
textPlayeroneScore = maingameP1ScoreFont.render(str(playeroneScore), True, WHITE)
screen.blit(textPlayeroneScore, (0,0))
textPlayertwoScore = maingameP2ScoreFont.render(str(playertwoScore), True, WHITE)
screen.blit(textPlayertwoScore, ((800 - textPlayertwoScore.get_width()),0))
pygame.draw.rect (screen, WHITE, (0,90,800,10),0)
pygame.draw.rect (screen, WHITE, (395,112,10,25),0)
pygame.draw.rect (screen, WHITE, (395,162,10,25),0)
pygame.draw.rect (screen, WHITE, (395,212,10,25),0)
pygame.draw.rect (screen, WHITE, (395,262,10,25),0)
pygame.draw.rect (screen, WHITE, (395,312,10,25),0)
pygame.draw.rect (screen, WHITE, (395,362,10,25),0)
pygame.draw.rect (screen, WHITE, (395,412,10,25),0)
pygame.draw.rect (screen, WHITE, (395,462,10,25),0)
pygame.draw.rect (screen, WHITE, (395,512,10,25),0)
pygame.draw.rect (screen, WHITE, (395,562,10,25),0)
pygame.draw.rect (screen, WHITE, (395,612,10,25),0)
pygame.draw.rect (screen, WHITE, (45,0,3,100),0)
pygame.draw.rect (screen, WHITE, (752,0,3,100),0)
playerOneText = maingamePlayerOneFont.render("Player 1", True, WHITE)
playerTwoText = maingamePlayerOneFont.render("Player 2", True, WHITE)
screen.blit(playerOneText, (70,15))
screen.blit(playerTwoText, (525,15))
pygame.display.update()
def instructions():
screen.fill(TEAL)
for n, line in enumerate(instructionsFile):
text = font.render(line, 1, WHITE)
text_rect = text.get_rect()
text_rect.centerx = screenSize[0]//2
text_rect.centery = n*25 + 50
screen.blit(text, text_rect)
pygame.display.update()
def winnerPlayerOne():
screen.fill(TEAL)
winnerP1Message = winnerFont.render("Congrats Player 1. You Win! Press 'c' to continue", True, WHITE)
screen.blit(winnerP1Message, ((400 - (winnerP1Message.get_width()/2)),(300 - (winnerP1Message.get_height()/2))))
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
quitGame()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_c:
startscreen()
def winnerPlayerTwo():
screen.fill(TEAL)
winnerP2Message = winnerFont.render("Congrats Player 2. You Win! Press 'c' to continue", True, WHITE)
screen.blit(winnerP2Message, ((400 - (winnerP2Message.get_width()/2)),(300 - (winnerP2Message.get_height()/2))))
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
quitGame()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_c:
startscreen()
def CPUwinner():
screen.fill(TEAL)
computerWinner = winnerFont.render("Better luck next time... The computer has won! Press 'c' to continue", True, WHITE)
screen.blit(computerWinner, ((400 - (computerWinner.get_width()/2)),(300 - (computerWinner.get_height()/2))))
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
quitGame()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_c:
startscreen()
def quitGame():
pygame.quit()
sys.exit()
def playOnePlayer():
screen.fill(MUTEDBLUE)
play = True
startballSPACE = True
#One Player Game- Set Variables
playeroneScore = 0
playertwoScore = 0
ballx = 400
bally = 350
balldx = 0
balldy = 0
xOne = 775
yOne = 275
xTwo = 0
yTwo = 275
dyOne = 0
dyTwo = 0
heightPaddleoneandtwo = 150
paddleheight = 150
go = True
while go:
if playeroneScore == 3:
winnerPlayerOne()
if playertwoScore == 3:
CPUwinner()
balldxdir = random.choice([-5,5])
balldydir = random.choice([-5,5])
for event in pygame.event.get():
if event.type == pygame.QUIT:
quitGame()
elif event.type == pygame.KEYDOWN:
#Controls for player one- Up and Down key
if event.key == pygame.K_UP:
dyTwo = -10
directiony = 3
elif event.key == pygame.K_DOWN:
dyTwo = 10
directiony = 4
#Controls to quit
elif event.key == pygame.K_q:
startscreen()
#Controls to start game (press space bar)
elif startballSPACE == True:
if event.key == pygame.K_SPACE:
play = True
balldx = balldxdir
balldy = balldydir
startballSPACE = False
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
dyTwo = 0
directionyTWO = 0
screen.fill(MUTEDBLUE)
#Paddle Positioning
yTwo = yTwo + dyTwo
yOne = bally - (heightPaddleoneandtwo/2)
#Screen Top and Bottom Collision Check
if yOne > (600 - heightPaddleoneandtwo):
dyOne = 0
yOne = (600 - heightPaddleoneandtwo)
if yOne < 100:
dyOne = 0
yOne = 100
if yTwo > (600 - heightPaddleoneandtwo) and directiony == 4:
dyTwo = 0
yTwo = (600 - heightPaddleoneandtwo)
if yTwo < 100 and directiony == 3:
dyTwo = 0
yTwo = 100
#Makes Ball Bounce in opposite direction
if play == True:
if ballx == xOne and bally <= (yOne + heightPaddleoneandtwo) and bally >= yOne:
balldx = -balldx
#Makes Paddle Shorter on collisions with ball (makes game tougher)
if heightPaddleoneandtwo >= 10:
heightPaddleoneandtwo = heightPaddleoneandtwo - 7
else:
heightPaddleoneandtwo = heightPaddleoneandtwo
#Makes Ball Bounce in opposite direction
elif bally >= 580:
balldy = -balldy
elif ballx == (xTwo + 25) and bally <= (yTwo + heightPaddleoneandtwo) and bally >= yTwo:
balldx = -balldx
#Makes Paddle Shorter on collisions with ball (makes game tougher)
if heightPaddleoneandtwo >= 10:
heightPaddleoneandtwo = heightPaddleoneandtwo - 7
else:
heightPaddleoneandtwo = heightPaddleoneandtwo
elif bally <= 110:
balldy = -balldy
elif ballx<0 or ballx>800:
#Adds score if ball passes paddle
if ballx>800:
playeroneScore = playeroneScore + 1
startballSPACE = True
elif ballx < 0:
startballSPACE = True
playertwoScore = playertwoScore + 1
ballx = 400
bally = 350
balldx = 0
balldy = 0
yOne = 275
y2Two = 275
heightPaddleoneandtwo = 150
play = False
ballx = ballx + balldx
bally = bally - balldy
#Draw all screen components
pygame.draw.rect(screen, WHITE,(xOne, yOne, 25, heightPaddleoneandtwo),0)
pygame.draw.rect(screen, WHITE,(xTwo, yTwo, 25, heightPaddleoneandtwo),0)
pygame.draw.circle (screen, WHITE, (ballx,bally), 10, 0)
textScore = maingamescoreFont.render("Score", True, WHITE)
screen.blit(textScore, (((800/2)-(textScore.get_width()/2),0)))
textPlayeroneScore = maingameP1ScoreFont.render(str(playeroneScore), True, WHITE)
screen.blit(textPlayeroneScore, (0,0))
textPlayertwoScore = maingameP2ScoreFont.render(str(playertwoScore), True, WHITE)
screen.blit(textPlayertwoScore, ((800 - textPlayertwoScore.get_width()),0))
pygame.draw.rect (screen, WHITE, (0,90,800,10),0)
pygame.draw.rect (screen, WHITE, (395,112,10,25),0)
pygame.draw.rect (screen, WHITE, (395,162,10,25),0)
pygame.draw.rect (screen, WHITE, (395,212,10,25),0)
pygame.draw.rect (screen, WHITE, (395,262,10,25),0)
pygame.draw.rect (screen, WHITE, (395,312,10,25),0)
pygame.draw.rect (screen, WHITE, (395,362,10,25),0)
pygame.draw.rect (screen, WHITE, (395,412,10,25),0)
pygame.draw.rect (screen, WHITE, (395,462,10,25),0)
pygame.draw.rect (screen, WHITE, (395,512,10,25),0)
pygame.draw.rect (screen, WHITE, (395,562,10,25),0)
pygame.draw.rect (screen, WHITE, (395,612,10,25),0)
pygame.draw.rect (screen, WHITE, (45,0,3,100),0)
pygame.draw.rect (screen, WHITE, (752,0,3,100),0)
playerOneText = maingamePlayerOneFont.render("Player 1", True, WHITE)
playerTwoText = maingamePlayerOneFont.render("CPU", True, WHITE)
screen.blit(playerOneText, (70,15))
screen.blit(playerTwoText, (630,15))
pygame.display.update()
startscreen()
您就快完成了,但是 screen.blit 需要一个矩形对象与您的文本精灵一起使用,该对象用于定义精灵的大小和位置。此外,你需要给每一行它自己的 x 或 y 坐标(否则它们将全部绘制在彼此之上)解决这个问题的方法是使用 for n, line in enumerate(f)
这将为你提供每个索引值行,称为 n。
如果使用枚举并删除当前的 screen.blit 语句并将其替换为此块,它应该可以工作:
text_rect = instructText.get_rect()
text_rect.centerx = SCREENSIZE[0]//2
text_rect.centery = n*25 + 50
screen.blit(text, text_rect)
这里你通过调用它的get_rect()
方法得到一个文本精灵的矩形对象,然后将它的中心x坐标设置为屏幕的中心(我用一个contant来保持屏幕大小),然后将它的中心 y 坐标设置为 25 * 数组中的行位置 + 50 个填充像素,最后使用 screen.blit(text, text_rect)
将其绘制到屏幕上
我用自定义字体做了一个工作示例,你可以看看here
在之前的项目中我也做过这样的任务对象,欢迎大家使用,或者复制过来,大家可以找到here
无论如何,希望对您有所帮助,编码愉快。
编辑:
刚刚查看了您的代码,据我所知,您的代码目前什么也没做 :)
您在 sartscreen() 函数中定义了您想要的行为,但从不调用该函数,因此没有任何反应,这是您的主要问题,幸运的是它很容易解决,只需像这样调用文档底部的函数 startscreen(screen)
等等?你会注意到我已经将屏幕对象添加到这个函数调用中,这是因为你需要将屏幕对象的引用交给你的函数,否则它不知道你正在谈论的 "which" 屏幕对象,您需要对任何使用屏幕对象的函数执行此操作,但是您还需要告诉您的函数需要一个参数,因此当您定义开始屏幕函数和指令函数时,您还需要添加屏幕对象像这样 def startscreen(screen):
和 def instructions(screen):
现在你的函数知道你在调用 screen.fill(COLOR)
和 screen.blit(text, text_rect)
时在说什么
除此之外,这只是一个小细节问题,但在应用这两个更改后,您的菜单似乎适用于 python 3.5,但是我建议您查看 Clock object 以及限制你的帧速率(否则游戏将 运行 在快速计算机上超快)
我正在尝试在 pygame 屏幕中显示来自文本文件的游戏说明。在这一点上,我已经编码了:
def instructions():
screen.fill(TEAL)
with open("pongInstructions.txt", encoding = 'utf-8') as f:
for line in f:
instructText = instructionsFont.render(line, True, WHITE)
screen.blit(instructText, ((400 - (instructText.get_width()/2)),(300 - (instructText.get_height()/2))))
pygame.display.update()
当我调用指令功能时,什么也没有发生。有人知道为什么吗?非常感谢任何帮助!
编辑:
#Imports and Initialize Pygame Screen
import pygame
import sys
import time
import random
import codecs
pygame.init()
screenSize = (800,600)
screen = pygame.display.set_mode((screenSize),0)
pygame.display.set_caption("Tasnim's Pong")
instructionsFile = [line.strip('\n')
for line in open('pongInstructions.txt', 'r', encoding = 'utf-8').readlines()]
font = pygame.font.SysFont("americantypewriter", 16)
def instructions(screen):
screen.fill(TEAL)
for n, line in enumerate(instructionsFile):
text = font.render(line, 1, WHITE)
text_rect = text.get_rect()
text_rect.centerx = screenSize[0]//2
text_rect.centery = n*25 + 50
screen.blit(text, text_rect)
pygame.display.update()
#Define Colours
WHITE = (255,255,255)
BLUE = (0,0,255)
BLACK = (0,0,0)
GRAY = (128, 128, 128)
MAROON = (128, 0, 0)
NAVYBLUE = (0, 0, 128)
OLIVE = (128, 128, 0)
PURPLE = (128, 0, 128)
TEAL = (0,128,128)
PINK = (226,132,164)
MUTEDBLUE = (155,182,203)
PLUM = (221,160,221)
#All Fonts
startscreenTitleFont = pygame.font.SysFont("americantypewriter", 120)
startscreenPlayerChoiceFont = pygame.font.SysFont("americantypewriter", 60)
startscreenQuitFont = ("americantypewriter", 60)
startscreenCreditsFont = ("americantypewriter", 60)
instructionsFont = pygame.font.SysFont("americantypewriter",60)
quitFont = pygame.font.SysFont("americantypewriter",60)
maingameP1ScoreFont = pygame.font.SysFont("americantypewriter",70)
maingameP2ScoreFont = pygame.font.SysFont("americantypewriter",70)
maingamePlayerOneFont = pygame.font.SysFont("americantypewriter",50)
maingamescoreFont = pygame.font.SysFont("americantypewriter",25, bold = True)
winnerFont = pygame.font.SysFont("americantypewriter",25)
instructionsFile = [line.strip('\n')
for line in open('pongInstructions.txt', 'r', encoding = 'utf-8').readlines()]
font = pygame.font.SysFont("americantypewriter", 16)
#Introduction Screen
def startscreen():
go = True
choice = 1
screen.fill(MUTEDBLUE)
pygame.display.update()
pongTitle = startscreenTitleFont.render("Pong", True, WHITE)
screen.blit(pongTitle,((400 - (pongTitle.get_width()/2)),0))
playerchoiceTitle = startscreenPlayerChoiceFont.render("Two Player Game", True, WHITE)
screen.blit(playerchoiceTitle, ((400 - (playerchoiceTitle.get_width()/2)),160))
playerchoice2Title = startscreenPlayerChoiceFont.render("One Player Game", True, WHITE)
screen.blit(playerchoice2Title, ((400 - (playerchoice2Title.get_width()/2)),275))
instructionsTitle = instructionsFont.render("How To Play", True, WHITE)
screen.blit(instructionsTitle, ((400 - (instructionsTitle.get_width()/2)),390))
quitgameTitle = quitFont.render("Quit", True, WHITE)
screen.blit(quitgameTitle, ((400 - (quitgameTitle.get_width()/2)),495))
pygame.display.update()
#Choices for Introduction Screen
while go:
for event in pygame.event.get():
if event.type == pygame.QUIT:
go = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
if choice != 1:
choice = choice - 1
elif event.key == pygame.K_DOWN:
if choice != 4:
choice = choice + 1
elif event.key == pygame.K_RETURN:
if choice == 1:
playTwoPlayer()
elif choice == 2:
playOnePlayer()
elif choice == 3:
instructions()
elif choice == 4:
quitGame()
if choice == 1:
screen.fill(MUTEDBLUE)
pongTitle = startscreenTitleFont.render("Pong", True, WHITE)
screen.blit(pongTitle,((400 - (pongTitle.get_width()/2)),0))
playerchoiceTitle = startscreenPlayerChoiceFont.render("Two Player Game", True, NAVYBLUE)
screen.blit(playerchoiceTitle, ((400 - (playerchoiceTitle.get_width()/2)),160))
playerchoice2Title = startscreenPlayerChoiceFont.render("One Player Game", True, WHITE)
screen.blit(playerchoice2Title, ((400 - (playerchoice2Title.get_width()/2)),275))
instructionsTitle = instructionsFont.render("How To Play", True, WHITE)
screen.blit(instructionsTitle, ((400 - (instructionsTitle.get_width()/2)),390))
quitgameTitle = quitFont.render("Quit", True, WHITE)
screen.blit(quitgameTitle, ((400 - (quitgameTitle.get_width()/2)),495))
elif choice == 2:
screen.fill(MUTEDBLUE)
pongTitle = startscreenTitleFont.render("Pong", True, WHITE)
screen.blit(pongTitle,((400 - (pongTitle.get_width()/2)),0))
playerchoiceTitle = startscreenPlayerChoiceFont.render("Two Player Game", True, WHITE)
screen.blit(playerchoiceTitle, ((400 - (playerchoiceTitle.get_width()/2)),160))
playerchoice2Title = startscreenPlayerChoiceFont.render("One Player Game", True, NAVYBLUE)
screen.blit(playerchoice2Title, ((400 - (playerchoice2Title.get_width()/2)),275))
instructionsTitle = instructionsFont.render("How To Play", True, WHITE)
screen.blit(instructionsTitle, ((400 - (instructionsTitle.get_width()/2)),390))
quitgameTitle = quitFont.render("Quit", True, WHITE)
screen.blit(quitgameTitle, ((400 - (quitgameTitle.get_width()/2)),495))
elif choice == 3:
screen.fill(MUTEDBLUE)
pongTitle = startscreenTitleFont.render("Pong", True, WHITE)
screen.blit(pongTitle,((400 - (pongTitle.get_width()/2)),0))
playerchoiceTitle = startscreenPlayerChoiceFont.render("Two Player Game", True, WHITE)
screen.blit(playerchoiceTitle, ((400 - (playerchoiceTitle.get_width()/2)),160))
playerchoice2Title = startscreenPlayerChoiceFont.render("One Player Game", True, WHITE)
screen.blit(playerchoice2Title, ((400 - (playerchoice2Title.get_width()/2)),275))
instructionsTitle = instructionsFont.render("How To Play", True, NAVYBLUE)
screen.blit(instructionsTitle, ((400 - (instructionsTitle.get_width()/2)),390))
quitgameTitle = quitFont.render("Quit", True, WHITE)
screen.blit(quitgameTitle, ((400 - (quitgameTitle.get_width()/2)),495))
if choice == 4:
screen.fill(MUTEDBLUE)
pongTitle = startscreenTitleFont.render("Pong", True, WHITE)
screen.blit(pongTitle,((400 - (pongTitle.get_width()/2)),0))
playerchoiceTitle = startscreenPlayerChoiceFont.render("Two Player Game", True, WHITE)
screen.blit(playerchoiceTitle, ((400 - (playerchoiceTitle.get_width()/2)),160))
playerchoice2Title = startscreenPlayerChoiceFont.render("One Player Game", True, WHITE)
screen.blit(playerchoice2Title, ((400 - (playerchoice2Title.get_width()/2)),275))
instructionsTitle = instructionsFont.render("How To Play", True, WHITE)
screen.blit(instructionsTitle, ((400 - (instructionsTitle.get_width()/2)),390))
quitgameTitle = quitFont.render("Quit", True, NAVYBLUE)
screen.blit(quitgameTitle, ((400 - (quitgameTitle.get_width()/2)),495))
pygame.display.update()
pygame.display.update()
#Two Player Game
def playTwoPlayer():
screen.fill(MUTEDBLUE)
play = True
startballSPACE = True
#Two Player Game- Set Variables
playeroneScore = 0
playertwoScore = 0
ballx = 400
bally = 350
balldx = 0
balldy = 0
xOne = 775
yOne = 275
xTwo = 0
yTwo = 275
dyOne = 0
dyTwo = 0
heightPaddleoneandtwo = 150
paddleheight = 150
go = True
while go:
if playeroneScore == 3:
winnerPlayerOne()
if playertwoScore == 3:
winnerPlayerTwo()
balldxdir = random.choice([-5,5])
balldydir = random.choice([-5,5])
for event in pygame.event.get():
if event.type == pygame.QUIT:
quitGame()
elif event.type == pygame.KEYDOWN:
#Controls for player one- Up and Down key
if event.key == pygame.K_UP:
dyOne = -10
directiony = 3
elif event.key == pygame.K_DOWN:
dyOne = 10
directiony = 4
#Controls for player two- W and S key
elif event.key == pygame.K_w:
dyTwo = -10
directionyTWO = 2
elif event.key == pygame.K_s:
dyTwo = 10
directionyTWO = 1
#Controls to quit
elif event.key == pygame.K_q:
startscreen()
#Controls to start game (press space bar)
elif startballSPACE == True:
if event.key == pygame.K_SPACE:
play = True
balldx = balldxdir
balldy = balldydir
startballSPACE = False
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
dyOne = 0
directiony = 0
elif event.key == pygame.K_w or event.key == pygame.K_s:
dyTwo = 0
directionyTWO = 0
screen.fill(MUTEDBLUE)
#Paddle Positioning
yOne = yOne + dyOne
yTwo =yTwo + dyTwo
#Screen Top and Bottom Collision Check
if yOne > (600 - heightPaddleoneandtwo) and directiony == 4:
dyOne = 0
yOne = (600 - heightPaddleoneandtwo)
if yOne < 100 and directiony == 3:
dyOne = 0
yOne = 100
if yTwo > (600 - heightPaddleoneandtwo) and directionyTWO == 1:
dyTwo = 0
yTwo = (600 - heightPaddleoneandtwo)
if yTwo < 100 and directionyTWO == 2:
dyTwo = 0
yTwo = 100
#Makes Ball Bounce in opposite direction
if play == True:
if ballx == xOne and bally <= (yOne + heightPaddleoneandtwo) and bally >= yOne:
balldx = -balldx
#Makes Paddle Shorter on collisions with ball (makes game tougher)
if heightPaddleoneandtwo >= 10:
heightPaddleoneandtwo = heightPaddleoneandtwo - 7
else:
heightPaddleoneandtwo = heightPaddleoneandtwo
#Makes Ball Bounce in opposite direction
elif bally >= 580:
balldy = -balldy
elif ballx == (xTwo + 25) and bally <= (yTwo + heightPaddleoneandtwo) and bally >= yTwo:
balldx = -balldx
#Makes Paddle Shorter on collisions with ball (makes game tougher)
if heightPaddleoneandtwo >= 10:
heightPaddleoneandtwo = heightPaddleoneandtwo - 7
else:
heightPaddleoneandtwo = heightPaddleoneandtwo
elif bally <= 110:
balldy = -balldy
elif ballx<0 or ballx>800:
#Adds score if ball passes paddle
if ballx>800:
playeroneScore = playeroneScore + 1
startballSPACE = True
elif ballx < 0:
startballSPACE = True
playertwoScore = playertwoScore + 1
ballx = 400
bally = 350
balldx = 0
balldy = 0
yOne = 275
y2Two = 275
heightPaddleoneandtwo = 150
play = False
ballx = ballx + balldx
bally = bally - balldy
#Draw all screen components
pygame.draw.rect(screen, WHITE,(xOne, yOne, 25, heightPaddleoneandtwo),0)
pygame.draw.rect(screen, WHITE,(xTwo, yTwo, 25, heightPaddleoneandtwo),0)
pygame.draw.circle (screen, WHITE, (ballx,bally), 10, 0)
textScore = maingamescoreFont.render("Score", True, WHITE)
screen.blit(textScore, (((800/2)-(textScore.get_width()/2),0)))
textPlayeroneScore = maingameP1ScoreFont.render(str(playeroneScore), True, WHITE)
screen.blit(textPlayeroneScore, (0,0))
textPlayertwoScore = maingameP2ScoreFont.render(str(playertwoScore), True, WHITE)
screen.blit(textPlayertwoScore, ((800 - textPlayertwoScore.get_width()),0))
pygame.draw.rect (screen, WHITE, (0,90,800,10),0)
pygame.draw.rect (screen, WHITE, (395,112,10,25),0)
pygame.draw.rect (screen, WHITE, (395,162,10,25),0)
pygame.draw.rect (screen, WHITE, (395,212,10,25),0)
pygame.draw.rect (screen, WHITE, (395,262,10,25),0)
pygame.draw.rect (screen, WHITE, (395,312,10,25),0)
pygame.draw.rect (screen, WHITE, (395,362,10,25),0)
pygame.draw.rect (screen, WHITE, (395,412,10,25),0)
pygame.draw.rect (screen, WHITE, (395,462,10,25),0)
pygame.draw.rect (screen, WHITE, (395,512,10,25),0)
pygame.draw.rect (screen, WHITE, (395,562,10,25),0)
pygame.draw.rect (screen, WHITE, (395,612,10,25),0)
pygame.draw.rect (screen, WHITE, (45,0,3,100),0)
pygame.draw.rect (screen, WHITE, (752,0,3,100),0)
playerOneText = maingamePlayerOneFont.render("Player 1", True, WHITE)
playerTwoText = maingamePlayerOneFont.render("Player 2", True, WHITE)
screen.blit(playerOneText, (70,15))
screen.blit(playerTwoText, (525,15))
pygame.display.update()
def instructions():
screen.fill(TEAL)
for n, line in enumerate(instructionsFile):
text = font.render(line, 1, WHITE)
text_rect = text.get_rect()
text_rect.centerx = screenSize[0]//2
text_rect.centery = n*25 + 50
screen.blit(text, text_rect)
pygame.display.update()
def winnerPlayerOne():
screen.fill(TEAL)
winnerP1Message = winnerFont.render("Congrats Player 1. You Win! Press 'c' to continue", True, WHITE)
screen.blit(winnerP1Message, ((400 - (winnerP1Message.get_width()/2)),(300 - (winnerP1Message.get_height()/2))))
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
quitGame()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_c:
startscreen()
def winnerPlayerTwo():
screen.fill(TEAL)
winnerP2Message = winnerFont.render("Congrats Player 2. You Win! Press 'c' to continue", True, WHITE)
screen.blit(winnerP2Message, ((400 - (winnerP2Message.get_width()/2)),(300 - (winnerP2Message.get_height()/2))))
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
quitGame()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_c:
startscreen()
def CPUwinner():
screen.fill(TEAL)
computerWinner = winnerFont.render("Better luck next time... The computer has won! Press 'c' to continue", True, WHITE)
screen.blit(computerWinner, ((400 - (computerWinner.get_width()/2)),(300 - (computerWinner.get_height()/2))))
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
quitGame()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_c:
startscreen()
def quitGame():
pygame.quit()
sys.exit()
def playOnePlayer():
screen.fill(MUTEDBLUE)
play = True
startballSPACE = True
#One Player Game- Set Variables
playeroneScore = 0
playertwoScore = 0
ballx = 400
bally = 350
balldx = 0
balldy = 0
xOne = 775
yOne = 275
xTwo = 0
yTwo = 275
dyOne = 0
dyTwo = 0
heightPaddleoneandtwo = 150
paddleheight = 150
go = True
while go:
if playeroneScore == 3:
winnerPlayerOne()
if playertwoScore == 3:
CPUwinner()
balldxdir = random.choice([-5,5])
balldydir = random.choice([-5,5])
for event in pygame.event.get():
if event.type == pygame.QUIT:
quitGame()
elif event.type == pygame.KEYDOWN:
#Controls for player one- Up and Down key
if event.key == pygame.K_UP:
dyTwo = -10
directiony = 3
elif event.key == pygame.K_DOWN:
dyTwo = 10
directiony = 4
#Controls to quit
elif event.key == pygame.K_q:
startscreen()
#Controls to start game (press space bar)
elif startballSPACE == True:
if event.key == pygame.K_SPACE:
play = True
balldx = balldxdir
balldy = balldydir
startballSPACE = False
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
dyTwo = 0
directionyTWO = 0
screen.fill(MUTEDBLUE)
#Paddle Positioning
yTwo = yTwo + dyTwo
yOne = bally - (heightPaddleoneandtwo/2)
#Screen Top and Bottom Collision Check
if yOne > (600 - heightPaddleoneandtwo):
dyOne = 0
yOne = (600 - heightPaddleoneandtwo)
if yOne < 100:
dyOne = 0
yOne = 100
if yTwo > (600 - heightPaddleoneandtwo) and directiony == 4:
dyTwo = 0
yTwo = (600 - heightPaddleoneandtwo)
if yTwo < 100 and directiony == 3:
dyTwo = 0
yTwo = 100
#Makes Ball Bounce in opposite direction
if play == True:
if ballx == xOne and bally <= (yOne + heightPaddleoneandtwo) and bally >= yOne:
balldx = -balldx
#Makes Paddle Shorter on collisions with ball (makes game tougher)
if heightPaddleoneandtwo >= 10:
heightPaddleoneandtwo = heightPaddleoneandtwo - 7
else:
heightPaddleoneandtwo = heightPaddleoneandtwo
#Makes Ball Bounce in opposite direction
elif bally >= 580:
balldy = -balldy
elif ballx == (xTwo + 25) and bally <= (yTwo + heightPaddleoneandtwo) and bally >= yTwo:
balldx = -balldx
#Makes Paddle Shorter on collisions with ball (makes game tougher)
if heightPaddleoneandtwo >= 10:
heightPaddleoneandtwo = heightPaddleoneandtwo - 7
else:
heightPaddleoneandtwo = heightPaddleoneandtwo
elif bally <= 110:
balldy = -balldy
elif ballx<0 or ballx>800:
#Adds score if ball passes paddle
if ballx>800:
playeroneScore = playeroneScore + 1
startballSPACE = True
elif ballx < 0:
startballSPACE = True
playertwoScore = playertwoScore + 1
ballx = 400
bally = 350
balldx = 0
balldy = 0
yOne = 275
y2Two = 275
heightPaddleoneandtwo = 150
play = False
ballx = ballx + balldx
bally = bally - balldy
#Draw all screen components
pygame.draw.rect(screen, WHITE,(xOne, yOne, 25, heightPaddleoneandtwo),0)
pygame.draw.rect(screen, WHITE,(xTwo, yTwo, 25, heightPaddleoneandtwo),0)
pygame.draw.circle (screen, WHITE, (ballx,bally), 10, 0)
textScore = maingamescoreFont.render("Score", True, WHITE)
screen.blit(textScore, (((800/2)-(textScore.get_width()/2),0)))
textPlayeroneScore = maingameP1ScoreFont.render(str(playeroneScore), True, WHITE)
screen.blit(textPlayeroneScore, (0,0))
textPlayertwoScore = maingameP2ScoreFont.render(str(playertwoScore), True, WHITE)
screen.blit(textPlayertwoScore, ((800 - textPlayertwoScore.get_width()),0))
pygame.draw.rect (screen, WHITE, (0,90,800,10),0)
pygame.draw.rect (screen, WHITE, (395,112,10,25),0)
pygame.draw.rect (screen, WHITE, (395,162,10,25),0)
pygame.draw.rect (screen, WHITE, (395,212,10,25),0)
pygame.draw.rect (screen, WHITE, (395,262,10,25),0)
pygame.draw.rect (screen, WHITE, (395,312,10,25),0)
pygame.draw.rect (screen, WHITE, (395,362,10,25),0)
pygame.draw.rect (screen, WHITE, (395,412,10,25),0)
pygame.draw.rect (screen, WHITE, (395,462,10,25),0)
pygame.draw.rect (screen, WHITE, (395,512,10,25),0)
pygame.draw.rect (screen, WHITE, (395,562,10,25),0)
pygame.draw.rect (screen, WHITE, (395,612,10,25),0)
pygame.draw.rect (screen, WHITE, (45,0,3,100),0)
pygame.draw.rect (screen, WHITE, (752,0,3,100),0)
playerOneText = maingamePlayerOneFont.render("Player 1", True, WHITE)
playerTwoText = maingamePlayerOneFont.render("CPU", True, WHITE)
screen.blit(playerOneText, (70,15))
screen.blit(playerTwoText, (630,15))
pygame.display.update()
startscreen()
您就快完成了,但是 screen.blit 需要一个矩形对象与您的文本精灵一起使用,该对象用于定义精灵的大小和位置。此外,你需要给每一行它自己的 x 或 y 坐标(否则它们将全部绘制在彼此之上)解决这个问题的方法是使用 for n, line in enumerate(f)
这将为你提供每个索引值行,称为 n。
如果使用枚举并删除当前的 screen.blit 语句并将其替换为此块,它应该可以工作:
text_rect = instructText.get_rect()
text_rect.centerx = SCREENSIZE[0]//2
text_rect.centery = n*25 + 50
screen.blit(text, text_rect)
这里你通过调用它的get_rect()
方法得到一个文本精灵的矩形对象,然后将它的中心x坐标设置为屏幕的中心(我用一个contant来保持屏幕大小),然后将它的中心 y 坐标设置为 25 * 数组中的行位置 + 50 个填充像素,最后使用 screen.blit(text, text_rect)
我用自定义字体做了一个工作示例,你可以看看here
在之前的项目中我也做过这样的任务对象,欢迎大家使用,或者复制过来,大家可以找到here
无论如何,希望对您有所帮助,编码愉快。
编辑:
刚刚查看了您的代码,据我所知,您的代码目前什么也没做 :)
您在 sartscreen() 函数中定义了您想要的行为,但从不调用该函数,因此没有任何反应,这是您的主要问题,幸运的是它很容易解决,只需像这样调用文档底部的函数 startscreen(screen)
等等?你会注意到我已经将屏幕对象添加到这个函数调用中,这是因为你需要将屏幕对象的引用交给你的函数,否则它不知道你正在谈论的 "which" 屏幕对象,您需要对任何使用屏幕对象的函数执行此操作,但是您还需要告诉您的函数需要一个参数,因此当您定义开始屏幕函数和指令函数时,您还需要添加屏幕对象像这样 def startscreen(screen):
和 def instructions(screen):
现在你的函数知道你在调用 screen.fill(COLOR)
和 screen.blit(text, text_rect)
除此之外,这只是一个小细节问题,但在应用这两个更改后,您的菜单似乎适用于 python 3.5,但是我建议您查看 Clock object 以及限制你的帧速率(否则游戏将 运行 在快速计算机上超快)