Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Exemplar Graphics-5
- # This is an example program that contains graphics using
- # the pygame module, contains user-defined classes, and
- # responds to multiple kinds of events.
- # It contains these kinds of statements: expression, assignment,
- # import, function definition, while, for, if, return, class
- # definition
- # It contains these kinds of expressions: identifier, literal,
- # attribute reference, function call, binary operator, expression
- # list
- # It uses these types: str, int, float, bool, NoneType, function,
- # module, tuple, pygame.Color, pygame.Rect, Game,
- # Circle
- from uagame import Window
- import pygame, time
- from pygame.locals import *
- # User-defined functions
- def main():
- window = Window('ur mom', 500, 400)
- window.set_auto_update(False)
- game = Game(window)
- game.play()
- window.close()
- # User-defined classes
- class Tile:
- def __init__(self, spot, window, x, y, width, height):
- self.spot = spot
- self.window = window
- self.x_coor = x
- self.y_coor = y
- self.width = width
- self.height = height
- self.color = pygame.Color('white')
- self.border = 3
- self.clicked = False
- def draw(self):
- self.rect = pygame.draw.rect(self.window.get_surface(), self.color, [self.x_coor, self.y_coor, self.width, self.height],self.border)
- def click(self, mouse_pos):
- self.mouse_pos = mouse_pos
- thisissomethingrandom = self.rect.collidepoint(self.mouse_pos)
- print(type(True))
- #print(typing)
- #print(str(self.mouse_pos))
- class Game:
- # An object in this class represents a complete game.
- def __init__(self, window):
- # Initialize a Game.
- # - self is the Game to initialize
- # - window is the uagame window object
- self.window = window
- self.bg_color = pygame.Color('blue')
- self.fg_color_str = "green"
- self.pause_time = 0.04 # smaller is faster game
- self.close_clicked = False
- self.continue_game = True
- self.window.set_bg_color('black')
- self.blah_pos = 0
- self.viewablah = False
- #Tiles
- self.grid_size = 3
- self.which_tile = 0
- self.wind_x = window.get_width()
- self.wind_y = window.get_height()
- self.put_row = []
- tiles_width = self.wind_x // self.grid_size
- tiles_height = self.wind_y // self.grid_size
- #Tile(self, spot, window, x, y, width, height):
- for column_spot in range (0, self.grid_size):
- tiles_y = (tiles_height)*column_spot
- for row_spot in range(0, self.grid_size):
- tiles_x = (tiles_width)*row_spot
- self.the_tile = Tile(row_spot, self.window, tiles_x , tiles_y, tiles_width, tiles_height)
- self.put_row.append(self.the_tile)
- def play(self):
- # Play the game until the player presses the close box.
- # - self is the Game that should be continued or not.
- while not self.close_clicked: # until player clicks close box
- # play frame
- self.handle_event()
- self.draw()
- if self.continue_game:
- self.update()
- self.decide_continue()
- time.sleep(self.pause_time) # set game velocity by pausing
- def handle_event(self):
- # Handle each user event by changing the game state
- # appropriately.
- # - self is the Game whose events will be handled
- event = pygame.event.poll()
- if event.type == QUIT:
- self.close_clicked = True
- elif event.type == MOUSEBUTTONUP and self.continue_game:
- self.handle_mouse_up()
- def draw(self):
- # Draw all game objects.
- # - self is the Game to draw
- self.window.clear()
- if self.viewablah == True:
- self.window.draw_string('blah', self.blah_pos, self.blah_pos)
- for tiles in range (0, (self.grid_size*self.grid_size)):
- self.the_thingy = self.put_row[tiles]
- Tile.draw(self.the_thingy)
- if not self.continue_game:
- # Perform appropriate game over actions
- self.window.clear()
- self.window.update()
- def handle_mouse_up(self):
- # Respond to the player releasing the mouse button by
- # taking appropriate actions.
- # - self is the Game where the mouse up occurred
- where_mouse = pygame.mouse.get_pos()
- for n in range(0,9):
- this = self.put_row[n]
- this.click(where_mouse)
- pass
- def update(self):
- # Update the game objects.
- # - self is the Game to update
- self.elapsed_time = pygame.time.get_ticks() // 1000
- def decide_continue(self):
- # Check and remember if the game should continue
- # - self is the Game to check
- pass
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement