Skip to content

Commit

Permalink
Made lots of changes, optimized code and edited other stuff.
Browse files Browse the repository at this point in the history
  • Loading branch information
Harry committed Feb 17, 2019
1 parent fd01e3d commit 465e790
Showing 1 changed file with 41 additions and 107 deletions.
148 changes: 41 additions & 107 deletions snake-6.py
Expand Up @@ -2,7 +2,7 @@
import pygame
pygame.init()
except:
print("----<error>-----\nSomething wrong with pygame\nPlz run with Python 3 and make sure pygame is installed")
print("----<error>-----\nSomething wrong with pygame\nPlz run with Python 3.X and make sure pygame is installed\nIf you have Linxu please use the 'dependencys.sh' provided to install all dependencys")
input()
exit()
a = False
Expand All @@ -18,7 +18,7 @@
import os
d = True
except:
print("----<error>-----\nProblem with imported modules\nModules\t\t|Imported\t|\n----------------|---------------|\nrandom\t\t|"+str(a)+"\t\t|\ntime\t\t|"+str(b)+"\t\t|\nthreading\t|"+str(c)+"\t\t|\nos\t\t|"+str(d)+"\t\t|\nPlease fix")
print("----<error>-----\nProblem with imported modules\nModules\t\t|Imported\t|\n----------------|---------------|\nrandom\t\t|"+str(a)+"\t\t|\ntime\t\t|"+str(b)+"\t\t|\nthreading\t|"+str(c)+"\t\t|\nos\t\t|"+str(d)+"\t\t|\nPlease fix this.")
def strtobool(s):
return s.lower() in ("true", "yes", "1")
class var:
Expand Down Expand Up @@ -58,7 +58,6 @@ def getvar(num):
if(len(tmp[i].split(":"))== 2):
if(tmp[i].split(":")[0].lower() == "bombs"):
var.bomb.enabled = strtobool(tmp[i].split(":")[1])
print(str(var.bomb.enabled)+" = "+str(strtobool(tmp[i].split(":")[1])))
elif(tmp[i].split(":")[0].lower() == "walls"):
var.rules.wallloop = not(strtobool(tmp[i].split(":")[1]))
elif(tmp[i].split(":")[0].lower() == "speed"):
Expand All @@ -68,7 +67,6 @@ def getvar(num):
elif(tmp[i].split(":")[0].lower() == "lengt"):
var.snake.length = float(tmp[i].split(":")[1])
file.close()

def modes():
file = open("snake.data",'r')
tmp = (file.read())
Expand All @@ -84,9 +82,6 @@ class apple:
def place():
var.apple.x = random.randint(1,var.grid.width-2)
var.apple.y = random.randint(1,var.grid.height-2)
# while not(check(var.apple.x, var.apple.y)):
# var.apple.x = random.randint(1,var.grid.width-2)
# var.apple.y = random.randint(1,var.grid.height-2)
class bomb:
enabled = True
color = (0,255,0)
Expand All @@ -112,8 +107,6 @@ class snake:
class tail:
x = []
y = []


def setup():
if(os.path.isfile("hs.data")):
try:
Expand All @@ -140,53 +133,55 @@ def setup():
var.bomb.place()
var.current.points = 0
var.snake.speed = float(5)
class graph:#graphics
def rec(s,x,y,w,h,c):#draws rectangle
pygame.draw.rect(s,c,pygame.Rect(x,y,w,h))
def string(s,x,y,height,string,c):
font = var.misc.font
if(height != None):
font = pygame.font.SysFont('Consolas', height)
else:
height = 15
if (len(string.split("\n"))>0):
for i in range(len(string.split("\n"))):
text = font.render(string.split("\n")[i], False, c)
s.blit(text,(x,y+(height*i)))
else:
text = font.render(string, False, c)
s.blit(text,(x,y))
class game:
def start():
var.setup()
var.getvar(var.current.mode)
game.draw.screen()
def death():
var.snake.deaths+=1
if (var.current.highscore > var.current.points):
game.draw.string(var.py.screen,var.grid.blockwidth,var.grid.blockwidth,50,"Game over!\nScore: "+str(var.current.points)+"\nHighScore: "+str(var.current.highscore),(255,0,0))
elif (var.current.highscore <= var.current.points):
game.draw.string(var.py.screen,var.grid.blockwidth,var.grid.blockwidth,50,"Game over!\nNew HighScore!\nHighScore: "+str(var.current.points),(255,0,0))
var.current.highscore = var.current.points
if(os.path.isfile("hs.data")):
file = open("hs.data",'w')
file.write(str(var.current.highscore))
file.close()
else:
file = open("hs.data",'a')
file.write(str(var.current.highscore))
file.close()
if(var.current.mode == 3):
if (var.current.highscore > var.current.points):
graph.string(var.py.screen,var.grid.blockwidth,var.grid.blockwidth,50,"Game over!\nScore: "+str(var.current.points)+"\nHighScore: "+str(var.current.highscore),(255,0,0))
elif (var.current.highscore <= var.current.points):
graph.string(var.py.screen,var.grid.blockwidth,var.grid.blockwidth,50,"Game over!\nNew HighScore!\nHighScore: "+str(var.current.points),(255,0,0))
var.current.highscore = var.current.points
if(os.path.isfile("hs.data")):
file = open("hs.data",'w')
file.write(str(var.current.highscore))
file.close()
else:
file = open("hs.data",'a')
file.write(str(var.current.highscore))
file.close()
pygame.display.flip()
time.sleep(1.5)
var.setup()
var.getvar(var.current.mode)
game.draw.screen()
class draw:
def rec(s,x,y,w,h,c):
pygame.draw.rect(s,c,pygame.Rect(x,y,w,h))
def string(s,x,y,height,string,c):
font = var.misc.font
if(height != None):
font = pygame.font.SysFont('Consolas', height)
else:
height = 15
if (len(string.split("\n"))>0):
for i in range(len(string.split("\n"))):
text = font.render(string.split("\n")[i], False, c)
s.blit(text,(x,y+(height*i)))
else:
text = font.render(string, False, c)
s.blit(text,(x,y))
def screen():
screen = var.py.screen
block_width = var.grid.blockwidth
screen_width = var.screen.width
screen_height = var.screen.height
rec = game.draw.rec
rec = graph.rec#quick variable
c1 = var.grid.color1
c2 = var.grid.color2
for x in range(int(var.grid.width)):
Expand All @@ -196,10 +191,8 @@ def screen():
of = 1
if (((x+of)%2)==0):
rec(screen,x*block_width,y*block_width,block_width,block_width,c1)

else:
rec(screen,x*block_width,y*block_width,block_width,block_width,c2)

rec(screen,0,0,block_width,screen_height,(8,8,8))
rec(screen,screen_width-block_width,0,block_width,screen_height,(8,8,8))
rec(screen,0,0,screen_width,block_width,(8,8,8))
Expand All @@ -220,7 +213,7 @@ def screen():
rec(screen,block_width*var.snake.tail.x[i],block_width*var.snake.tail.y[i],block_width,block_width,color)
color=var.snake.color
rec(screen,block_width*var.snake.x,block_width*var.snake.y,block_width,block_width,color)
game.draw.string(screen,0,0,25,"P: "+str(var.current.points)+"|HS: "+str(var.current.highscore)+"|V: "+str(var.snake.speed)+"|L: "+str(var.snake.length)+"|B/S",(255,255,255))
graph.string(screen,0,0,25,"P: "+str(var.current.points)+"|HS: "+str(var.current.highscore)+"|V: "+str(var.snake.speed)+"|L: "+str(var.snake.length)+"|"+str(var.snake.speed)+"B/S",(255,255,255))
pygame.display.flip()
def ref():
game.tails()
Expand All @@ -229,71 +222,53 @@ def ref():
move = game.move
if (dire== 0):
move(0,-1)
#snake.y -=1
elif (dire == 1):
move(1,0)
#snake.x+=1
elif (dire == 2):
#snake.y+=1
move(0,1)
elif (dire == 3):
move(-1,0)
#snake.x-=1
def move(x,y):
death = game.death
#x check
if (var.snake.x+x)>= var.grid.width-1:
if (var.snake.x+x)>= var.grid.width-1:#x check
if(var.rules.wallloop):
var.snake.x = 1;
else:
death()
#print ("out of bounds")

