Skip to content

Instantly share code, notes, and snippets.

@ecounysis
Created May 9, 2014 05:31
Show Gist options
  • Save ecounysis/9c29fb8ab9a2daf0f264 to your computer and use it in GitHub Desktop.
Save ecounysis/9c29fb8ab9a2daf0f264 to your computer and use it in GitHub Desktop.
the beginnings of a weird little game
import time, random, sys, pygame
from pygame.locals import *
i=0
rows=600
cols=1200
def getBackground(r,c,size):
colors = [(0,255,0), (10,220,20), (10,200,20), (0,150,0)]
background = pygame.Surface((c,r))
background = background.convert()
for x in range(r/size/4):
for y in range(c/size/4):
r=random.random()
color = colors[0]
if r>0.2: color=colors[1]
if r>0.6: color=colors[2]
if r>0.8: color=colors[3]
rect=pygame.Rect((y*size*4, x*size*4, size*4, size*4))
pygame.draw.rect(background, color, rect)
return background
UP=0
DOWN=1
LEFT=2
RIGHT=3
STILL=-1
class Diamond():
def __init__(self,x,y,color):
self.x=x
self.y=y
self.color=color
def draw(self):
g = [0,0,1,0,0,
0,0,1,0,0,
0,1,1,1,0,
1,1,1,1,1,
0,1,1,1,0,
0,0,1,0,0,
0,0,1,0,0]
cc=pygame.Surface((5, 6))
cc.set_colorkey(TRANSPARENT)
for i in range(len(g)):
x=i%5
y=i/5
color=TRANSPARENT
if g[i]==1:
color=self.color
if g[i]==0:
#print i
color=TRANSPARENT
cc.set_at((x,y), color)
return cc
class Creeper():
def setSize(self, sz):
self.size = sz
self.speed = self.speed*sz
def increaseSpeed(self):
self.speed+=self.size
def moveUP(self):
self.motion=UP
def moveDOWN(self):
self.motion=DOWN
def moveRIGHT(self):
self.motion=RIGHT
def moveLEFT(self):
self.motion=LEFT
def moveSTOP(self):
self.motion=STILL
self.speed = self.size
def setDeathAnim(self):
self.death_animation = [[1,1,0,0,1,1,
1,1,0,0,1,1,
0,0,1,1,0,0,
0,1,1,1,1,0,
0,1,1,1,1,0,
0,1,0,0,1,0],
[0,1,0,0,1,0,
1,1,1,1,1,1,
1,1,0,1,1,0,
0,1,1,1,1,0,
0,1,0,1,1,0,
0,1,1,0,1,0],
[1,1,0,0,1,1,
1,1,1,0,1,1,
0,0,1,1,0,0,
0,1,0,1,1,0,
0,1,1,0,1,0,
0,1,1,1,1,0],
[0,0,0,0,0,0,
0,0,0,0,0,0,
0,0,0,0,0,0,
0,1,0,0,1,0,
0,1,1,1,1,0,
1,1,0,0,1,1],
[1,0,0,0,0,1,
0,0,0,0,0,0,
0,0,1,1,0,0,
0,0,0,0,0,0,
1,1,0,0,1,1,
1,1,1,1,1,1],
]
def __init__(self, x, y, size):
self.speed=size
self.color=BLACK
self.vector=[1,1,0,0,1,1,
1,1,0,0,1,1,
0,0,1,1,0,0,
0,1,1,1,1,0,
0,1,1,1,1,0,
0,1,0,0,1,0]
self.dead=False
self.setDeathAnim()
self.dead_vector=-1
self.size=size
self.motion=STILL
self.x=x
self.y=y
def move(self):
if self.motion==UP:
if self.y>0:
self.y-=self.speed
if self.motion==DOWN:
if self.y<rows-6*self.size:
self.y+=self.speed
if self.motion==RIGHT:
if self.x<cols-6*self.size:
self.x+=self.speed
if self.motion==LEFT:
if self.x>=0:
self.x-=self.speed
if self.y<0: self.y=0
if self.x<0: self.x=0
if self.y>rows-6*self.size: self.y=rows-6*self.size
if self.x>cols-6*self.size: self.x=cols-6*self.size
def draw(self):
if not self.dead:
self.move()
g=self.vector
elif (self.dead_vector+1<len(self.death_animation)):
self.color=RED
self.dead_vector+=1
g=self.death_animation[self.dead_vector]
else:
g=[0 for i in range(36)]
cc=pygame.Surface((6*self.size, 6*self.size))
cc.set_colorkey(TRANSPARENT)
for i in range(len(g)):
xx=i%6
yy=i/6
if g[i]==1:
color=self.color
if g[i]==0:
color=TRANSPARENT
for k in range(self.size):
x=xx*self.size+k
for l in range(self.size):
y=yy*self.size+l
cc.set_at((x,y),color)
return cc
SIZE=6
BLACK=(0,0,0)
GOLD=(204,153,0)
LBLUE=(0,255,255)
GREENG=(0,220,40)
TRANSPARENT=pygame.Color(255,255,255,0)
pygame.init()
screen = pygame.display.set_mode((cols,rows))
pygame.display.set_caption('Creepers!')
background=getBackground(rows,cols,SIZE)
drawStack=[]
for i in range(50):
x=int(random.random()*cols)
y=int(random.random()*rows)
drawStack.append(Diamond(x,y,LBLUE))
for i in range(100):
x=int(random.random()*cols)
y=int(random.random()*rows)
drawStack.append(Diamond(x,y,GOLD))
c=Creeper(40,40,SIZE)
RED=(230,0,20)
DEATH=(0,0,0)
for i in range(25):
x=int(random.random()*cols)
y=int(random.random()*rows)
if x>80 or y>80:
d=True
for el in drawStack:
if abs(el.x-x)<=10 and abs(el.y-y)<10:
d=False
if d:
drawStack.append(Diamond(x,y,DEATH))
else:
i-=1
else:
i-=1
def draw():
screen.blit(background,(0,0))
for el in drawStack:
screen.blit(el.draw(),(el.x,el.y))
screen.blit(c.draw(),(c.x,c.y))
def logic():
for el in drawStack:
if (c.x+SIZE*6>=el.x) and (c.y+SIZE*6>=el.y) and (el.x+5>=c.x) and (el.y+6>=c.y):
if (el.color != DEATH):
drawStack.remove(el)
return False
else:
c.dead=True
return True
return False
FPS = 35 # frames per second setting
fpsClock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
c.moveUP()
if event.key == pygame.K_DOWN:
c.moveDOWN()
if event.key == pygame.K_LEFT:
c.moveLEFT()
if event.key == pygame.K_RIGHT:
c.moveRIGHT()
if event.type == pygame.KEYUP:
if event.key != pygame.K_SPACE:
c.moveSTOP()
else:
c.increaseSpeed()
game_over = logic()
draw()
pygame.display.flip()
fpsClock.tick(FPS)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment