Friday, March 13, 2015

Graphic Dice - Homework 6 Finished

This week we had to turn in the homework for making a set of graphic dice using Pygame. We had to make a total of 3 files that would work. Below are the three files that I made: a class file, an interface, and the main game file.

# graphic_die.py
# Christian Munoz
# 03/13/2015
import pygame
import math
from pygame.locals import *
from random import randint
class graphic_die:
"""class that displays a graphical representation of a 6 sided die"""
def __init__(self, size, surface, position):
"""This function defines some values of the die and where things should display."""
self.SURF = surface
self.POS = position
self.VALUE = 1
self.SIZE = size
self.DIESURF = pygame.Surface((size, size), flags=SRCALPHA, depth=32)
self.DIESURF.fill((0, 0, 0, 0))
# RGB values for later use.
self.DIEIVORY = (230, 230, 200)
self.DIERED = (200, 0, 0)
self.PIPSBLK = (40, 40, 40)
self.PIPSWHT = (200, 200, 200)
self.RADIUS = self.SIZE//10
HSIZE = self.SIZE//2
QSIZE = self.SIZE//4
# create Pips/Dots in standard places
self.POINT1 = (HSIZE, HSIZE)
self.POINT2 = (QSIZE, QSIZE)
self.POINT3 = (HSIZE + QSIZE, HSIZE + QSIZE)
self.POINT4 = (HSIZE + QSIZE, QSIZE)
self.POINT5 = (QSIZE, HSIZE + QSIZE)
self.POINT6 = (QSIZE, HSIZE)
self.POINT7 = (HSIZE + QSIZE, HSIZE)
def draw_background(self):
"""Create square with rounded corners"""
pygame.draw.circle(self.DIESURF, self.DIEIVORY, (self.RADIUS, self.RADIUS), self.RADIUS)
pygame.draw.circle(self.DIESURF, self.DIEIVORY, (self.SIZE - self.RADIUS, self.RADIUS), self.RADIUS)
pygame.draw.circle(self.DIESURF, self.DIEIVORY, (self.RADIUS, self.SIZE - self.RADIUS), self.RADIUS)
pygame.draw.circle(self.DIESURF, self.DIEIVORY, (self.SIZE - self.RADIUS, self.SIZE - self.RADIUS), self.RADIUS)
pygame.draw.rect(self.DIESURF, self.DIEIVORY, Rect((self.RADIUS, 0), (self.SIZE - 2 * self.RADIUS, self.SIZE)))
pygame.draw.rect(self.DIESURF, self.DIEIVORY, Rect((0, self.RADIUS), (self.SIZE, self.SIZE - 2 * self.RADIUS)))
def make_pip(self, point):
"""Helper function to make pips."""
pip_big = pygame.draw.circle(self.DIESURF, self.PIPSBLK, point, self.RADIUS)
pip_medium = pygame.draw.circle(self.DIESURF, self.DIERED, point, (self.RADIUS -2))
pip_small = pygame.draw.circle(self.DIESURF, self.PIPSBLK, point, math.ceil(self.RADIUS/2))
return pip_big, pip_medium, pip_small
def set_random_value(self):
"""Returns a random value between 1-6."""
self.VALUE = randint(1, 6)
return self.VALUE
def draw_value(self, value):
"""Draws the right amount of pips depending on the value of the die."""
self.draw_background()
if value == 1:
pip1 = self.make_pip(self.POINT1)
elif value == 2:
pip2 = self.make_pip(self.POINT2)
pip3 = self.make_pip(self.POINT3)
elif value == 3:
pip1 = self.make_pip(self.POINT1)
pip2 = self.make_pip(self.POINT2)
pip3 = self.make_pip(self.POINT3)
elif value == 4:
pip2 = self.make_pip(self.POINT2)
pip3 = self.make_pip(self.POINT3)
pip4 = self.make_pip(self.POINT4)
pip5 = self.make_pip(self.POINT5)
elif value == 5:
pip1 = self.make_pip(self.POINT1)
pip2 = self.make_pip(self.POINT2)
pip3 = self.make_pip(self.POINT3)
pip4 = self.make_pip(self.POINT4)
pip5 = self.make_pip(self.POINT5)
elif value == 6:
pip2 = self.make_pip(self.POINT2)
pip3 = self.make_pip(self.POINT3)
pip4 = self.make_pip(self.POINT4)
pip5 = self.make_pip(self.POINT5)
pip6 = self.make_pip(self.POINT6)
pip7 = self.make_pip(self.POINT7)
def display_die(self):
self.draw_value(self.VALUE)
self.SURF.blit(self.DIESURF, self.POS)
# graphic_die_game.py
# Christian Munoz
# 03/13/2015
import pygame
import sys
from pygame.locals import *
from graphic_die_interface import graphic_die_interface
def main():
"""This function will run the die game."""
pygame.init()
game_interface = graphic_die_interface()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
game_interface.display_interface()
pygame.display.update()
if __name__ == "__main__":
main()
sys.exit()
# graphic_die_interface.py
# Christian Munoz
# 03/13/2015
import pygame
from pygame.locals import *
from graphic_die_class import graphic_die
class graphic_die_interface:
def __init__(self):
pygame.init()
#Sets the size of the screen and the dice that will be used.
self.screen_size = (1280, 720)
self.die_size = 100
self.half_x = (self.screen_size[0]/2) - (self.die_size/2)
self.half_y = (self.screen_size[1]/2) - (self.die_size/2)
#RGB colors for later use
self.black = (0, 0, 0)
self.white = (255, 255, 255)
self.display_surface = pygame.display.set_mode(self.screen_size)
pygame.display.set_caption('Dice Game')
self.font = pygame.font.SysFont("Broadway", 60)
self.text = "Christian's Dice Game"
self.label = self.font.render(self.text, 1, self.white)
self.die1 = graphic_die(self.die_size, self.display_surface, (self.half_x, self.half_y))
self.die1.set_random_value()
self.die2 = graphic_die(self.die_size, self.display_surface, ((self.half_x/2), self.half_y))
self.die2.set_random_value()
self.die3 = graphic_die(self.die_size, self.display_surface, ((self.half_x/2) + self.half_x, self.half_y))
self.die3.set_random_value()
self.die4 = graphic_die(self.die_size, self.display_surface, ((self.half_x/4) - self.die_size, self.half_y))
self.die4.set_random_value()
self.die5 = graphic_die(self.die_size, self.display_surface, (self.screen_size[0] - (self.die_size * 1.5), self.half_y))
self.die5.set_random_value()
def fill_gradient(self, surface, color, gradient, rect=None, vertical=True, forward=True):
"""fill a surface with a gradient pattern
Parameters:
color -> starting color
gradient -> final color
rect -> area to fill; default is surface's rect
vertical -> True=vertical; False=horizontal
forward -> True=forward; False=reverse
Pygame recipe: http://www.pygame.org/wiki/GradientCode"""
if rect is None: rect = surface.get_rect()
x1,x2 = rect.left, rect.right
y1,y2 = rect.top, rect.bottom
if vertical:
h = y2-y1
else:
h = x2-x1
if forward:
a, b = color, gradient
else:
b, a = color, gradient
rate = (
float(b[0]-a[0])/h,
float(b[1]-a[1])/h,
float(b[2]-a[2])/h
)
fn_line = pygame.draw.line
if vertical:
for line in range(y1,y2):
color = (
min(max(a[0]+(rate[0]*(line-y1)),0),255),
min(max(a[1]+(rate[1]*(line-y1)),0),255),
min(max(a[2]+(rate[2]*(line-y1)),0),255)
)
fn_line(surface, color, (x1,line), (x2,line))
else:
for col in range(x1,x2):
color = (
min(max(a[0]+(rate[0]*(col-x1)),0),255),
min(max(a[1]+(rate[1]*(col-x1)),0),255),
min(max(a[2]+(rate[2]*(col-x1)),0),255)
)
fn_line(surface, color, (col,y1), (col,y2))
def display_dice(self):
"""This function will display all dice."""
self.fill_gradient(self.display_surface, self.black, self.white)
self.die1.display_die()
self.die2.display_die()
self.die3.display_die()
self.die4.display_die()
self.die5.display_die()
def display_text(self):
"""This functions displays all the text on the game."""
self.display_surface.blit(self.label, (self.half_x - (len( self.text)/2), self.half_y/2))
def display_interface(self):
"""This functions displays everything that needs to go on the display surface."""
#self.display_text()
self.display_dice()

I will have to go back and fix some of the code since if I scale down the size of the window, the position of the dice breaks and it doesn't look like it should.

That's all for now, thanks for reading!

No comments:

Post a Comment