back to michigan, a silly snake game in python 2.7 and pygame

I'll be trying to get the forced perspective experiment done in the next couple days.

I'm headed back to Michigan soon. (So long California!)
There's a lot to do before we hit the road so between that and some health problems I don't know if I'll get that experiment done and posted.
If you don't see a post in a while, No worries, I'll be back when I can.

Here's a bit of an old code I wrote in pygame python 2.7.   It's messy, and you'd have to put you're own images in, but it's basically a snake game.

If coding can't be fun, what's the point?
Remember it's from before I really did any real diving into code, so it's not written how I would do it today. 
Have fun! Reak havoc, whoever you are!

# .....I didn't test it.  I know it did work back in 2013
#  .....  all be it badly. 

import sys
import pygame
from pygame.locals import*
from sys import exit
import random
from random import randint



pygame.init()
UnicornIntro = pygame.image.load('apoobrush.png')
Introbackground = pygame.transform.scale(UnicornIntro, (500, 500))
background = pygame.image.load('Michigan_night.png')
Width = 1000
Height = 1000


N, M = 30, 30

Scale = 35
w, h = Scale * M, Scale * N
screen = pygame.display.set_mode((w, h))
BLACK = (0,0,0)
WHITE = (255, 255, 255)
PURPLE = (240, 50, 255)
ORANGE = (240, 240, 0)
Rainbow = (randint(0,255), randint(0, 255), randint(0,255))
pygame.display.set_caption('Unicorn POO')
screen = pygame.display.set_mode((w, h))

screen.fill(WHITE)
UnicornHead = [(5,5)]
PooStreak = [(5,5), (5,4), (5,3), (5,2), (5,1)]
Food = [(10,5), (6, 4), (10, 16), (10, 20)]
font = pygame.font.Font(None, 49)
font_m = pygame.font.Font(None, 32)
text = font.render("Pooping Unicorn! Clicky clicky!", False, PURPLE)
textPos = text.get_rect()
textPos.centerx = screen.get_rect().centerx
pygame.draw.rect(screen, (100, 255, 255), (0, 0, 800, 60))
screen_edge = [(880, 20), (880, 880), (20, 880), (20, 20)]
(R, L, U, D) = range(4)
d = R

spazspot1 = randint(20, Width)
spazspot2 = randint(20, Height)
       
next = 0       
k=1
start = 0

def HeadDraw():
    Unicorn= pygame.image.load("brushUp.png").convert()
    unicorn = pygame.transform.scale(Unicorn, (70, 70))   
    for i in UnicornHead:
        screen.blit(unicorn, (i[0] * Scale - 15, i[1]*Scale -15, Scale - 1, Scale - 1))           


def move():
    if d==R: x=1 ; y = 0;
    if d==L: x=-1 ; y = 0;
    if d==U: x=0 ; y = -1;
    if d==D: x=0 ; y = 1;
    t = PooStreak[0]
    t = (t[0]+x, t[1]+y)
   
    UnicornHead.insert(0, t)
    PooStreak.insert(0, t)
    del UnicornHead[-1]
    del PooStreak[-1]
    NewFood()
    if PooStreak[0] in PooStreak[1:]: del PooStreak[2:]
   
   
   
def btn_press(btn):
    global d
    if btn == K_UP: d = U
    if btn == K_DOWN: d = D
    if btn == K_LEFT: d = L
    if btn == K_RIGHT: d = R


       
   
       
       
   
def TailDraw():
    UnicornPoop = pygame.image.load('aunicornpoop.jpg').convert()
    unicornpoop = pygame.transform.scale(UnicornPoop, (35, 35))   
    Rainbow = (randint(0,255), randint(0, 255), randint(0,255))
   
    for i in PooStreak:
         pygame.draw.circle(screen, (randint(0,255), randint(0, 255), randint(0,255)),(i[0] * Scale + 10, i[1]*Scale + 25), 10)

def NewFood():
    if PooStreak[0] in Food:
       
        i=Food.index(PooStreak[0])
        Food[i]=(randint(0, N), randint(0, M))
        PooStreak.append(PooStreak[-1])
       
   
       
def FoodDraw():
    for i in Food:
        Spirit = pygame.image.load('Unicornfood.png')
        spirit = pygame.transform.scale(Spirit, (65, 65))
        screen.blit(spirit, (i[0] * Scale, i[1]*Scale, Scale - 1, Scale - 1))   
def GameOver():
    if len(PooStreak) == 2:
        next = 2
        start = 0
        screen.fill(WHITE)
        Title = font.render("You stepped on the Rainbows!", False, ORANGE)
        Title2 = font_m.render(" GAME OVER ", False, ORANGE)
        Title3 = font_m.render("Push ENTER to continue", False, ORANGE)
        UnicornEnd = pygame.image.load('aunicornpoop.jpg')
        EndScreen = pygame.transform.scale(UnicornIntro, (500, 500))
        screen.blit(EndScreen, (100, 100))
        screen.blit(Title, (250, 200))
        screen.blit(Title2, (150, 350))
        screen.blit(Title3, (180, 490))
        keys = pygame.key.get_pressed()



       
       
while k:
    for event in pygame.event.get():
        if event.type == KEYDOWN:
            btn_press(event.key)
            if event.key == K_ESCAPE:
                exit()
        elif event.type == QUIT:
            exit()

   
    #event = pygame.event.poll()   
    index = 0
        #Unicornpic= images[]
   
    GameOver()
   

    if next == 0:
        screen.fill(WHITE)
        Title = font.render("Making Rainbows", False, BLACK)
        Title2 = font_m.render("Use directional arrows to guide your Unicorn.", False, BLACK)
        Title3 = font_m.render("Push ENTER to continue", False, PURPLE)
        UnicornIntro = pygame.image.load('apoobrush.png')
        Introbackground = pygame.transform.scale(UnicornIntro, (500, 500))
        screen.blit(Introbackground, (100, 100))
        screen.blit(Title, (250, 100))
        screen.blit(Title2, (150, 150))
        screen.blit(Title3, (180, 190))
        keys = pygame.key.get_pressed()
        pygame.display.flip()
    if (keys[K_RETURN]):
        next = 1
        start = 1
    if start == 1:
        clock = pygame.time.Clock()
       
        score = len(PooStreak)
       
       
        screen.blit(screen, (0,0))
        pygame.display.update()
        k +=1
        if k%5==0: move()
        screen.fill(BLACK)
       
        screen.blit(text, textPos)
        TailDraw()
       
        HeadDraw()
        NewFood()
       
        FoodDraw()
       
        renderscore = font.render(("SCORE: %d" %score), False, ORANGE)
        screen.blit(renderscore, (800, 60))
        speed = 30
        clock.tick(speed)
           
   

Comments

Popular posts from this blog

JavaScript Ascii animation with while loops and console.log

playing with trigonometry sin in pygame

JavaScript and a Matrix