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 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 paddle bounce if picy + pich >=paddley , picy + pich <=paddley + paddleh \ , speedy > 0: if picx +picw / 2 >= paddlex , picx +picw / 2 <= paddlex + \ paddlew: points += 1 speedy = -speedy # draw text on screen draw_screen = "lives: " + str(lives) + "points: " + str(points) # check whether game on if lives < 1: speedx = speedy = 0 draw_string = "game over.your score was: " + str(points) draw_string += ". press f1 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
this pong game using of book, wanted test out pygame screen went blank, searched if had same problem had different answers couldn't fix it.i added few parts if think dont need them or want me add think should do. if there suggestions please answer, recommendations answer.
you have indent code line picx += speedx
, line before pygame.quit()
you have add in pygame.display.flip()
before line pygame.quit()
(and indent it!)
then line text.rect.y = 10
not make sense. font.render()
returns surface , can set x
, y
positions when blit()
.
also line:
draw_string = "game over.your score was: " + str(points) draw_string += ". press f1 play again. "
has wierd indentation.
you have add timer = pygame.time.clock()
somewhere after import of pygame.