Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import sys, time, random
- pygame.init()
- pygame.mixer.init()
- x = 50
- y = 50
- y1 = 0
- y2 = 0
- pSpeed = 6
- speed = 5
- aiSpeed = 5
- score = 0
- aiScore = 0
- windowSize = pygame.display.Info()
- f = 1
- flag = True
- screen = pygame.display.set_mode((windowSize.current_w, windowSize.current_h), pygame.FULLSCREEN)
- ballHit = pygame.mixer.Sound("ballHit.wav")
- ballBounce = pygame.mixer.Sound("ballBounce.wav")
- myriadProFont = pygame.font.SysFont("Myriad Pro", 48)
- directionX = 1
- directionY = 1
- clock = pygame.time.Clock()
- pygame.key.set_repeat(1, 10)
- pygame.mouse.set_visible(0)
- while flag:
- clock.tick(100)
- scoreCount = myriadProFont.render(str(score), 1, (255, 255, 255), (0, 0, 0))
- aiScoreCount = myriadProFont.render(str(aiScore), 1, (255, 255, 255), (0, 0, 0))
- keystate = pygame.key.get_pressed()
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- flag = False
- if y2 < y and y2 <= windowSize.current_h - 80:
- y2 += aiSpeed
- if y < y2 and y2 >= 0:
- y2 -= aiSpeed
- if keystate[pygame.K_w] and y1 >= 0:
- y1 -= pSpeed
- if keystate[pygame.K_s] and y1 <= windowSize.current_h - 80:
- y1 += pSpeed
- if keystate[pygame.K_r] and speed == 0:
- x = 50
- y = 50
- y1 = 0
- y2 = 0
- score = 0
- f = 1
- flag = True
- speed = 5
- directionX = 1
- directionY = 1
- aiScore = 0
- if keystate[pygame.K_e] and speed == 0:
- flag = False
- screen.fill((0, 0, 0))
- pygame.draw.rect(screen, (255, 255, 255), (screen.get_size()[0] / 2 - 5/2, 0, 5, windowSize.current_w))
- pygame.draw.rect(screen, (255, 0, 0), (x, y, 10, 10))
- pygame.draw.rect(screen, (255, 255, 255), (0, y1, 10, 80))
- pygame.draw.rect(screen, (255, 255, 255), (windowSize.current_w - 10, y2 - 30, 10, 80))
- size = (20, 20)
- x += speed * directionX
- y += speed * directionY
- if x == 10 and y > y1 and y < y1 + 80:
- directionX *= -1
- score += 1
- ballHit.play()
- if x < 0:
- screen.fill((0, 0, 0))
- finalScore = myriadProFont.render("Final Score: " + str(score), 1, (255, 255, 255), (0, 0, 0))
- retry = myriadProFont.render("Press 'r' to retry", 1, (255, 255, 255), (0, 0, 0))
- end = myriadProFont.render("Press 'e' to exit", 1, (255, 255, 255), (0, 0, 0))
- screen.blit(finalScore, (screen.get_size()[0] / 2 - finalScore.get_size()[0] / 2, 200))
- screen.blit(retry, (screen.get_size()[0] / 2 - retry.get_size()[0] / 2, 250))
- screen.blit(end, (screen.get_size()[0] / 2 - end.get_size()[0] / 2, 300))
- speed = 0
- if x >= windowSize.current_w - 10:
- directionX *= -1
- aiScore += 1
- ballHit.play()
- if y + size[1] > windowSize.current_h or y <= 0:
- directionY *= -1
- ballBounce.play()
- screen.blit(scoreCount, (screen.get_size()[0] / 2 - scoreCount.get_size()[0] / 2-20, 0))
- screen.blit(aiScoreCount, (screen.get_size()[0] / 2 - aiScoreCount.get_size()[0] / 2+20, 0))
- pygame.display.update()
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement