我目前正在尝试在pygame中重新创建Pong。这是我的代码:
import sys, pygame
from pygame.locals import *
FPS = 60
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
WINDOWWIDTH = 400
WINDOWHEIGHT = 300
LINETHICKNESS = 10
PADDLESIZE = 50
PADDLEOFFSET = 20
def drawArena():
DISPLAYSURF.fill((BLACK))
pygame.draw.rect(DISPLAYSURF, WHITE, ((0,0),(WINDOWWIDTH,WINDOWHEIGHT)), LINETHICKNESS*2)
pygame.draw.line(DISPLAYSURF, WHITE, ((WINDOWWIDTH/2),0),((WINDOWWIDTH/2),WINDOWHEIGHT), (LINETHICKNESS/4))
def drawPaddle(paddle):
if paddle.bottom > (WINDOWHEIGHT - LINETHICKNESS):
paddle.bottom = WINDOWHEIGHT - LINETHICKNESS
elif paddle.top > LINETHICKNESS:
paddle.top = LINETHICKNESS
pygame.draw.rect(DISPLAYSURF, WHITE, paddle)
def drawBall(ball):
pygame.draw.rect(DISPLAYSURF, WHITE, ball)
def moveBall(ball, ballDirX, ballDirY):
ball.x += ballDirX
ball.y += ballDirY
return ball
def checkWallCollision(ball, ballDirX, ballDirY):
if ball.top == (LINETHICKNESS) or ball.bottom == (WINDOWHEIGHT - LINETHICKNESS):
ballDirY = ballDirY * -1
if ball.left == (LINETHICKNESS) or ball.right == (WINDOWHEIGHT - LINETHICKNESS):
ballDirX = ballDirX * -1
return ballDirX, ballDirY
def checkPaddleCollision(ball, ballDirX, paddle1, paddle2):
if ballDirX == -1 and paddle1.right == ball.left and paddle1.top < ball.top and paddle1.bottom > ball.bottom:
return -1
elif ballDirX == -1 and paddle2.left == ball.right and paddle2.top < ball.top and paddle2.bottom > ball.bottom:
return -1
else:
return 1
def checkPointScored(paddle1, ball, score, ballDirX):
if ball.left == LINETHICKNESS:
return 0
elif ballDirX == -1 and paddle1.right == ball.left and paddle1.top < ball.top and paddle1.bottom > ball.bottom:
score += 1
elif ball.right == WINDOWWIDTH - LINETHICKNESS:
score += 5
return score
else: return score
def aI(ball, ballDirX, paddle2):
if ballDirX == -1:
if paddle2.centery < (WINDOWHEIGHT/2):
paddle2.y += 1
elif paddle2.centery > (WINDOWHEIGHT/2):
paddle2.y -= 1
elif ballDirX == 1:
if paddle2.centery < ball.centery:
paddle2.y += 1
else:
paddle2.y -=1
return paddle2
def displayScore(score):
resultSurf = BASICFONT.render('Score = %s' %(score), True, WHITE)
resultRect = resultSurf,get_rect()
resultRect.topleft = WINDOWWIDTH = 150, 25
DISPLAYSURF.blit(resultSurf, resultRect)
def main():
pygame.init()
global DISPLAYSURF
global BASICFONT, BASICFONTSIZE
BASICFONTSIZE = 20
BASICFONT = pygame.font.Font('freesansbold.ttf', BASICFONTSIZE)
FPSCLOCK = pygame.time.Clock()
DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH,WINDOWHEIGHT))
pygame.display.set_caption('Pong')
ballX = WINDOWWIDTH/2 - LINETHICKNESS/2
ballY = WINDOWHEIGHT/2 - LINETHICKNESS/2
playerOnePosition = (WINDOWHEIGHT - PADDLESIZE) /2
playerTwoPosition = (WINDOWHEIGHT - PADDLESIZE) /2
score = 0
ballDirX = -1
ballDirY = -1
paddle1 = pygame.Rect(PADDLEOFFSET, playerOnePosition, LINETHICKNESS, PADDLESIZE)
paddle2 = pygame.Rect(WINDOWWIDTH - PADDLEOFFSET - LINETHICKNESS, playerTwoPosition, LINETHICKNESS, PADDLESIZE)
ball = pygame.Rect(ballX, ballY, LINETHICKNESS, LINETHICKNESS)
drawArena()
drawPaddle(paddle1)
drawPaddle(paddle2)
drawBall(ball)
while True: #main game loop
pygame.event.get()
pygame.event.pump()
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
drawArena()
drawPaddle(paddle1)
drawPaddle(paddle2)
drawBall(ball)
pygame.display.update()
FPSCLOCK.tick(FPS)
if __name__=='__main__':
main()
我的问题是当我通过shell运行文件时输出为:
Python 3.6.0 |Continuum Analytics, Inc.| (default, Dec 23 2016, 11:57:41) on Windows (64 bits).
This is the Pyzo interpreter with integrated event loop for TK.
Type 'help' for help, type '?' for a list of *magic* commands.
Running script: "H:\pong.py"
然而,我创建的窗口没有出现,看起来好像它已经被打开或者某种类型的东西崩溃了。请帮我看看这是什么!
答案 0 :(得分:0)
下面的代码运行正常,在命令行中显示输出事件,并可以处理事件并关闭窗口。
除了已经提到的(请参阅注释)缩进问题之外,捕获事件存在问题(请参阅输出注释的行),因此无法处理事件并关闭凸起的窗口。
import sys, pygame
from pygame.locals import *
FPS = 60
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
WINDOWWIDTH = 400
WINDOWHEIGHT = 300
LINETHICKNESS = 10
PADDLESIZE = 50
PADDLEOFFSET = 20
def drawArena():
DISPLAYSURF.fill((BLACK))
pygame.draw.rect(DISPLAYSURF, WHITE, ((0,0),(WINDOWWIDTH,WINDOWHEIGHT)), LINETHICKNESS*2)
pygame.draw.line(DISPLAYSURF, WHITE, ((int(WINDOWWIDTH/2)),0),((int(WINDOWWIDTH/2)),WINDOWHEIGHT), (int(LINETHICKNESS/4)))
def drawPaddle(paddle):
if paddle.bottom > (WINDOWHEIGHT - LINETHICKNESS):
paddle.bottom = WINDOWHEIGHT - LINETHICKNESS
elif paddle.top > LINETHICKNESS:
paddle.top = LINETHICKNESS
pygame.draw.rect(DISPLAYSURF, WHITE, paddle)
def drawBall(ball):
pygame.draw.rect(DISPLAYSURF, WHITE, ball)
def moveBall(ball, ballDirX, ballDirY):
ball.x += ballDirX
ball.y += ballDirY
return ball
def checkWallCollision(ball, ballDirX, ballDirY):
if ball.top == (LINETHICKNESS) or ball.bottom == (WINDOWHEIGHT - LINETHICKNESS):
ballDirY = ballDirY * -1
if ball.left == (LINETHICKNESS) or ball.right == (WINDOWHEIGHT - LINETHICKNESS):
ballDirX = ballDirX * -1
return ballDirX, ballDirY
def checkPaddleCollision(ball, ballDirX, paddle1, paddle2):
if ballDirX == -1 and paddle1.right == ball.left and paddle1.top < ball.top and paddle1.bottom > ball.bottom:
return -1
elif ballDirX == -1 and paddle2.left == ball.right and paddle2.top < ball.top and paddle2.bottom > ball.bottom:
return -1
else:
return 1
def checkPointScored(paddle1, ball, score, ballDirX):
if ball.left == LINETHICKNESS:
return 0
elif ballDirX == -1 and paddle1.right == ball.left and paddle1.top < ball.top and paddle1.bottom > ball.bottom:
score += 1
elif ball.right == WINDOWWIDTH - LINETHICKNESS:
score += 5
return score
else: return score
def aI(ball, ballDirX, paddle2):
if ballDirX == -1:
if paddle2.centery < (WINDOWHEIGHT/2):
paddle2.y += 1
elif paddle2.centery > (WINDOWHEIGHT/2):
paddle2.y -= 1
elif ballDirX == 1:
if paddle2.centery < ball.centery:
paddle2.y += 1
else:
paddle2.y -=1
return paddle2
def displayScore(score):
resultSurf = BASICFONT.render('Score = %s' %(score), True, WHITE)
resultRect = resultSurf,get_rect()
resultRect.topleft = WINDOWWIDTH = 150, 25
DISPLAYSURF.blit(resultSurf, resultRect)
def main():
pygame.init()
global DISPLAYSURF
global BASICFONT, BASICFONTSIZE
BASICFONTSIZE = 20
BASICFONT = pygame.font.Font('freesansbold.ttf', BASICFONTSIZE)
FPSCLOCK = pygame.time.Clock()
DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH,WINDOWHEIGHT))
pygame.display.set_caption('Pong')
ballX = WINDOWWIDTH/2 - LINETHICKNESS/2
ballY = WINDOWHEIGHT/2 - LINETHICKNESS/2
playerOnePosition = (WINDOWHEIGHT - PADDLESIZE) /2
playerTwoPosition = (WINDOWHEIGHT - PADDLESIZE) /2
score = 0
ballDirX = -1
ballDirY = -1
paddle1 = pygame.Rect(PADDLEOFFSET, playerOnePosition, LINETHICKNESS, PADDLESIZE)
paddle2 = pygame.Rect(WINDOWWIDTH - PADDLEOFFSET - LINETHICKNESS, playerTwoPosition, LINETHICKNESS, PADDLESIZE)
ball = pygame.Rect(ballX, ballY, LINETHICKNESS, LINETHICKNESS)
drawArena()
drawPaddle(paddle1)
drawPaddle(paddle2)
drawBall(ball)
while True: #main game loop
# pygame.event.get()
# pygame.event.pump()
for event in pygame.event.get():
print("event", event)
if event.type == QUIT:
pygame.quit()
sys.exit()
drawArena()
drawPaddle(paddle1)
drawPaddle(paddle2)
drawBall(ball)
pygame.display.update()
FPSCLOCK.tick(FPS)
if __name__=='__main__':
main()