0% found this document useful (0 votes)
19 views2 pages

Games. Py

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views2 pages

Games. Py

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#Snake Game starts

import math import random import pygame import tkinter as tk from tkinter import
messagebox
class cube(object): rows = 20 w = 500 def __init__(self, start,
dirnx=1, dirny=0,color=(255,0,0)): [Link] = start [Link] = 1
[Link] = 0 [Link] = color def move(self, dirnx, dirny):
[Link] = dirnx [Link] = dirny [Link] = ([Link][0] +
[Link], [Link]) def draw(self, surface, eyes=False): dis =
self.w // [Link] i = [Link][0] j = [Link][1]
[Link](surface, [Link], (i*dis+1,j*dis+1, dis-2, dis-2)) if
eyes: centre = dis//2 radius = 3 circleMiddle
= (i*dis+centre-radius, j*dis+8) circleMiddle2 = (i*dis + dis -
radius*2, j*dis+8) [Link](surface, (0,0,0), circleMiddle,
radius) [Link](surface, (0,0,0), circleMiddle2, radius)

class snake(object): body=[] turns = {} def __init__(self, color, pos):


[Link]= color [Link] = cube(pos) [Link]([Link])
[Link] = 0 [Link] = 1 def move(self): for event in
[Link](): if [Link] == [Link]:
[Link]() keys = [Link].get_pressed()
for key in keys: if keys[pygame.K_LEFT]:
[Link] = -1 [Link] = 0
[Link][[Link][:]] = [[Link], [Link]]
elif keys[pygame.K_RIGHT]: [Link] = 1
[Link] = 0 [Link][[Link][:]] = [[Link],
[Link]] elif keys[pygame.K_UP]:
[Link] = 0 [Link] = -1
[Link][[Link][:]] = [[Link], [Link]]
elif keys[pygame.K_DOWN]: [Link] = 0
[Link] = 1 [Link][[Link][:]] = [[Link],
[Link]] for i, c in enumerate([Link]):
p=[Link][:] if p in [Link]: turn = [Link][p]
[Link](turn[0], turn[1]) if i == len([Link])-1:
[Link](p) else: if [Link] == -1 and [Link][0]
<= 0: [Link] = ([Link]-1, [Link][1]) elif [Link] == 1 and [Link][0]
>= [Link]-1: [Link] = (0, [Link][1]) elif [Link] == 1 and [Link][1]
>= [Link]-1: [Link] = ([Link][0], 0) elif [Link] == -1 and [Link][1]
<= 0: [Link] = ([Link][0], [Link]-1) else: [Link]([Link], [Link])
def reset(self, pos): [Link] = cube(pos) [Link] = []
[Link]([Link]) [Link] = () [Link] = 0
[Link] = 1 def addCube(self): tail = [Link][-1]
dx, dy = [Link], [Link] if dx == 1 and dy == 0:
[Link](cube(([Link][0]-1, [Link][1]))) elif dx == -1 and dy
== 0: [Link](cube(([Link][0]+1, [Link][1]))) elif
dx == 0 and dy == 1: [Link](cube(([Link][0], [Link][1]-
1))) elif dx == 0 and dy == -1:
[Link](cube(([Link][0], [Link][1]+1)))
[Link][-1].dirnx = dx [Link][-1].dirny = dy def
draw(self, surface): for i, c in enumerate([Link]): if i ==
0: [Link](surface, True) else:
[Link](surface)
def drawGrid(w, rows, surface): sizeBtwn = w // rows x = 0 y = 0
for l in range (rows): x = x + sizeBtwn y = y + sizeBtwn
[Link](surface,(255, 255, 255), (x,0), (x,w))
[Link](surface,(255, 255, 255), (0,y), (w,y)) def
redrawWindow(surface): global rows, width, s, snack [Link]((0,0,0))
[Link](surface) [Link](surface) drawGrid(width, rows,surface)
[Link]()
def randomSnack(rows, item): positions = [Link] while True:
x = [Link](rows) y = [Link](rows) if
len(list(filter(lambda z:[Link] == (x,y), positions)))> 0: continue
else: break return (x,y) def
message_box(subject, content): root = [Link]() [Link]("-topmost",
True) [Link]() [Link](subject,content) try:
[Link]() except:
pass
def main(): global width, rows, s, snack width = 500 rows = 20 win
= [Link].set_mode((width, width)) s = snake((255,0,0), (10,10))
snack = cube(randomSnack(rows, s), color=(0,255,0)) flag = True clock
= [Link]() while flag: [Link](50)
[Link](10) [Link]() if [Link][0].pos == [Link]:
[Link]() snack = cube(randomSnack(rows, s), color=(0,255,0))
for x in range(len([Link])): if [Link][x].pos in list(map(lambda
z:[Link], [Link][x+1:])): print('Score: ', len([Link]))
message_box('You Lost!', 'Play again...') [Link]((10,10))
break redrawWindow(win) pass
main()

You might also like