Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # ---------------------------------------------------------------------
- # pygame (simple) template with classes (by furas)
- # ---------------------------------------------------------------------
- #
- # previous version http://pastebin.com/cwt8CEyb
- #
- import pygame
- #import random
- # === CONSTANTS ===
- BLACK = ( 0, 0, 0)
- WHITE = (255, 255, 255)
- RED = (255, 0, 0)
- GREEN = ( 0, 255, 0)
- BLUE = ( 0, 0, 255)
- SCREEN_WIDTH = 800
- SCREEN_HEIGHT = 600
- BLOCK_SIZE = 50
- # === CLASSES ===
- # TODO: classes are very similar - for example all have the same `mainloop`.
- # TODO: create `Stage` class with `mainloop` and use `class inheritance`.
- # TODO: create `Button` class for buttons in menu
- # TODO: create `Config` class to have everything in one place (for example `screen`)
- class StartScreen():
- def __init__(self, screen):
- self.screen = screen
- self.screen_rect = self.screen.get_rect()
- self.font_30 = pygame.font.SysFont(None, 30)
- self.font_50 = pygame.font.SysFont(None, 50)
- self.text_start = self.font_50.render("INTRO", True, GREEN)
- self.text_start_rect = self.text_start.get_rect()
- # center on screen
- self.text_start_rect.center = self.screen_rect.center
- self.text_press = self.font_30.render("PRESS ANY KEY OR CLICK MOUSE", True, WHITE)
- self.text_press_rect = self.text_press.get_rect()
- # middle bottom screen
- self.text_press_rect.midbottom = self.screen_rect.midbottom
- def draw(self):
- self.screen.fill(BLACK)
- self.screen.blit(self.text_start, self.text_start_rect)
- self.screen.blit(self.text_press, self.text_press_rect)
- def update(self):
- # TODO: move object, etc. (without drawing)
- pass
- def event_handle(self, event):
- # press any key
- if event.type == pygame.KEYDOWN:
- self.running = False
- elif event.type == pygame.MOUSEBUTTONDOWN:
- self.running = False
- def mainloop(self):
- self.clock = pygame.time.Clock()
- self.running = True
- while self.running:
- # --- events ---
- for event in pygame.event.get():
- # --- global events ---
- if event.type == pygame.QUIT:
- self.running = False
- elif event.type == pygame.KEYDOWN:
- if event.key == pygame.K_ESCAPE:
- self.running = False
- # --- local events ---
- self.event_handle(event)
- # --- updates ---
- self.update()
- # --- draws ---
- self.draw()
- pygame.display.flip()
- # --- FPS ---
- self.clock.tick(25)
- return True
- class EndScreen():
- def __init__(self, screen):
- self.screen = screen
- self.screen_rect = self.screen.get_rect()
- self.font_30 = pygame.font.SysFont(None, 30)
- self.font_50 = pygame.font.SysFont(None, 50)
- self.text = self.font_50.render("THE END", True, RED)
- self.text_rect = self.text.get_rect()
- # center on screen
- self.text_rect.center = self.screen_rect.center
- self.text_press = self.font_30.render("PRESS ANY KEY OR CLICK MOUSE", True, WHITE)
- self.text_press_rect = self.text_press.get_rect()
- # middle bottom screen
- self.text_press_rect.midbottom = self.screen_rect.midbottom
- def draw(self):
- self.screen.fill(BLACK)
- self.screen.blit(self.text, self.text_rect)
- self.screen.blit(self.text_press, self.text_press_rect)
- def update(self):
- # TODO: move object, etc. (without drawing)
- pass
- def event_handle(self, event):
- if event.type == pygame.KEYDOWN:
- self.running = False
- elif event.type == pygame.MOUSEBUTTONDOWN:
- self.running = False
- def mainloop(self):
- self.clock = pygame.time.Clock()
- self.running = True
- while self.running:
- # --- events ---
- for event in pygame.event.get():
- # --- global events ---
- if event.type == pygame.QUIT:
- self.running = False
- elif event.type == pygame.KEYDOWN:
- if event.key == pygame.K_ESCAPE:
- self.running = False
- # --- local events ---
- self.event_handle(event)
- # --- updates ---
- self.update()
- # --- draws ---
- self.draw()
- pygame.display.flip()
- # --- FPS ---
- self.clock.tick(25)
- return True
- class MainMenu():
- def __init__(self, screen):
- self.screen = screen
- self.screen_rect = self.screen.get_rect()
- self.font_30 = pygame.font.SysFont(None, 30)
- self.font_50 = pygame.font.SysFont(None, 50)
- # --- buttons --- TODO: create Button Class
- self.text_game = self.font_50.render("GAME", True, RED)
- self.text_game_rect = self.text_game.get_rect()
- self.text_game_rect.centerx = self.screen_rect.centerx
- self.text_game_rect.centery = self.screen_rect.centery - 100
- self.text_options = self.font_50.render("OPTIONS", True, RED)
- self.text_options_rect = self.text_options.get_rect()
- self.text_options_rect.center = self.screen_rect.center
- self.text_exit = self.font_50.render("EXIT", True, RED)
- self.text_exit_rect = self.text_exit.get_rect()
- self.text_exit_rect.centerx = self.screen_rect.centerx
- self.text_exit_rect.centery = self.screen_rect.centery + 100
- # --- rect of hovered button
- self.hover_rect = None
- # --- game and option "stage"
- self.game_stage = Game(screen) # Game(screen, config)
- self.options_stage = OptionsMenu(screen) # OptionsMenu(screen, config)
- def on_game(self):
- self.game_stage.mainloop()
- def on_options(self):
- self.options_stage.mainloop()
- def on_exit(self):
- self.running = False
- def draw(self):
- self.screen.fill(BLACK)
- # highlight button
- if self.hover_rect:
- pygame.draw.rect(self.screen, WHITE, self.hover_rect)
- self.screen.blit(self.text_game, self.text_game_rect)
- self.screen.blit(self.text_options, self.text_options_rect)
- self.screen.blit(self.text_exit, self.text_exit_rect)
- def update(self):
- # TODO: move object, etc. (without drawing)
- pass
- def event_handle(self, event):
- # check mouse position with text rect
- if event.type == pygame.MOUSEMOTION:
- if self.text_game_rect.collidepoint(event.pos):
- self.hover_rect = self.text_game_rect
- elif self.text_options_rect.collidepoint(event.pos):
- self.hover_rect = self.text_options_rect
- elif self.text_exit_rect.collidepoint(event.pos):
- self.hover_rect = self.text_exit_rect
- else:
- self.hover_rect = None
- # click button
- elif event.type == pygame.MOUSEBUTTONDOWN:
- if event.button == 1: # left mouse button
- if self.text_game_rect.collidepoint(event.pos):
- # go to game
- self.on_game()
- elif self.text_options_rect.collidepoint(event.pos):
- # go to options
- self.on_options()
- elif self.text_exit_rect.collidepoint(event.pos):
- # exit
- self.on_exit()
- def mainloop(self):
- self.clock = pygame.time.Clock()
- self.running = True
- while self.running:
- # --- events ---
- for event in pygame.event.get():
- # --- global events ---
- if event.type == pygame.QUIT:
- self.running = False
- elif event.type == pygame.KEYDOWN:
- if event.key == pygame.K_ESCAPE:
- self.running = False
- # --- local events ---
- self.event_handle(event)
- # --- updates ---
- self.update()
- # --- draws ---
- self.draw()
- pygame.display.flip()
- # --- FPS ---
- self.clock.tick(25)
- return True
- class OptionsMenu():
- def __init__(self, screen):
- self.screen = screen
- self.screen_rect = self.screen.get_rect()
- self.font_30 = pygame.font.SysFont(None, 30)
- self.font_50 = pygame.font.SysFont(None, 50)
- # --- buttons --- TODO: create Button Class
- self.text_display = self.font_50.render("DISPLAY", True, RED)
- self.text_display_rect = self.text_display.get_rect()
- self.text_display_rect.centerx = self.screen_rect.centerx
- self.text_display_rect.centery = self.screen_rect.centery - 100
- self.text_music = self.font_50.render("SOUND", True, RED)
- self.text_music_rect = self.text_music.get_rect()
- self.text_music_rect.center = self.screen_rect.center
- self.text_exit = self.font_50.render("BACK", True, RED)
- self.text_exit_rect = self.text_exit.get_rect()
- self.text_exit_rect.centerx = self.screen_rect.centerx
- self.text_exit_rect.centery = self.screen_rect.centery + 100
- # --- rect of hovered button
- self.hover_rect = None
- # --- game and option "stage"
- self.game_stage = Game(screen)
- #self.options_stage = OptionsMenu(screen)
- def on_display(self):
- pass
- def on_music(self):
- pass
- def on_exit(self):
- self.running = False
- def draw(self):
- self.screen.fill(BLACK)
- # highlight button
- if self.hover_rect:
- pygame.draw.rect(self.screen, WHITE, self.hover_rect)
- self.screen.blit(self.text_display, self.text_display_rect)
- self.screen.blit(self.text_music, self.text_music_rect)
- self.screen.blit(self.text_exit, self.text_exit_rect)
- def update(self):
- # TODO: move object, etc. (without drawing)
- pass
- def event_handle(self, event):
- # check mouse position with text rect
- if event.type == pygame.MOUSEMOTION:
- if self.text_display_rect.collidepoint(event.pos):
- self.hover_rect = self.text_display_rect
- elif self.text_music_rect.collidepoint(event.pos):
- self.hover_rect = self.text_music_rect
- elif self.text_exit_rect.collidepoint(event.pos):
- self.hover_rect = self.text_exit_rect
- else:
- self.hover_rect = None
- # click button
- elif event.type == pygame.MOUSEBUTTONDOWN:
- if event.button == 1: # left mouse button
- if self.text_display_rect.collidepoint(event.pos):
- # go to game
- self.on_display()
- elif self.text_music_rect.collidepoint(event.pos):
- # go to options
- self.on_music()
- elif self.text_exit_rect.collidepoint(event.pos):
- # exit
- self.on_exit()
- def mainloop(self):
- self.clock = pygame.time.Clock()
- self.running = True
- while self.running:
- # --- events ---
- for event in pygame.event.get():
- # --- global events ---
- if event.type == pygame.QUIT:
- self.running = False
- elif event.type == pygame.KEYDOWN:
- if event.key == pygame.K_ESCAPE:
- self.running = False
- # --- local events ---
- self.event_handle(event)
- # --- updates ---
- self.update()
- # --- draws ---
- self.draw()
- pygame.display.flip()
- # --- FPS ---
- self.clock.tick(25)
- return True
- class Game():
- def __init__(self, screen):
- self.screen = screen
- self.screen_rect = self.screen.get_rect()
- self.player = pygame.Surface((BLOCK_SIZE, BLOCK_SIZE))
- self.player.fill(GREEN)
- self.player_rect = self.player.get_rect()
- self.player_rect.center = self.screen_rect.center
- self.player_move_x = 0
- self.player_move_y = 0
- self.font_30 = pygame.font.SysFont(None, 30)
- self.text_press = self.font_30.render("PRESS 'ESCAPE' TO EXIT", True, WHITE)
- self.text_press_rect = self.text_press.get_rect()
- # middle bottom screen
- self.text_press_rect.midbottom = self.screen_rect.midbottom
- self.text_arrows = self.font_30.render("USE 'ARROWS' OR 'CLICK' TO MOVE OBJECT", True, WHITE)
- self.text_arrows_rect = self.text_arrows.get_rect()
- # middle top screen
- self.text_arrows_rect.midtop = self.screen_rect.midtop
- def draw(self):
- self.screen.fill(BLACK)
- self.screen.blit(self.player, self.player_rect)
- self.screen.blit(self.text_press, self.text_press_rect)
- self.screen.blit(self.text_arrows, self.text_arrows_rect)
- def update(self):
- # --- updates ---
- self.player_rect.x += self.player_move_x
- self.player_rect.y += self.player_move_y
- self.player_rect.clamp_ip(self.screen_rect)
- def event_handle(self, event):
- # --- player events ---
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_LEFT:
- self.player_move_x -= 10
- elif event.key == pygame.K_RIGHT:
- self.player_move_x += 10
- elif event.key == pygame.K_UP:
- self.player_move_y -= 10
- elif event.key == pygame.K_DOWN:
- self.player_move_y += 10
- elif event.type == pygame.KEYUP:
- if event.key == pygame.K_LEFT:
- self.player_move_x += 10
- elif event.key == pygame.K_RIGHT:
- self.player_move_x -= 10
- elif event.key == pygame.K_UP:
- self.player_move_y += 10
- elif event.key == pygame.K_DOWN:
- self.player_move_y -= 10
- elif event.type == pygame.MOUSEBUTTONDOWN:
- self.player_rect.center = event.pos
- def mainloop(self):
- self.clock = pygame.time.Clock()
- self.running = True
- while self.running:
- # --- events ---
- for event in pygame.event.get():
- # --- global events ---
- if event.type == pygame.QUIT:
- self.running = False
- elif event.type == pygame.KEYDOWN:
- if event.key == pygame.K_ESCAPE:
- self.running = False
- # --- local events ---
- self.event_handle(event)
- # --- updates ---
- self.update()
- # --- draws ---
- self.draw()
- pygame.display.flip()
- # --- FPS ---
- self.clock.tick(25)
- return True
- # === FUNCTIONS ===
- # empty
- # === MAIN ===
- # --- init (only once for all classes) ---
- pygame.init()
- screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
- # --- states (almost "finite states machine" [FSM])---
- result = StartScreen(screen).mainloop()
- if result:
- result = MainMenu(screen).mainloop()
- if result:
- result = EndScreen(screen).mainloop()
- # --- the end (only once for all classes) ---
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement