Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- from random import randint
- pygame.init()
- pygame.font.init()
- myfont = pygame.font.SysFont('Helvetica', 90)
- smallfont = pygame.font.SysFont('Helvatica', 70)
- win = pygame.display.set_mode((1000, 1000))
- blocks = [[(50 * i, 50 * j, 50, 50) for i in range(20)] for j in range(20)]
- for j in range(20):
- for i in range(20):
- pygame.draw.rect(win, (255, 255, 255), (50 * i, 50 * j, 50, 50), 1)
- class Snake():
- def __init__(self):
- self.body = [[10, 10, 'R'], [10, 9, 'R'], [10, 8, 'R']]
- self.run = True
- self.score = 0
- def checklost(self):
- if self.body[0][0] >= 20 or self.body[0][0] < 0 or self.body[0][1] >= 20 or self.body[0][1] < 0:
- self.run = False
- for i in Snake.body[1:]:
- if i[:2] == self.body[0][:2]:
- self.run = False
- def checkdir(self, i):
- if self.body[i][2] == 'R' and self.body[i - 1][1] != self.body[i][1] + 1:
- return False
- if self.body[i][2] == 'L' and self.body[i - 1][1] != self.body[i][1] - 1:
- return False
- if self.body[i][2] == 'U' and self.body[i - 1][0] != self.body[i][0] - 1:
- return False
- if self.body[i][2] == 'D' and self.body[i - 1][0] != self.body[i][0] + 1:
- return False
- return True
- def changedir(self, keys):
- if keys[pygame.K_RIGHT] and self.body[0][2] != 'L' and self.body[0][2] != 'R':
- self.body[0][2] = 'R'
- elif keys[pygame.K_LEFT] and self.body[0][2] != 'R' and self.body[0][2] != 'L':
- self.body[0][2] = 'L'
- elif keys[pygame.K_UP] and self.body[0][2] != 'D' and self.body[0][2] != 'U':
- self.body[0][2] = 'U'
- elif keys[pygame.K_DOWN] and self.body[0][2] != 'U' and self.body[0][2] != 'D':
- self.body[0][2] = 'D'
- def bodycontrol(self):
- for i in range(len(self.body) - 1, 0, -1):
- if not self.checkdir(i):
- self.body[i][2] = self.body[i - 1][2]
- def move(self):
- for i in self.body:
- if i[2] == 'R':
- i[1] += 1
- if i[2] == 'L':
- i[1] -= 1
- if i[2] == 'U':
- i[0] -= 1
- if i[2] == 'D':
- i[0] += 1
- def eatfood(self):
- if self.body[0][:2] == Food.foodpos:
- self.growth()
- Food.changefood()
- self.score += 1
- def growth(self):
- if self.body[-1][2] == 'R':
- if self.body[-1][1] != 0:
- self.body.append([self.body[-1][0], self.body[-1][1] - 1, self.body[-1][2]])
- elif self.body[-1][0] == 0:
- self.body.append([self.body[-1][0] + 1, self.body[-1][1], 'U'])
- else:
- self.body.append([self.body[-1][0] - 1, self.body[-1][1], 'D'])
- if self.body[-1][2] == 'L':
- if self.body[-1][1] != 19:
- self.body.append([self.body[-1][0], self.body[-1][1] + 1, self.body[-1][2]])
- elif self.body[-1][0] == 0:
- self.body.append([self.body[-1][0] + 1, self.body[-1][1], 'U'])
- else:
- self.body.append([self.body[-1][0] - 1, self.body[-1][1], 'D'])
- if self.body[-1][2] == 'U':
- if self.body[-1][0] != 19:
- self.body.append([self.body[-1][0] + 1, self.body[-1][1], self.body[-1][2]])
- elif self.body[-1][1] == 0:
- self.body.append([self.body[-1][0], self.body[-1][1] + 1, 'L'])
- else:
- self.body.append([self.body[-1][0], self.body[-1][1] - 1, 'R'])
- if self.body[-1][2] == 'D':
- if self.body[-1][0] != 0:
- self.body.append([self.body[-1][0] - 1, self.body[-1][1], self.body[-1][2]])
- elif self.body[-1][1] == 0:
- self.body.append([self.body[-1][0], self.body[-1][1] + 1, 'L'])
- else:
- self.body.append([self.body[-1][0], self.body[-1][1] - 1, 'R'])
- class Food():
- def __init__(self):
- self.foodpos = [randint(0, 19), randint(0, 19)]
- def changefood(self):
- self.foodpos = [randint(0, 19), randint(0, 19)]
- for i in Snake.body:
- if self.foodpos == i[:2]:
- self.changefood()
- Snake = Snake()
- Food = Food()
- start = False
- pygame.display.set_caption('Snake Game')
- c = 1
- while not start:
- pygame.time.delay(50)
- if pygame.key.get_pressed()[pygame.K_SPACE]:
- start = True
- if pygame.key.get_pressed()[pygame.K_RIGHT] and c != 3:
- c += 1
- if pygame.key.get_pressed()[pygame.K_LEFT] and c != 1:
- c -= 1
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- Snake.run = False
- start = True
- Text = myfont.render('Select your speed', False, (255, 255, 100))
- smalltext1 = myfont.render('Fast', False, (0, 255, 0))
- smalltext2 = myfont.render('Med', False, (0, 255, 0))
- smalltext3 = myfont.render('Slow', False, (0, 255, 0))
- starttext = smallfont.render('Press SPACE to start', False, (255, 255, 100))
- win.blit(Text, (200, 400))
- win.blit(smalltext1, (125, 12 * 50 - 5))
- win.blit(smalltext2, (430, 12 * 50 - 5))
- win.blit(smalltext3, (725, 12 * 50 - 5))
- win.blit(starttext, (260, 16 * 50))
- if c == 2:
- pygame.draw.rect(win, (255, 0, 0), (400, 12 * 50, 200, 100), 10)
- else:
- pygame.draw.rect(win, (255, 255, 255), (400, 12 * 50, 200, 100), 10)
- if c == 1:
- pygame.draw.rect(win, (255, 0, 0), (100, 12 * 50, 200, 100), 10)
- else:
- pygame.draw.rect(win, (255, 255, 255), (100, 12 * 50, 200, 100), 10)
- if c == 3:
- pygame.draw.rect(win, (255, 0, 0), (700, 12 * 50, 200, 100), 10)
- else:
- pygame.draw.rect(win, (255, 255, 255), (700, 12 * 50, 200, 100), 10)
- pygame.display.update()
- n = 0
- if c == 1:
- n = 75
- if c == 2:
- n = 100
- if c == 3:
- n = 125
- while Snake.run:
- pygame.display.set_caption('Snake Game Your Score is ' + str(Snake.score))
- pygame.time.delay(n)
- k = pygame.key.get_pressed()
- Snake.changedir(k)
- Snake.move()
- Snake.eatfood()
- Snake.bodycontrol()
- Snake.checklost()
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- Snake.run = False
- win.fill((0, 0, 0))
- for j in range(20):
- for i in range(20):
- pygame.draw.rect(win, (255, 255, 255), (50 * i, 50 * j, 50, 50), 1)
- if Snake.run:
- rect = blocks[Snake.body[0][0]][Snake.body[0][1]]
- pygame.draw.rect(win, (0, 150, 0), rect)
- for i in Snake.body:
- Rect = blocks[i[0]][i[1]]
- pygame.draw.rect(win, (0, 255, 0), Rect, 10)
- r = blocks[Food.foodpos[0]][Food.foodpos[1]]
- pygame.draw.rect(win, (255, 0, 0), r, 10)
- pygame.display.update()
- print('Your score was ' + str(Snake.score))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement