Facebook
From Gamboge Rhinoceros, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 71
  1. import sys
  2. import config
  3. import pygame
  4. from sprites import *
  5.  
  6. def Button(screen, position, text, button_size=(200, 50)):
  7.         left, top = position
  8.         bwidth, bheight = button_size
  9.         pygame.draw.line(screen, (150, 150, 150), (left, top), (left+bwidth, top), 5)
  10.         pygame.draw.line(screen, (150, 150, 150), (left, top-2), (left, top+bheight), 5)
  11.         pygame.draw.line(screen, (50, 50, 50), (left, top+bheight), (left+bwidth, top+bheight), 5)
  12.         pygame.draw.line(screen, (50, 50, 50), (left+bwidth, top+bheight), (left+bwidth, top), 5)
  13.         pygame.draw.rect(screen, (100, 100, 100), (left, top, bwidth, bheight))
  14.         font = pygame.font.Font(config.FONTPATH, 30)
  15.         text_render = font.render(text, 1, (255, 235, 205))
  16.         return screen.blit(text_render, (left+50, top+10))
  17.  
  18. def startInterface(screen):
  19.         clock = pygame.time.Clock()
  20.         while True:
  21.                 screen.fill((41,36,33))
  22.                 button_1 = Button(screen,(150,175), '1 Gracz')
  23.                 button_2 = Button(screen, (150, 175), '2 Graczy')
  24.                 for event in pygame.event.get():
  25.                         if event.type == pygame.QUIT:
  26.                                 pygame.quit()
  27.                                 sys.exit()
  28.                         if event.type == pygame.MOUSEBUTTONDOWN:
  29.                                 if button_1.collidepoint(pygame.mouse.get_pos()):
  30.                                         return 1
  31.                                 elif button_2.collidepoint(pygame.mouse.get_pos()):
  32.                                         return 2
  33.                 clock.tick(10)
  34.                 pygame.display.update()
  35.  
  36. def endInterface(screen,score_left,score_right):
  37.         clock = pygame.time.Clock()
  38.         font1 = pygame.font.Font(config.FONTPATH,30)
  39.         font2 = pygame.font.Font(config.FONTPATH, 20)
  40.         msg = 'Gracz po lewej wygrał!' if score_left>score_right else 'Gracz po prawej wygrał!'
  41.         texts = [font1.render(msg,True,config.WHITE),
  42.                          font2.render('Wciśnij ESC żeby wyjść.',True,config.WHITE),
  43.                          font2.render(('Wciśnij Enter żeby grać ponownie.',True, config.WHITE))]
  44.         position = [[120,200],[155,270],[80,300]]
  45.         while True:
  46.                 screen.fill((41,36,33))
  47.                 for event in pygame.event.get():
  48.                         if event.type == pygame.QUIT:
  49.                                 pygame.quit()
  50.                                 sys.exit()
  51.                         if event.type == pygame.KEYDOWN:
  52.                                 if event.key == pygame.K_RETURN:
  53.                                         return
  54.                                 elif event.key == pygame.K_ESCAPE:
  55.                                         sys.exit()
  56.                                         pygame.quit()
  57.                 for text, pos in zip(texts, positions):
  58.                         screen.blit(text,pos)
  59.                 clock.tick(10)
  60.                 pygame.display.update()
  61.  
  62. def runDemo(screen):
  63.         hit_sound = pygame.mixer.Sound(config.HITSOUNDPATH)
  64.         goal_sound = pygame.mixer.Sound(config.GOALSOUNDPATH)
  65.         pygame.mixer.music.load(config.BGMPATH)
  66.         pygame.mixer.music.play(-1,0.0)
  67.         font = pygame.font.Font(config.FONTPATH, 50)
  68.         game_mode = startInterface(screen)
  69.         score_left = 0
  70.         racket_left = Racket(config.RACKETPICPATH, 'LEFT')
  71.         score_right = 0
  72.         racket_right = Racket(config.RACKETPICPATH, 'RIGHT')
  73.         ball = Ball(config.BALLPICPATH)
  74.         clock = pygame.time.Clock()
  75.         while True:
  76.                 for event in pygame.event.get():
  77.                         if event.type == pygame.QUIT:
  78.                                 pygame.quit()
  79.                                 sys.exit(-1)
  80.                 screen.fill((41,36,33))
  81.                 pressed_keys = pygame.key.get_pressed()
  82.                 if pressed_keys[pygame.K_UP]:
  83.                         racket_right.move('UP')
  84.                 elif pressed_keys[pygame.K_DOWN]:
  85.                         racket_right.move('Down')
  86.                 if game_mode == 2:
  87.                         if pressed_keys[pygame.K_w]:
  88.                                 racket_left.move('UP')
  89.                         elif pressed_keys[pygame.K_s]:
  90.                                 racket_left.move('Down')
  91.                 else:
  92.                         racket_left.automove(ball)
  93.  
  94.                 scores = ball.move(ball, racket_left, racket_right, hit_sound, goal_sound)
  95.                 score_left += scores[0]
  96.                 score_right += scores[1]
  97.                 pygame.draw.rect(screen, config.WHITE, (247,0,6,500))
  98.                 ball.draw(screen)
  99.                 racket_left.draw(screen)
  100.                 racket_right(screen)
  101.                 screen.blit(font.render(str(score_left),False,config.WHITE), (150,10))
  102.                 screen.blit(font.render(str(score_right), False, config.WHITE), (300, 10))
  103.                 if score_left == 11 or score_right == 11:
  104.                         return score_left, score_right
  105.                 clock.tick(100)
  106.                 pygame.display.update()
  107.  
  108. def main():
  109.         pygame.init()
  110.         pygame.mixer.init()
  111.         screen = pygame.display.set_mode((config.WIDTH, config.HEIGHT))
  112.         pygame.display.set_caption(('PONG - v.0.1'))
  113.         while True:
  114.                 score_left, score_right = runDemo()
  115.                 endInterface(screen,score_left,score_right)
  116.  
  117. if __name__ == '__main__':
  118.         main()