Last active
January 4, 2016 11:09
-
-
Save nit99kumar/8613809 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
import pygame, random, sys | |
from pygame.locals import * | |
BLACK = (0, 0, 0) | |
WHITE = (255, 255, 255) | |
BLUE = (0, 0, 255) | |
GREEN = (0, 255, 0) | |
RED = (255, 0, 0) | |
GREY = (190, 190, 190) | |
SIZE = (1024, 576) | |
head = [200, 50] | |
tail = [50, 50] | |
class window: | |
corners = list() | |
def __init__(self): | |
self.clock = pygame.time.Clock() | |
if not self.clock: | |
print 'warning! clock disabled' | |
self.font = pygame.font | |
if not self.font: | |
print 'warning! fonts disabled' | |
self.screen = pygame.display | |
if not self.screen: | |
print 'warning! screen disabled' | |
self.sound = pygame.mixer | |
if not self.sound: | |
print 'warning! sound disabled' | |
pygame.init() | |
self.s = self.screen.set_mode(SIZE) | |
self.screen.set_caption( 'Laser Snake' ) | |
pygame.mouse.set_visible(0) | |
def sub_list(self, list1, list2): | |
temp_x = list1[0] - list2[0] | |
temp_y = list1[1] - list2[1] | |
return [temp_x, temp_y] | |
def update_head(self, head_dir, points): | |
if head_dir == 0: | |
points[0][1] -= 5 | |
elif head_dir == 1: | |
points[0][0] += 5 | |
elif head_dir == 2: | |
points[0][1] += 5 | |
elif head_dir == 3: | |
points[0][0] -= 5 | |
return points | |
def update_tail(self, tail_dir, points): | |
if(tail_dir == 0): | |
points[-1][1] -= 5 | |
elif(tail_dir == 1): | |
points[-1][0] += 5 | |
elif(tail_dir == 2): | |
points[-1][1] += 5 | |
elif(tail_dir == 3): | |
points[-1][0] -= 5 | |
return points | |
def update_tail_dir(self, points): | |
l = len(points) | |
if l > 2: | |
self.temp1 = self.sub_list(points[-1], points[-2]) | |
print self.temp1 | |
if(self.temp1[0] == 0 and self.temp1[1] == 0): | |
self.temp2 = self.sub_list(points[-1], points[-3]) | |
points.remove(points[-2]) | |
if(self.temp2[0] == 0 and self.temp2[1] == 0): | |
self.tail_dir = 2 | |
elif(self.temp2[1] == 0 and self.temp2[1] < 0): | |
self.tail_dir = 0 | |
elif(self.temp2[1] == 0 and self.temp2[1] > 0): | |
self.tail_dir = 1 | |
elif(self.temp2[1] == 0 and self.temp2[1] < 0): | |
self.tail_dir = 3 | |
print 'tail direction: ' + str(self.tail_dir) | |
return self.tail_dir | |
def update_points(self, points, head_dir, tail_dir): | |
if head_dir == 0: | |
points.insert(0, [points[0][0], points[0][1]-5]) | |
elif head_dir == 1: | |
points.insert(0, [points[0][0]+5, points[0][1]]) | |
elif head_dir == 2: | |
points.insert(0, [points[0][0], points[0][1]+5]) | |
elif head_dir == 3: | |
points.insert(0, [points[0][0]-5, points[0][1]]) | |
if(tail_dir == 0): | |
points.insert(-1, [points[-1][0], points[-1][1]-5]) | |
elif(tail_dir == 1): | |
points.insert(-1, [points[-1][0]+5, points[-1][1]]) | |
elif(tail_dir == 2): | |
points.insert(-1, [points[-1][0], points[-1][1]+5]) | |
elif(tail_dir == 3): | |
points.insert(-1, [points[-1][0]-5, points[-1][1]]) | |
tail_dir = self.update_tail_dir(points) | |
return points, tail_dir | |
def update_screen(self, list_snakes = []): | |
directions = { 'UP': 0, 'RIGHT': 1, 'DOWN': 2, 'LEFT': 3, 'OPP_UP': 2, 'OPP_RIGHT': 3, 'OPP_DOWN': 0, 'OPP_LEFT': 1 } | |
self.head_dir = 1 | |
self.tail_dir = 1 | |
self.points = [head, tail] | |
print ( self.points ) | |
while True: | |
self.clock.tick(30) | |
self.s.fill(WHITE) | |
self.screen.update() | |
pygame.draw.aalines(self.s, RED, False, self.points, 15) | |
self.points = self.update_head(self.head_dir, self.points) | |
self.points = self.update_tail(self.tail_dir, self.points) | |
for event in pygame.event.get(): | |
if event.type == QUIT: | |
sys.exit(0) | |
elif event.type == KEYDOWN: | |
if event.key == K_UP: | |
if self.head_dir != directions['OPP_UP'] and self.head_dir != directions['UP']: | |
self.head_dir = directions['UP'] | |
self.points, self.tail_dir = self.update_points(self.points, self.head_dir, self.tail_dir) | |
elif event.key == K_DOWN: | |
if self.head_dir != directions['OPP_DOWN'] and self.head_dir != directions['DOWN']: | |
self.head_dir = directions['DOWN'] | |
self.points, self.tail_dir = self.update_points(self.points, self.head_dir, self.tail_dir) | |
elif event.key == K_LEFT: | |
if self.head_dir != directions['OPP_LEFT'] and self.head_dir != directions['LEFT']: | |
self.head_dir = directions['LEFT'] | |
self.points, self.tail_dir = self.update_points(self.points, self.head_dir, self.tail_dir) | |
elif event.key == K_RIGHT: | |
if self.head_dir != directions['OPP_RIGHT'] and self.head_dir != directions['RIGHT']: | |
self.head_dir = directions['RIGHT'] | |
self.points, self.tail_dir = self.update_points(self.points, self.head_dir, self.tail_dir) | |
elif event.key == K_ESCAPE:sys.exit(0) | |
print ( self.points ) | |
pygame.display.flip() | |
def game_end(self): | |
pass | |
def game_running(self): | |
pass | |
def main(): | |
w_obj = window() | |
w_obj.update_screen() | |
if __name__ == "__main__": | |
main() | |
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
import pygame, random, sys | |
from pygame.locals import * | |
BLACK = (0, 0, 0) | |
WHITE = (255, 255, 255) | |
BLUE = (0, 0, 255) | |
GREEN = (0, 255, 0) | |
RED = (255, 0, 0) | |
GREY = (190, 190, 190) | |
SIZE = (1024, 576) | |
head = [200, 50] | |
tail = [50, 50] | |
class window: | |
corners = list() | |
def __init__(self): | |
self.clock = pygame.time.Clock() | |
if not self.clock: | |
print 'warning! clock disabled' | |
self.font = pygame.font | |
if not self.font: | |
print 'warning! fonts disabled' | |
self.screen = pygame.display | |
if not self.screen: | |
print 'warning! screen disabled' | |
self.sound = pygame.mixer | |
if not self.sound: | |
print 'warning! sound disabled' | |
pygame.init() | |
self.s = self.screen.set_mode(SIZE) | |
self.screen.set_caption( 'Laser Snake' ) | |
pygame.mouse.set_visible(0) | |
def sub_list(self, list1, list2): | |
self.temp_x = list1[0] - list2[0] | |
self.temp_y = list1[1] - list2[1] | |
return [self.temp_x, self.temp_y] | |
def update_head(self, head_dir, points): | |
if head_dir == 0: | |
points[0][1] -= 5 | |
elif head_dir == 1: | |
points[0][0] += 5 | |
elif head_dir == 2: | |
points[0][1] += 5 | |
elif head_dir == 3: | |
points[0][0] -= 5 | |
return points | |
def update_tail(self, tail_dir, points): | |
if(tail_dir == 0): | |
points[-1][1] -= 5 | |
elif(tail_dir == 1): | |
points[-1][0] += 5 | |
elif(tail_dir == 2): | |
points[-1][1] += 5 | |
elif(tail_dir == 3): | |
points[-1][0] -= 5 | |
return points | |
def update_tail_dir(self, points): | |
l = len(points) | |
if l > 2: | |
self.temp1 = self.sub_list(points[-1], points[-2]) | |
print self.temp1 | |
if(self.temp1[0] == 0 and self.temp1[1] == 0): | |
self.temp2 = self.sub_list(points[-1], points[-3]) | |
points.remove(points[-2]) | |
if(self.temp2[0] == 0 and self.temp2[1] > 0): | |
self.tail_dir = 0 | |
elif(self.temp2[0] == 0 and self.temp2[1] < 0): | |
self.tail_dir = 2 | |
elif(self.temp2[0] > 0 and self.temp2[1] == 0): | |
self.tail_dir = 3 | |
elif(self.temp2[0] < 0 and self.temp2[1] == 0): | |
self.tail_dir = 1 | |
print 'tail direction: ' + str(self.tail_dir) | |
return self.tail_dir | |
def update_points(self, points, head_dir, tail_dir): | |
if head_dir == 0: | |
points.insert(0, [points[0][0], points[0][1]-5]) | |
elif head_dir == 1: | |
points.insert(0, [points[0][0]+5, points[0][1]]) | |
elif head_dir == 2: | |
points.insert(0, [points[0][0], points[0][1]+5]) | |
elif head_dir == 3: | |
points.insert(0, [points[0][0]-5, points[0][1]]) | |
tail_dir = self.update_tail_dir(points) | |
return points, tail_dir | |
def update_screen(self, list_snakes = []): | |
directions = { 'UP': 0, 'RIGHT': 1, 'DOWN': 2, 'LEFT': 3, 'OPP_UP': 2, 'OPP_RIGHT': 3, 'OPP_DOWN': 0, 'OPP_LEFT': 1 } | |
self.head_dir = 1 | |
self.tail_dir = 1 | |
self.points = [head, tail] | |
print ( self.points ) | |
while True: | |
self.clock.tick(30) | |
self.s.fill(WHITE) | |
self.screen.update() | |
pygame.draw.aalines(self.s, RED, False, self.points, 15) | |
self.points = self.update_head(self.head_dir, self.points) | |
self.points = self.update_tail(self.tail_dir, self.points) | |
for event in pygame.event.get(): | |
if event.type == QUIT: | |
sys.exit(0) | |
elif event.type == KEYDOWN: | |
if event.key == K_UP: | |
if self.head_dir != directions['OPP_UP'] and self.head_dir != directions['UP']: | |
self.head_dir = directions['UP'] | |
self.points, self.tail_dir = self.update_points(self.points, self.head_dir, self.tail_dir) | |
elif event.key == K_DOWN: | |
if self.head_dir != directions['OPP_DOWN'] and self.head_dir != directions['DOWN']: | |
self.head_dir = directions['DOWN'] | |
self.points, self.tail_dir = self.update_points(self.points, self.head_dir, self.tail_dir) | |
elif event.key == K_LEFT: | |
if self.head_dir != directions['OPP_LEFT'] and self.head_dir != directions['LEFT']: | |
self.head_dir = directions['LEFT'] | |
self.points, self.tail_dir = self.update_points(self.points, self.head_dir, self.tail_dir) | |
elif event.key == K_RIGHT: | |
if self.head_dir != directions['OPP_RIGHT'] and self.head_dir != directions['RIGHT']: | |
self.head_dir = directions['RIGHT'] | |
self.points, self.tail_dir = self.update_points(self.points, self.head_dir, self.tail_dir) | |
elif event.key == K_ESCAPE:sys.exit(0) | |
print ( self.points ) | |
pygame.display.flip() | |
def game_end(self): | |
pass | |
def game_running(self): | |
pass | |
def main(): | |
w_obj = window() | |
w_obj.update_screen() | |
if __name__ == "__main__": | |
main() | |
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
import pygame, random, sys | |
from pygame.locals import * | |
BLACK = (0, 0, 0) | |
WHITE = (255, 255, 255) | |
BLUE = (0, 0, 255) | |
GREEN = (0, 255, 0) | |
RED = (255, 0, 0) | |
GREY = (190, 190, 190) | |
SIZE = (1024, 576) | |
head = [200, 50] | |
tail = [50, 50] | |
class window: | |
corners = list() | |
def __init__(self): | |
self.clock = pygame.time.Clock() | |
if not self.clock: | |
print 'warning! clock disabled' | |
self.font = pygame.font | |
if not self.font: | |
print 'warning! fonts disabled' | |
self.screen = pygame.display | |
if not self.screen: | |
print 'warning! screen disabled' | |
self.sound = pygame.mixer | |
if not self.sound: | |
print 'warning! sound disabled' | |
pygame.init() | |
self.s = self.screen.set_mode(SIZE) | |
self.screen.set_caption( 'Laser Snake' ) | |
pygame.mouse.set_visible(0) | |
def sub_list(self, list1, list2): | |
self.temp_x = list1[0] - list2[0] | |
self.temp_y = list1[1] - list2[1] | |
return [self.temp_x, self.temp_y] | |
def update_head(self, head_dir, points): | |
if head_dir == 0: | |
points[0][1] -= 1 | |
elif head_dir == 1: | |
points[0][0] += 1 | |
elif head_dir == 2: | |
points[0][1] += 1 | |
elif head_dir == 3: | |
points[0][0] -= 1 | |
return points | |
def update_tail(self, tail_dir, points): | |
self.temp_tail_dir = self.update_tail_dir(points) | |
if tail_dir != self.temp_tail_dir: | |
tail_dir = self.temp_tail_dir | |
if(tail_dir == 0): | |
points[-1][1] -= 1 | |
elif(tail_dir == 1): | |
points[-1][0] += 1 | |
elif(tail_dir == 2): | |
points[-1][1] += 1 | |
elif(tail_dir == 3): | |
points[-1][0] -= 1 | |
return points | |
def update_tail_dir(self, points): | |
l = len(points) | |
if l > 2: | |
self.temp1 = self.sub_list(points[-1], points[-2]) | |
if(abs(self.temp1[0]) <= 1 and abs(self.temp1[1]) <= 1): | |
print 'here' | |
self.temp2 = self.sub_list(points[-1], points[-3]) | |
points.remove(points[-2]) | |
if(abs(self.temp2[0]) <= 1 and self.temp2[1] > 0): | |
self.tail_dir = 0 | |
elif(abs(self.temp2[0]) <= 1 and self.temp2[1] < 0): | |
self.tail_dir = 2 | |
elif(self.temp2[0] > 0 and abs(self.temp2[1]) <= 1): | |
self.tail_dir = 3 | |
elif(self.temp2[0] < 0 and abs(self.temp2[1]) <= 1): | |
self.tail_dir = 1 | |
return self.tail_dir | |
def update_points(self, points, head_dir, tail_dir): | |
if head_dir == 0: | |
points.insert(0, [points[0][0], points[0][1]-1]) | |
elif head_dir == 1: | |
points.insert(0, [points[0][0]+1, points[0][1]]) | |
elif head_dir == 2: | |
points.insert(0, [points[0][0], points[0][1]+1]) | |
elif head_dir == 3: | |
points.insert(0, [points[0][0]-1, points[0][1]]) | |
tail_dir = self.update_tail_dir(points) | |
return points, tail_dir | |
def update_screen(self, list_snakes = []): | |
directions = { 'UP': 0, 'RIGHT': 1, 'DOWN': 2, 'LEFT': 3, 'OPP_UP': 2, 'OPP_RIGHT': 3, 'OPP_DOWN': 0, 'OPP_LEFT': 1 } | |
self.head_dir = 1 | |
self.tail_dir = 1 | |
self.points = [head, tail] | |
print ( self.points ) | |
while True: | |
self.clock.tick(30) | |
self.s.fill(WHITE) | |
self.screen.update() | |
pygame.draw.aalines(self.s, RED, False, self.points, 15) | |
self.points = self.update_head(self.head_dir, self.points) | |
self.points = self.update_tail(self.tail_dir, self.points) | |
for event in pygame.event.get(): | |
if event.type == QUIT: | |
sys.exit(0) | |
elif event.type == KEYDOWN: | |
if event.key == K_UP: | |
if self.head_dir != directions['OPP_UP'] and self.head_dir != directions['UP']: | |
self.head_dir = directions['UP'] | |
self.points, self.tail_dir = self.update_points(self.points, self.head_dir, self.tail_dir) | |
elif event.key == K_DOWN: | |
if self.head_dir != directions['OPP_DOWN'] and self.head_dir != directions['DOWN']: | |
self.head_dir = directions['DOWN'] | |
self.points, self.tail_dir = self.update_points(self.points, self.head_dir, self.tail_dir) | |
elif event.key == K_LEFT: | |
if self.head_dir != directions['OPP_LEFT'] and self.head_dir != directions['LEFT']: | |
self.head_dir = directions['LEFT'] | |
self.points, self.tail_dir = self.update_points(self.points, self.head_dir, self.tail_dir) | |
elif event.key == K_RIGHT: | |
if self.head_dir != directions['OPP_RIGHT'] and self.head_dir != directions['RIGHT']: | |
self.head_dir = directions['RIGHT'] | |
self.points, self.tail_dir = self.update_points(self.points, self.head_dir, self.tail_dir) | |
elif event.key == K_ESCAPE:sys.exit(0) | |
print 'tail direction: ' + str(self.tail_dir) | |
print ( self.points ) | |
pygame.display.flip() | |
def game_end(self): | |
pass | |
def game_running(self): | |
pass | |
def main(): | |
w_obj = window() | |
w_obj.update_screen() | |
if __name__ == "__main__": | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment