Created
September 11, 2011 01:07
-
-
Save Lambdanaut/1209033 to your computer and use it in GitHub Desktop.
A Falling Sand game made using Python2 & SDL
This file contains 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
#!/usr/bin/python | |
#An experimental falling(or rising, in this case) sand game coded by Lambdanaut | |
import pygame, sys, os, random | |
from pygame.locals import * | |
if sys.platform == 'win32' or sys.platform == 'win64': | |
os.environ['SDL_VIDEO_CENTERED'] = '1' | |
#Init Variables | |
def popRoom(x,y): | |
room = [] | |
for i in range(0,y): | |
room.append([]) | |
for e in range(0,x): | |
room[i].append(0) | |
return room | |
palette = 0 | |
room = popRoom(100,100) | |
squareSize = [5,5] | |
width = squareSize[0] * len(room[0]) | |
height = squareSize[1] * len(room) | |
#Init Pygame and Screen | |
pygame.init() | |
screen = (width,height) | |
icon = pygame.Surface((1,1)); icon.set_alpha(0); | |
pygame.display.set_caption("SAND") | |
surface = pygame.display.set_mode(screen) | |
clock = pygame.time.Clock() | |
#Extra Functions | |
def drawGround(): | |
for i in range(0,len(room)): | |
for e in range(0,len(room[0])): | |
#Draw Water | |
if room[i][e] == 1: | |
rect = pygame.Rect(squareSize[0]*e,squareSize[1]*i,squareSize[0],squareSize[1]) | |
pygame.draw.rect(surface,(0,25,255),rect,0) | |
#Draw Wall | |
if room[i][e] == 2: | |
rect = pygame.Rect(squareSize[0]*e,squareSize[1]*i,squareSize[0],squareSize[1]) | |
pygame.draw.rect(surface,(0,0,50),rect,0) | |
#Draw Sand | |
if room[i][e] == 3: | |
rect = pygame.Rect(squareSize[0]*e,squareSize[1]*i,squareSize[0],squareSize[1]) | |
pygame.draw.rect(surface,(255,255,0),rect,0) | |
#Draw Plant | |
if room[i][e] == 4: | |
rect = pygame.Rect(squareSize[0]*e,squareSize[1]*i,squareSize[0],squareSize[1]) | |
pygame.draw.rect(surface,(0,255,0),rect,0) | |
#Draw Fountain | |
if room[i][e] == 5: | |
rect = pygame.Rect(squareSize[0]*e,squareSize[1]*i,squareSize[0],squareSize[1]) | |
pygame.draw.rect(surface,(100,0,255),rect,0) | |
#Draw Fire | |
if room[i][e] == 6: | |
rect = pygame.Rect(squareSize[0]*e,squareSize[1]*i,squareSize[0],squareSize[1]) | |
pygame.draw.rect(surface,(255,0,0),rect,0) | |
def gravity(row,spot,cpRoom,type): | |
#Fluid Object | |
if type == 1: | |
r = random.randint(0,1) | |
if cpRoom[row - 1][spot] == 0: | |
room[row][spot] = 0 | |
room[row - 1][spot] = type | |
elif cpRoom[row - 1][spot - 1] == 0 and r == 0: | |
room[row][spot] = 0 | |
room[row - 1][spot - 1] = type | |
elif cpRoom[row - 1][spot + 1] == 0 and r == 1: | |
room[row][spot] = 0 | |
room[row - 1][spot + 1] = type | |
elif cpRoom[row][spot - 1] == 0 and cpRoom[row][spot + 1] != 0 and r == 0: | |
room[row][spot] = 0 | |
room[row][spot - 1] = type | |
elif cpRoom[row][spot + 1] == 0 and cpRoom[row][spot - 1] != 0 and r == 1: | |
room[row][spot] = 0 | |
room[row][spot + 1] = type | |
elif cpRoom[row + 1][spot - 1] == 0 and cpRoom[row][spot + 1] != 0 and r == 0: | |
room[row][spot] = 0 | |
room[row + 1][spot - 1] = type | |
elif cpRoom[row + 1][spot + 1] == 0 and cpRoom[row][spot - 1] != 0 and r == 1: | |
room[row][spot] = 0 | |
room[row + 1][spot + 1] = type | |
#Plant | |
elif type == 4: | |
#Grow | |
if cpRoom[row - 1][spot] == 1: | |
room[row - 1][spot - 1] = type | |
elif cpRoom[row - 1][spot - 1] == 1: | |
room[row - 1][spot - 1] = type | |
elif cpRoom[row - 1][spot + 1] == 1: | |
room[row - 1][spot + 1] = type | |
elif cpRoom[row][spot - 1] == 1: | |
room[row][spot - 1] = type | |
elif cpRoom[row][spot + 1] == 1: | |
room[row][spot + 1] = type | |
elif cpRoom[row + 1][spot - 1] == 1: | |
room[row + 1][spot - 1] = type | |
elif cpRoom[row + 1][spot + 1] == 1: | |
room[row + 1][spot + 1] = type | |
#Sand | |
elif type == 3: | |
r = random.randint(0,2) | |
if cpRoom[row - 1][spot] == 0: | |
room[row][spot] = 0 | |
room[row - 1][spot] = type | |
elif cpRoom[row - 1][spot - 1] == 0: | |
room[row][spot] = 0 | |
room[row - 1][spot - 1] = type | |
elif cpRoom[row - 1][spot + 1] == 0: | |
room[row][spot] = 0 | |
room[row - 1][spot + 1] = type | |
#Sink in water | |
elif cpRoom[row - 1][spot] == 1 and r == 1: | |
room[row][spot] = 1 | |
room[row - 1][spot] = type | |
#Fountain | |
elif type == 5: | |
r = random.randint(0,2) | |
if room[row - 1][spot] == 0 and r == 0: | |
room[row - 1][spot] = 1 | |
elif cpRoom[row - 1][spot - 1] == 0 and r == 1: | |
room[row - 1][spot - 1] = 1 | |
elif cpRoom[row - 1][spot + 1] == 0 and r == 2: | |
room[row - 1][spot + 1] = 1 | |
#Fire | |
elif type == 6: | |
r = random.randint(0,30) | |
if cpRoom[row - 1][spot] == 1 or r == 0: | |
room[row][spot] = 0 | |
elif cpRoom[row - 1][spot] == 1 or r == 0: | |
room[row][spot] = 0 | |
elif cpRoom[row - 1][spot - 1] == 1 or r == 0: | |
room[row][spot] = 0 | |
elif cpRoom[row - 1][spot + 1] == 1 or r == 0: | |
room[row][spot] = 0 | |
#Burn Plants | |
if cpRoom[row - 1][spot] == 4: | |
room[row - 1][spot] = type | |
if cpRoom[row - 3][spot - 1] != 1 or 2 or 3 or 5 and random.randint(0,5) == 1: | |
room[row - 3][spot - 1] = type | |
elif cpRoom[row - 3][spot] != 1 or 2 or 3 or 5 and random.randint(0,5) == 1: | |
room[row - 3][spot] = type | |
elif cpRoom[row - 3][spot + 1] != 1 or 2 or 3 or 5 and random.randint(0,5) == 1: | |
room[row - 3][spot + 1] = type | |
elif cpRoom[row + 1][spot] == 4: | |
room[row + 1][spot] = type | |
if cpRoom[row - 3][spot - 1] != 1 or 2 or 3 or 5 and random.randint(0,5) == 1: | |
room[row - 3][spot - 1] = type | |
elif cpRoom[row - 3][spot] != 1 or 2 or 3 or 5 and random.randint(0,5) == 1: | |
room[row - 3][spot] = type | |
elif cpRoom[row - 3][spot + 1] != 1 or 2 or 3 or 5 and random.randint(0,5) == 1: | |
room[row - 3][spot + 1] = type | |
elif cpRoom[row - 1][spot - 1] == 4: | |
room[row - 1][spot - 1] = type | |
elif cpRoom[row - 1][spot + 1] == 4: | |
room[row - 1][spot + 1] = type | |
elif cpRoom[row][spot - 1] == 4: | |
room[row][spot - 1] = type | |
elif cpRoom[row][spot + 1] == 4: | |
room[row][spot + 1] = type | |
elif cpRoom[row + 1][spot + 1] == 4: | |
room[row + 1][spot + 1] = type | |
if cpRoom[row - 3][spot - 1] != 1 or 2 or 3 or 5 and random.randint(0,5) == 1: | |
room[row - 3][spot - 1] = type | |
elif cpRoom[row - 3][spot] != 1 or 2 or 3 or 5 and random.randint(0,5) == 1: | |
room[row - 3][spot] = type | |
elif cpRoom[row - 3][spot + 1] != 1 or 2 or 3 or 5 and random.randint(0,5) == 1: | |
room[row - 3][spot + 1] = type | |
elif cpRoom[row + 1][spot - 1] == 4: | |
room[row + 1][spot - 1] = type | |
if cpRoom[row - 3][spot - 1] != 1 or 2 or 3 or 5 and random.randint(0,5) == 1: | |
room[row - 3][spot - 1] = type | |
elif cpRoom[row - 3][spot] != 1 or 2 or 3 or 5 and random.randint(0,5) == 1: | |
room[row - 3][spot] = type | |
elif cpRoom[row - 3][spot + 1] != 1 or 2 or 3 or 5 and random.randint(0,5) == 1: | |
room[row - 3][spot + 1] = type | |
#Main Functions | |
def getInput(palette): | |
key = pygame.key.get_pressed() | |
for event in pygame.event.get(): | |
if event.type == QUIT or key[K_ESCAPE]: | |
pygame.quit(); sys.exit() | |
if palette == 0: | |
if pygame.mouse.get_pressed()[0] == 1: | |
room[int(round(pygame.mouse.get_pos()[1]/squareSize[1]))][int(round(pygame.mouse.get_pos()[0]/squareSize[0]))] = 1 | |
if pygame.mouse.get_pressed()[2] == 1: | |
room[int(round(pygame.mouse.get_pos()[1]/squareSize[1]))][int(round(pygame.mouse.get_pos()[0]/squareSize[0]))] = 2 | |
if palette == 1: | |
if pygame.mouse.get_pressed()[0] == 1: | |
room[int(round(pygame.mouse.get_pos()[1]/squareSize[1]))][int(round(pygame.mouse.get_pos()[0]/squareSize[0]))] = 3 | |
if pygame.mouse.get_pressed()[2] == 1: | |
room[int(round(pygame.mouse.get_pos()[1]/squareSize[1]))][int(round(pygame.mouse.get_pos()[0]/squareSize[0]))] = 4 | |
if palette == 2: | |
if pygame.mouse.get_pressed()[0] == 1: | |
room[int(round(pygame.mouse.get_pos()[1]/squareSize[1]))][int(round(pygame.mouse.get_pos()[0]/squareSize[0]))] = 5 | |
if pygame.mouse.get_pressed()[2] == 1: | |
room[int(round(pygame.mouse.get_pos()[1]/squareSize[1]))][int(round(pygame.mouse.get_pos()[0]/squareSize[0]))] = 6 | |
if pygame.key.get_pressed()[K_SPACE]: | |
if palette == 0: | |
print ("Left Click: Sand; Right Click: Grass") | |
return 1 | |
elif palette == 1: | |
print ("Left Click: Water Fountain; Right Click: Fire") | |
return 2 | |
elif palette == 2: | |
print ("Left Click: Water; Right Click: Wall") | |
return 0 | |
else: | |
return palette | |
def rules(): | |
cpRoom = room | |
for row in range(0,len(cpRoom)): | |
for spot in range(0,len(cpRoom[0])): | |
if row > 0 and row < len(room) - 1 and spot > 0 and spot < len(room[0]) - 1: | |
pass | |
else: | |
room[row][spot] = 0 | |
#Water Rules | |
if cpRoom[row][spot] == 1: | |
gravity(row,spot,cpRoom,1) | |
#Wall Rules | |
pass | |
#Sand Rules | |
if cpRoom[row][spot] == 3: | |
gravity(row,spot,cpRoom,3) | |
#Plant Rules | |
if cpRoom[row][spot] == 4: | |
gravity(row,spot,cpRoom,4) | |
#Fountain Rules | |
if cpRoom[row][spot] == 5: | |
gravity(row,spot,cpRoom,5) | |
#Fire Rules | |
if cpRoom[row][spot] == 6: | |
gravity(row,spot,cpRoom,6) | |
def draw(): | |
surface.fill((255,255,255)) | |
drawGround() | |
pygame.display.flip() | |
def main(palette): | |
while True: | |
clock.tick(30) | |
pygame.event.pump() | |
palette = getInput(palette) | |
rules() | |
draw() | |
if __name__ == '__main__': main(palette) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment