Skip to content

Commit

Permalink
- Created a simple 3x3 tttoe grid with highlighting of squares and ac…
Browse files Browse the repository at this point in the history
…tion on click by using pygame tutorial examples

- Moved all tutorial code in gui_ex.py
  • Loading branch information
AngelVI13 committed Apr 26, 2019
1 parent 1caf328 commit 0e99c51
Show file tree
Hide file tree
Showing 2 changed files with 282 additions and 132 deletions.
230 changes: 98 additions & 132 deletions gui.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
# tutorial from https://pythonprogramming.net/drawing-objects-pygame-tutorial/?completed=/displaying-text-pygame-screen/
from functools import partial

import pygame
import time
import random


RED_RECT = 'ff5e58'
RED_RECT_HIGHLIGHT = 'ffdbd4'
BLUE_RECT = '3bd6ff'
FORCED_BOARD = 'ffffcb'
# RED_RECT = 'ff5e58'
# RED_RECT_HIGHLIGHT = 'ffdbd4'
# BLUE_RECT = '3bd6ff'
# FORCED_BOARD = 'ffffcb'

pygame.init()

display_width = 800
display_height = 600
display_width = 480
display_height = 480

black = (0, 0, 0)
white = (255, 255, 255)
Expand All @@ -25,51 +26,47 @@

block_color = (53, 115, 255)

car_width = 73
# car_width = 73

gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('A bit Racey')
clock = pygame.time.Clock()

carImg = pygame.image.load('racecar.png')


def things_dodged(count):
font = pygame.font.SysFont(None, 25)
text = font.render("Dodged: " + str(count), True, black)
gameDisplay.blit(text, (0, 0))


def things(thingx, thingy, thingw, thingh, color):
pygame.draw.rect(gameDisplay, color, [thingx, thingy, thingw, thingh])


def car(x, y):
gameDisplay.blit(carImg, (x, y))


# carImg = pygame.image.load('racecar.png')


# def things_dodged(count):
# font = pygame.font.SysFont(None, 25)
# text = font.render("Dodged: " + str(count), True, black)
# gameDisplay.blit(text, (0, 0))
#
#
# def things(thingx, thingy, thingw, thingh, color):
# pygame.draw.rect(gameDisplay, color, [thingx, thingy, thingw, thingh])
#
#
# def car(x, y):
# gameDisplay.blit(carImg, (x, y))
#
#
def text_objects(text, font):
textSurface = font.render(text, True, black)
return textSurface, textSurface.get_rect()


def message_display(text):
largeText = pygame.font.Font('freesansbold.ttf', 115)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((display_width / 2), (display_height / 2))
gameDisplay.blit(TextSurf, TextRect)

pygame.display.update()

time.sleep(2)

game_loop()


def crash():
message_display('You Crashed')


#
#
# def message_display(text):
# largeText = pygame.font.Font('freesansbold.ttf', 115)
# TextSurf, TextRect = text_objects(text, largeText)
# TextRect.center = ((display_width / 2), (display_height / 2))
# gameDisplay.blit(TextSurf, TextRect)
#
# pygame.display.update()
#
#
# def crash():
# message_display('You Crashed')
#
#
def button(msg, x, y, w, h, ic, ac, action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
Expand All @@ -87,98 +84,67 @@ def button(msg, x, y, w, h, ic, ac, action=None):
textRect.center = ((x + (w / 2)), (y + (h / 2)))
gameDisplay.blit(textSurf, textRect)


def game_intro():
intro = True

while intro:
for event in pygame.event.get():
# print(event)
if event.type == pygame.QUIT:
pygame.quit()
quit()

gameDisplay.fill(white)
largeText = pygame.font.SysFont("comicsansms", 115)
TextSurf, TextRect = text_objects("A bit Racey", largeText)
TextRect.center = ((display_width / 2), (display_height / 2))
gameDisplay.blit(TextSurf, TextRect)

button("GO!", 150, 450, 100, 50, green, bright_green, game_loop)
button("Quit", 550, 450, 100, 50, red, bright_red, quitgame)

pygame.display.update()
clock.tick(15)

def quitgame():
pygame.quit()
quit()

def game_loop():
x = (display_width * 0.45)
y = (display_height * 0.8)

x_change = 0

thing_startx = random.randrange(0, display_width)
thing_starty = -600
thing_speed = 4
thing_width = 100
thing_height = 100

thingCount = 1

dodged = 0

gameExit = False

while not gameExit:

for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()

if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
if event.key == pygame.K_RIGHT:
x_change = 5

if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0

x += x_change
gameDisplay.fill(white)

things(thing_startx, thing_starty, thing_width, thing_height, block_color)

thing_starty += thing_speed
car(x, y)
things_dodged(dodged)

if x > display_width - car_width or x < 0:
crash()

if thing_starty > display_height:
thing_starty = 0 - thing_height
thing_startx = random.randrange(0, display_width)
dodged += 1
thing_speed += 1
thing_width += (dodged * 1.2)

if y < thing_starty + thing_height:
print('y crossover')

if x > thing_startx and x < thing_startx + thing_width or x + car_width > thing_startx and x + car_width < thing_startx + thing_width:
print('x crossover')
crash()

pygame.display.update()
clock.tick(60)
border_thickness = 2
borders = {
'top_left': (border_thickness, border_thickness, -border_thickness, -border_thickness),
'top_right': (0, border_thickness, -border_thickness, -border_thickness),
'bottom_left': (border_thickness, 0, -border_thickness, -border_thickness),
'bottom_right': (0, 0, -border_thickness, -border_thickness),
'u_shape': (border_thickness, 0, -2*border_thickness, -border_thickness),
'n_shape': (border_thickness, border_thickness, -2*border_thickness, -border_thickness),
'o_shape': (border_thickness, border_thickness, -2*border_thickness, -2*border_thickness),
']_shape': (0, border_thickness, -border_thickness, -2*border_thickness),
'[_shape': (border_thickness, border_thickness, -border_thickness, -2*border_thickness),
}


def make_cell(border, border_colour, box_colour, highlight_colour, x, y, w, h, action=None):
try:
mod_x, mod_y, mod_w, mod_h = borders[border]
except KeyError:
raise
else:
pygame.draw.rect(gameDisplay, border_colour, (x, y, w, h))
# pygame.draw.rect(gameDisplay, box_colour, (x+mod_x, y+mod_y, w+mod_w, h+mod_h))

mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
# print(click)
if x + w > mouse[0] > x and y + h > mouse[1] > y:
pygame.draw.rect(gameDisplay, highlight_colour, (x+mod_x, y+mod_y, w+mod_w, h+mod_h))

if click[0] == 1 and action is not None:
action()
else:
pygame.draw.rect(gameDisplay, box_colour, (x+mod_x, y+mod_y, w+mod_w, h+mod_h))


while True:
for event in pygame.event.get():
# print(event)
if event.type == pygame.QUIT:
quitgame()

main_box_width = display_width / 3
main_box_height = display_height / 3
gameDisplay.fill(white)
# top row
make_cell('bottom_right', black, white, green, 0, 0, main_box_width, main_box_height, partial(print, ('hello',)))
make_cell('u_shape', black, white, green, display_width/3, 0, main_box_width, main_box_height, partial(print, ('hello',)))
make_cell('bottom_left', black, white, green, display_width*(2/3), 0, main_box_width, main_box_height, partial(print, ('hello',)))
# middle row
make_cell(']_shape', black, white, green, 0, display_height/3, main_box_width, main_box_height, partial(print, ('hello',)))
make_cell('o_shape', black, white, green, display_width/3, display_height/3, main_box_width, main_box_height, partial(print, ('hello',)))
make_cell('[_shape', black, white, green, display_width*(2/3), display_height/3, main_box_width, main_box_height, partial(print, ('hello',)))
# bottom row
make_cell('top_right', black, white, green, 0, display_height * (2/3), main_box_width, main_box_height, partial(print, ('hello',)))
make_cell('n_shape', black, white, green, display_width/3, display_height * (2/3), main_box_width, main_box_height, partial(print, ('hello',)))
make_cell('top_left', black, white, green, display_width*(2/3), display_height * (2/3), main_box_width, main_box_height, partial(print, ('hello',)))

pygame.display.update()
clock.tick(15)

game_intro()
game_loop()
quitgame()
Loading

0 comments on commit 0e99c51

Please sign in to comment.