Created
September 26, 2018 09:01
-
-
Save kcranley1/eb3551b18e1d0dcff6f59958eeca8055 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The basics of writing Python games | |
# See Game Development in Python 3 With PyGame on YouTube | |
import pygame | |
import time | |
import random | |
pygame.init() | |
pygame.key.set_repeat(50, 50) #So that when you hold a key down, it repeats | |
display_width = 800 | |
display_height = 445 | |
pause = False | |
#Colour definitions: - pygame doesn't define colours!! | |
black = (0,0,0) | |
white = (255,255,255) | |
bright_red = (255,0,0) | |
red = (200,0,0) | |
bright_green = (0, 255, 0) | |
green = (190, 0, 0) | |
block_color = (53,115,255) | |
# Set up window: | |
gameDisplay = pygame.display.set_mode((display_width,display_height)) | |
pygame.display.set_caption('A Bit Racey') | |
# Define game's clock (fps: | |
clock=pygame.time.Clock() | |
#Load the boat image: | |
boatImg = pygame.image.load('sailboat.gif') | |
boat_width = 20 #check this is the width if the image | |
#Define text_objects: | |
def text_objects(text, font): | |
textSurface = font.render(text, True, black) | |
return textSurface, textSurface.get_rect() | |
#Define message_display: | |
#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() | |
#Define crash(): | |
def crash(): | |
#message_display('You Crashed!') | |
largeText = pygame.font.Font('freesansbold.ttf', 115) | |
TextSurf, TextRect = text_objects('You Crashed!', largeText) | |
TextRect.center = ((display_width/2),((display_height/2)-20)) | |
gameDisplay.blit(TextSurf, TextRect) | |
while True: | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
quitgame() | |
button("Play Again",150,270,100,50,green,bright_green,game_loop) | |
button("Quit",550,270,100,50,red,bright_red,quitgame) | |
pygame.display.update() | |
clock.tick(15) | |
#Define button() | |
def button(msg, x, y, w, h, inactiveColor, activeColor, action=1): | |
mouse = pygame.mouse.get_pos() | |
click = pygame.mouse.get_pressed() | |
#print (mouse) | |
if x+w > mouse[0] > x and y+h > mouse[1] > y: | |
pygame.draw.rect(gameDisplay, activeColor, (x,y,w,h)) | |
if click[0] == 1 and action != None: | |
action() | |
else: | |
pygame.draw.rect(gameDisplay, inactiveColor, (x,y,w,h)) | |
smallText = pygame.font.Font("freesansbold.ttf", 20) | |
textSurf, textRect = text_objects(msg, smallText) | |
textRect.center = ((x+(w/2)),(y+(h/2))) | |
gameDisplay.blit(textSurf, textRect) | |
#Define game_intro(): | |
def game_intro(): | |
intro = True | |
while intro: | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
quitgame() | |
gameDisplay.fill(block_color) | |
largeText = pygame.font.Font('freesansbold.ttf', 115) | |
TextSurf, TextRect = text_objects("A Bit Racey", largeText) | |
TextRect.center = ((display_width/2), (display_height/2)) | |
gameDisplay.blit(TextSurf, TextRect) | |
button("GO!", 150, 350, 100, 50, green, bright_green, game_loop) | |
button("QUIT!", 550, 350, 100, 50, red, bright_red, quitgame) | |
pygame.display.update() | |
clock.tick(15) | |
#Define quitgame(): | |
def quitgame(): | |
pygame.quit()#Pygame quit | |
quit() #Python quit | |
def unpause(): | |
global pause | |
pause = False | |
def paused(): | |
largeText = pygame.font.SysFont("comicsansms",115) | |
TextSurf, TextRect = text_objects("Paused", largeText) | |
TextRect.center = ((display_width/2),(display_height/2)) | |
gameDisplay.blit(TextSurf, TextRect) | |
while pause: | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
quitgame() | |
button("Continue",150,250,100,50,green,bright_green,unpause) | |
button("Quit",550,250,100,50,red,bright_red,quitgame) | |
pygame.display.update() | |
clock.tick(15) | |
#Boat function: | |
def boat(x,y): | |
gameDisplay.blit(boatImg, (x,y)) | |
#Define collision blocks: | |
def things(thingx, thingy, thingw, thingh, color): | |
pygame.draw.rect(gameDisplay, color, [thingx, thingy, thingw, thingh]) | |
#Define things_dodged(count) | |
def things_dodged(count): | |
font = pygame.font.SysFont(None, 25) | |
text = font.render("Dodged :" + str(count), True, black) | |
gameDisplay.blit(text, (0,0)) | |
#Game's loop (quit or exit game later): | |
def game_loop(): | |
global pause | |
x = (display_width * 0.45) | |
y = (display_height * 0.8) | |
x_change = 0 | |
thing_startx = random.randrange(0, display_width) | |
thing_starty = -600 #ie off the screen | |
thing_speed = 4 | |
thing_width = 1 | |
thing_height = 100 | |
dodged = 0 | |
gameExit = False | |
while not gameExit: | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
quitgame() | |
if event.type == pygame.KEYDOWN: | |
if event.key == pygame.K_LEFT: # left arrow | |
x_change = -5 | |
if event.key == pygame.K_RIGHT: # right arrow | |
x_change = 5 | |
if event.key == pygame.K_p: | |
pause = True | |
paused() | |
if event.type == pygame.KEYUP: | |
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT: | |
x_change = 0 | |
x += x_change | |
print(event) # allows you to see mouse moving events etc on the Python Shell window | |
gameDisplay.fill(white) | |
# things(thingx, thingy, thingw, thingh, color): | |
things(thing_startx, thing_starty, thing_width, thing_height, block_color) | |
thing_starty += thing_speed | |
boat(x,y) | |
things_dodged(dodged) | |
# Crash condition with sides of the display window: | |
if x > display_width-boat_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 + boat_width > thing_startx and x + boat_width < thing_startx + thing_width: | |
print ('x crossover') | |
crash() | |
pygame.display.update() # or use flip() - no argument - just update entire window | |
#Move frame on: | |
clock.tick(60) # fps | |
game_intro() | |
game_loop() | |
quitgame() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment