Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import random
- import sys
- SCREEN_SIZE = SCREEN_WIDTH, SCREEN_HEIGHT = 640, 480
- BLACK = (0, 0, 0)
- WHITE = (255, 255, 255)
- RED = (255, 0, 0)
- class PongGame():
- def __init__(self):
- self.screen = pygame.display.set_mode(SCREEN_SIZE)
- pygame.display.set_caption('Pong')
- self.FPS = 60
- self.fps_clock = pygame.time.Clock()
- self.background = pygame.Surface(self.screen.get_size())
- self.background.fill(BLACK)
- self.main_menu = MyGameMenu()
- self.main_menu.add_option("Start")
- self.main_menu.add_option("Quit")
- self.pause_menu = MyGameMenu()
- self.pause_menu.add_option("Continue")
- self.pause_menu.add_option("Main menu")
- self.paddle_img = pygame.image.load("paddle.png").convert()
- self.ball_img = pygame.image.load("ball.png").convert()
- self.border_img = pygame.image.load('strips.png').convert_alpha()
- self.score1 = 0
- self.score2 = 0
- self.player1 = None
- self.player2 = None
- self.ball = None
- self.showing_main_menu = True
- self.playing = False
- self.paused = False
- def start(self):
- self.paused = False
- self.playing = True
- self.score1 = 0
- self.score2 = 0
- self.player1 = Paddle(self.screen, self.paddle_img, 60, SCREEN_HEIGHT/2, 0, 350)
- self.player2 = Paddle(self.screen, self.paddle_img, SCREEN_WIDTH - 80, SCREEN_HEIGHT/2, 0, 350)
- self.ball = Ball(self.screen, self.ball_img, SCREEN_WIDTH/2, SCREEN_HEIGHT/2, 250, 250)
- self.ball.stuck = True
- self.game_objects = [self.player1, self.player2, self.ball]
- def check_intersection(self, rect1, rect2):
- return rect1.x + rect1.w > rect2.x \
- and rect1.x < rect2.x + rect2.w \
- and rect1.y + rect1.h > rect2.y \
- and rect1.y < rect2.y + rect2.h
- def get_intersection_rect(self, rect1, rect2):
- x_min, x_max, y_min, y_max = 0, 0, 0, 0
- if rect1.x > rect2.x:
- x_min = rect1.x
- else:
- x_min = rect2.x
- if rect1.x + rect1.w < rect2.x + rect2.w:
- x_max = rect1.x + rect1.w
- else:
- x_max = rect2.x + rect2.w
- if rect1.y > rect2.y:
- y_min = rect1.y
- else:
- y_min = rect2.y
- if rect1.y + rect1.h < rect2.y + rect2.h:
- y_max = rect1.y + rect1.h
- else:
- y_max = rect2.y + rect2.h
- width = x_max - x_min
- height = y_max - y_min
- return x_min, y_min, width, height
- def handle_collision(self, obj1, obj2):
- rect1 = obj1.rect
- rect2 = obj2.rect
- if not self.check_intersection(rect1, rect2):
- return
- rect = pygame.Rect(self.get_intersection_rect(rect1, rect2))
- if rect.w > rect.h:
- if rect1.y < rect2.y:
- obj2.rect.y += rect.h
- else:
- obj2.rect.y -= rect.h
- obj2.dy = -obj2.dy
- else:
- if rect1.x < rect2.x:
- obj2.rect.x += rect.w
- else:
- obj2.rect.x -= rect.w
- obj2.dx = -obj2.dx
- def main_loop(self):
- fps_clock = pygame.time.Clock()
- while True:
- dt = fps_clock.tick(self.FPS) / 1000.0
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- return
- elif event.type == pygame.KEYDOWN:
- if event.key == pygame.K_ESCAPE:
- if self.showing_main_menu:
- return
- elif self.paused:
- self.paused = False
- self.playing = True
- elif self.playing:
- self.playing = False
- self.paused = True
- elif event.key == pygame.K_SPACE:
- if self.playing:
- if self.ball.stuck:
- self.ball.dx *= random.choice([-1, 1])
- self.ball.dy *= random.choice([-1, 1])
- self.ball.stuck = not self.ball.stuck
- elif self.showing_main_menu:
- if self.main_menu.option_idx == 0:
- self.showing_main_menu = False
- self.start()
- elif self.main_menu.option_idx == 1:
- return
- elif self.paused:
- if self.pause_menu.option_idx == 0:
- self.paused = False
- self.playing = True
- elif self.pause_menu.option_idx == 1:
- self.paused = False
- self.playing = False
- self.showing_main_menu = True
- elif event.key == pygame.K_DOWN:
- if self.showing_main_menu:
- self.main_menu.move_up()
- elif self.paused:
- self.pause_menu.move_up()
- elif event.key == pygame.K_UP:
- if self.showing_main_menu:
- self.main_menu.move_down()
- elif self.paused:
- self.pause_menu.move_down()
- if self.playing:
- keys = pygame.key.get_pressed()
- if keys[pygame.K_UP]:
- self.player1.rect.y += -self.player1.dy * dt
- if keys[pygame.K_DOWN]:
- self.player1.rect.y += self.player1.dy * dt
- if keys[pygame.K_w]:
- self.player2.rect.y += -self.player1.dy * dt
- if keys[pygame.K_s]:
- self.player2.rect.y += self.player1.dy * dt
- if self.ball.rect.x < 0:
- self.score2 += 1
- elif self.ball.rect.x + self.ball.rect.w > SCREEN_WIDTH:
- self.score1 += 1
- for obj in self.game_objects:
- obj.update(dt)
- self.handle_collision(self.player1, self.ball)
- self.handle_collision(self.player2, self.ball)
- self.screen.blit(self.background, (0, 0))
- self.screen.blit(self.player1.img, self.player1.rect)
- self.screen.blit(self.player2.img, self.player2.rect)
- self.screen.blit(self.ball.img, self.ball.rect)
- self.screen.blit(draw_text(str(self.score1), 'notalot35.ttf', WHITE, 72), (200, 50))
- self.screen.blit(draw_text(str(self.score2), 'notalot35.ttf', (255, 255, 255), 72), (SCREEN_WIDTH - 200, 50))
- self.screen.blit(self.border_img, (SCREEN_WIDTH/2, 10))
- elif self.paused:
- self.screen.blit(self.background, (0, 0))
- self.screen.blit(draw_text("PAUSED", "notalot35.ttf", WHITE, 72), (SCREEN_WIDTH/2 - 80, 40))
- self.pause_menu.display(self.screen)
- elif self.showing_main_menu:
- self.screen.blit(self.background, (0, 0))
- title_surf = draw_text("PONG GAME ver 0.1", "notalot35.ttf", WHITE, 72)
- self.screen.blit(title_surf, (SCREEN_WIDTH/2 - title_surf.get_rect().w/2, 40 - title_surf.get_rect().h/2))
- self.main_menu.display(self.screen)
- pygame.display.flip()
- def draw_text(message, font_name, color, size):
- font = pygame.font.Font(font_name, size)
- text = font.render(message, False, color)
- text = text.convert_alpha()
- return text
- class GameObject():
- def __init__(self, screen, img, x, y, dx, dy):
- self.screen = screen
- self.img = img
- self.dx = dx
- self.dy = dy
- self.rect = img.get_rect()
- self.rect.x = x
- self.rect.y = y
- def update(self, dt): pass
- class Ball(GameObject):
- def __init__(self, screen, img, x, y, dx, dy):
- GameObject.__init__(self, screen, img, x, y, dx, dy)
- self.stuck = True
- def update(self, dt):
- if self.stuck:
- return
- if self.rect.x < 0 or self.rect.x + self.rect.w > self.screen.get_width():
- self.rect.x, self.rect.y = self.screen.get_width()/2, self.screen.get_height()/2
- self.stuck = True
- return
- if self.rect.y < 0:
- self.rect.y = 0
- self.dy *= -1
- elif self.rect.y + self.rect.h > self.screen.get_height():
- self.rect.y = self.screen.get_height() - self.rect.h
- self.dy *= -1
- self.rect.x += self.dx * dt
- self.rect.y += self.dy * dt
- class Paddle(GameObject):
- def __init__(self, screen, img, x, y, dx, dy):
- GameObject.__init__(self, screen, img, x, y, dx, dy)
- def update(self, dt):
- if self.rect.y < 0:
- self.rect.y = 0
- elif self.rect.y + self.rect.h > SCREEN_HEIGHT:
- self.rect.y = SCREEN_HEIGHT - self.rect.h
- class MyGameMenu():
- def __init__(self):
- self.options = []
- self.option_idx = -1
- def add_option(self, text):
- if not len(self.options):
- self.option_idx = 0
- self.options.append(text)
- def move_up(self):
- if self.option_idx < len(self.options) - 1:
- self.option_idx += 1
- def move_down(self):
- if self.option_idx > 0:
- self.option_idx -= 1
- def display(self, screen):
- for i, option in enumerate(self.options):
- clr = WHITE
- if i == self.option_idx:
- clr = RED
- text_surf = draw_text(str(option), 'notalot35.ttf', clr, 72)
- screen.blit(text_surf, (screen.get_width()/2 - text_surf.get_rect().w/2,
- screen.get_height()/2 - text_surf.get_rect().h/2 + i*72 - 40))
- def main():
- pygame.init()
- PongGame().main_loop()
- pygame.quit()
- sys.exit()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement