Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import random
- from time import sleep
- from pygame.locals import *
- BLACK = (0,0,0)
- WHITE = (255,255,255)
- window_height = 600
- window_width = 800
- screen = pygame.display.set_mode((window_width, window_height))
- pygame.display.set_caption("Snake")
- fps = 10
- fps_clock = pygame.time.Clock()
- pygame.font.init()
- font = pygame.font.SysFont('Comic San MS', 25)
- class Food:
- def __init__(self,x,y):
- self.x = x
- self.y = y
- def generate(self):
- apple = pygame.image.load('apple.png')
- apple = pygame.transform.scale(apple, (25,25))
- screen.blit(apple,(self.x, self.y))
- # pygame.draw.rect(screen, WHITE, pygame.Rect(self.x, self.y, 10, 10))
- class Snake:
- def __init__(self, x, y, food_pos):
- self.cells =[(x-40,y),(x-20,y),(x,y)]
- self.dir_x = 1
- self.dir_y = 0
- self.food_pos = food_pos
- def move(self):
- del self.cells[0]
- last_cell = self.cells[-1]
- self.cells.append((last_cell[0]+self.dir_x*20, last_cell[1]+self.dir_y*20))
- def draw(self):
- head = pygame.image.load('snake.png')
- body = pygame.image.load('snake.png')
- if self.dir_x == 1 and self.dir_y == 0:
- pass
- elif self.dir_x == -1 and self.dir_y == 0:
- head = pygame.transform.rotate(head, 180)
- body = pygame.transform.flip(body, True, False)
- elif self.dir_x == 0 and self.dir_y == 1:
- head = pygame.transform.rotate(head, -90)
- body = pygame.transform.rotate(body, -90)
- else:
- head = pygame.transform.rotate(head, 90)
- body = pygame.transform.rotate(body, 90)
- head = pygame.transform.scale(head, (18, 18))
- body = pygame.transform.scale(body, (18, 18))
- for i in self.cells:
- if i != self.cells[-1]:
- screen.blit(body, (i[0], i[1]))
- else:
- screen.blit(head, (i[0], i[1]))
- # pygame.draw.rect(screen, WHITE, pygame.Rect(i[0],i[1],18,18))
- def grow(self):
- self.cells.insert(0, self.cells[0])
- def hit_ceiling(self):
- if self.cells[-1][1] <=0:
- return True
- else:
- return False
- def hit_floor(self):
- if self.cells[-1][1] + 20 >= window_height:
- return True
- else:
- return False
- def hit_wall(self):
- if self.cells[-1][0] <= 0 or self.cells[-1][0]+20 >= window_width:
- return True
- else:
- return False
- class Score:
- def __init__(self):
- self.score = 0
- def draw(self):
- textsurface = font.render('Your Score: {0}'.format(self.score), False, WHITE)
- screen.blit(textsurface, (window_width-150, 40))
- class Game:
- def __init__(self):
- self.q = 0
- snake_x = window_width/2
- snake_y = window_height/2
- x = 20 * random.randint(1, 19)
- y = 20 * random.randint(1, 14)
- self.food = Food(x, y)
- self.food.generate()
- self.snake = Snake(snake_x, snake_y, (self.food.x, self.food.y))
- self.score = Score()
- def collision(self, x1, y1, x2, y2):
- if x2 < x1 < x2 + 20:
- if y2 < y1 < y2 + 20:
- return True
- else:
- return False
- def draw(self):
- screen.fill(BLACK)
- img = pygame.image.load('background.jpg')
- img = pygame.transform.scale(img, (window_width-40, window_height-40))
- screen.blit(img, (20,20))
- # pygame.draw.rect(screen, BLACK, (10, 10, window_width-20, window_height-20))
- def update(self):
- self.draw()
- self.food.generate()
- self.snake.food_pos = (self.food.x, self.food.y)
- self.snake.move()
- self.snake.draw()
- if self.snake.hit_ceiling() or self.snake.hit_floor() or self.snake.hit_wall():
- self.q = 1
- for i in range(len(self.snake.cells)-1):
- if self.snake.cells[i] == self.snake.cells[-1]:
- self.q = 1
- if self.collision(self.snake.cells[-1][0]+10, self.snake.cells[-1][1]+10, self.food.x, self.food.y):
- x = 20 * random.randint(1, 38)
- y = 20 * random.randint(1, 28)
- self.food = Food(x, y)
- self.snake.grow()
- self.food.generate()
- self.score.score += 1
- self.score.draw()
- def main():
- pygame.init()
- game = Game()
- flag = True
- while flag:
- for event in pygame.event.get():
- if event.type == KEYDOWN:
- if event.key == 273 and not game.snake.dir_y == 1:
- game.snake.dir_y = -1
- game.snake.dir_x = 0
- elif event.key == 274 and not game.snake.dir_y == -1:
- game.snake.dir_y = 1
- game.snake.dir_x = 0
- elif event.key == 276 and not game.snake.dir_x == 1:
- game.snake.dir_y = 0
- game.snake.dir_x = -1
- elif event.key == 275 and not game.snake.dir_x == -1:
- game.snake.dir_y = 0
- game.snake.dir_x = 1
- if event.type == QUIT:
- flag = False
- break
- game.update()
- if game.q == 1:
- screen.fill(BLACK)
- font = pygame.font.SysFont('Comic San MS', 35)
- gameover = font.render("Game Over", 1, WHITE)
- w = gameover.get_width()
- h = gameover.get_height()
- screen.blit(gameover, (window_width//2 - w//2, window_height//2 - h//2))
- pygame.display.update()
- fps_clock.tick(fps)
- if __name__ == '__main__':
- main()
Add Comment
Please, Sign In to add comment