import pygame #setup
pygame.init()
screen = pygame.display.set_mode((800,600))
DISPLAYSURF = pygame.display.set_caption("Smley Pong")
keepGoing = True
pic = pygame.image.load("Crazysmile.bmp")
colorkey = pic.get_at((0,0))
pic.set_colorkey(colorkey)
picx = 0
picy = 0
BLACK = (0,0,0)
WHITE = (255,255,255)
Clock = pygame.time.Clock()
speedx = 5
speedy = 5
paddlew = 200
paddleh = 25
paddley = 550
picw = 100
pich = 100
points = 0
lives = 5
font = pygame.font.SysFont("Times", 24)
while keepGoing: # Game loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
keepGoing = False
picx += speedx
picy += speedy
if picx <= 0 or picx + pic.get_width() >= 800:
speedx = -speedx
if picy <= 0:
speedy = -speedy
if picy >= 500:
lives -= 1
speedy = -speedy
screen.fill(BLACK)
screen.blit(pic, (picx, picy))
# Draw Paddle
paddlex = pygame.mouse.get_pos()[0]
paddlex = paddlew/2
pygame.draw.rect(screen, WHITE, (paddlex, paddley, paddlew, paddleh))
# Check for paddle bounce
if picy + pich >=paddley and picy + pich <=paddley + paddleh \
and speedy > 0:
if picx +picw / 2 >= paddlex and picx +picw / 2 <= paddlex + \
paddlew:
points += 1
speedy = -speedy
# Draw text on screen
draw_screen = "Lives: " + str(lives) + "points: " + str(points)
# Check whether game is over
if lives < 1:
speedx = speedy = 0
draw_String = "Game Over.Your score was: " + str(points) draw_string += ". Press F1 to play again. "
text = font.render(draw_string, True, WHITE)
text_rect = text.get_rect()
text_rect.centerx = screen.get_rect().centerx
text.rect.y = 10
screen.bilt(text, text_rect)
pygame.display.update()
timer.tick(60)
pygame.quit() # Exit
这是一个用乒乓球游戏的书,我想测试一下 pygame但是屏幕上一片空白,所以我搜索是否有人有同样的问题,但他们都有不同的答案,所以我无法解决它。我添加了一些额外的部分,但如果你认为我不需要它们或你想让我添加一些东西,只要说你认为我应该做什么。 如果有什么建议请回答,也建议也回答。在
您必须将代码从
picx += speedx
行缩进到pygame.quit()
之前的行然后您必须在
pygame.quit()
行之前添加pygame.display.flip()
(并将其缩进!)在那么}位置。在
text.rect.y = 10
行就没有意义了。font.render()
返回一个曲面,您只能在blit()
时设置x
和{还有一句话:
有一些线状的压痕。在
您还必须在导入pygame之后的某处添加
timer = pygame.time.Clock()
。在相关问题 更多 >
编程相关推荐