elif(var.snake.x+x)<= 0:
#print ("out of bounds")
if(var.rules.wallloop):
var.snake.x = var.grid.width-2;
else:
death()
else:
var.snake.x+=x
#y check
if (var.snake.y+y)>= var.grid.height-1:
#print ("out of bounds")
if (var.snake.y+y)>= var.grid.height-1:#y check
if(var.rules.wallloop):
var.snake.y = 1;
else:
death()
elif(var.snake.y+y)<= 0:
#print ("out of bounds")
if(var.rules.wallloop):
var.snake.y = var.grid.height-2;
else:
death()
else:
var.snake.y+=y
# apple
if (var.snake.x == var.apple.x) and (var.snake.y == var.apple.y):
if (var.snake.x == var.apple.x) and (var.snake.y == var.apple.y):# apple
var.snake.length+=1
var.current.points+=1
var.apple.lvl+=1
var.snake.speed += var.snake.accel
print(var.snake.speed)
var.apple.place()
var.bomb.place()
elif (var.snake.x == var.bomb.x) and (var.snake.y == var.bomb.y) and (var.bomb.enabled):

death()
var.bomb.place()

print(var.bomb.enabled)
#print("Apple pos:\nX - "+str(apple.x)+"\nY - "+str(apple.y))
cdeaths = var.snake.deaths;
for i in range(len(var.snake.tail.x)):
if(var.snake.deaths == cdeaths):
if (var.snake.x == var.snake.tail.x[i]):
if (var.snake.y == var.snake.tail.y[i]):
#print("game over")
death();
def tails():
var.snake.tail.x.append(var.snake.x)
Expand Down Expand Up @@ -330,30 +305,14 @@ def setup():
var.screen.height = 80
var.modes()
menu.menu2 = var.misc.modes
print(var.misc.modes)
menu1 = ["Play","Quit"]
menu2 = ["Easy","Medium","Hard"]
menu = 1
button = 0
class draw:
def rec(s,x,y,w,h,c):
pygame.draw.rect(s,c,pygame.Rect(x,y,w,h))
def string(s,x,y,height,string,c):
font = var.misc.font
if(height != None):
font = pygame.font.SysFont('Consolas', height)
else:
height = 15
if (len(string.split("\n"))>0):
for i in range(len(string.split("\n"))):
text = font.render(string.split("\n")[i], False, c)
s.blit(text,(x,y+(height*i)))
else:
text = font.render(string, False, c)
s.blit(text,(x,y))
def btn(s,x,y,w,h,c1,c2,st):
menu.draw.rec(s,x,y,w,h,c1)
menu.draw.string(s,x,y,h,st,c2)
graph.rec(s,x,y,w,h,c1)
graph.string(s,x,y,h,st,c2)
def draw():
s = var.py.screen
lmenu = []
Expand All @@ -368,7 +327,6 @@ def draw():
else:
menu.draw.btn(s,0,20*i,200,20,(20,30,40),(200,210,220),lmenu[i])
def keyd():

if (pygame.key.get_pressed()[pygame.K_RIGHT]) or (pygame.key.get_pressed()[pygame.K_RETURN]):
menu.keydel = 0
if (menu.menu == 1):
Expand All @@ -378,7 +336,6 @@ def keyd():
else:
quit();
else:

game.start()
var.current.mode = menu.button
var.getvar(menu.button)
Expand All @@ -391,59 +348,39 @@ def keyd():
menu.button += 1
else:
menu.button = 0
print(menu.button)

elif menu.menu == 2:
if(menu.button+1 <= len(menu.menu2)-1):
menu.button += 1
else:
menu.button = 0
print(menu.button)

elif(pygame.key.get_pressed()[pygame.K_UP]):
menu.keydel = 0
if(menu.button-1 >= 0):
menu.button -= 1
else:
menu.button = 0
print(menu.button)











def loop():
c = 0
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
quit()

# gamef.grid()
if (var.misc.scene == 0):

menu.draw.draw()
pygame.display.flip()

if(menu.keydel >= 50):
menu.keyd()
menu.keydel+=1
print(menu.keydel)


elif (var.misc.scene == 1):
game.keyd()
c+=1
game.draw.screen()
if (c >= (var.py.tick/var.snake.speed)):
game.ref()
c=0
#snake.speed+=1
if not(var.snake.direction == 5):
pygame.display.set_caption("Snake!!!|Score: "+str(var.current.points))
pygame.display.flip()
Expand All @@ -452,10 +389,7 @@ def loop():
menu.setup()
loop()
except Exception as e:
print(e)
log = open("snakegame-error.log",'a')
log.write("Error"+str(e)+"\n")
log.close()
loop()


0 comments on commit 465e790

Please sign in to comment.