View difference between Paste ID: THHLXXxj and 4YFpvVLZ
SHOW: | | - or go back to the newest paste.
1
import pygame
2
import random
3
import time
4
from Direction import Direction
5
from Snake import Snake
6
from Apple import Apple
7
from Egg import Egg
8
9
# width and height of the display
10
DISPLAY_WIDTH = 800
11
DISPLAY_HEIGHT = 608
12
Points = 0
13
14
# background creation
15-
background = pygame.Surface((800, 608))
15+
background = pygame.Surface((DISPLAY_WIDTH, DISPLAY_HEIGHT))
16
for i in range(25):
17
    for j in range(19):
18
        image = pygame.image.load("images/background.png")
19
        mask = (random.randrange(0, 20), random.randrange(
20
            0, 20), random.randrange(0, 20))
21
22
        image.fill(mask, special_flags=pygame.BLEND_ADD)
23
        background.blit(image, (i*32, j*32))
24
25
# settings
26
pygame.init()
27
pygame.font.init()
28
# display object and game clock
29
display = pygame.display.set_mode([DISPLAY_WIDTH, DISPLAY_HEIGHT])
30
clock = pygame.time.Clock()
31
# font object
32
my_font = pygame.font.SysFont('Comic Sans MS', 24)
33
34
# Snake
35
snake = Snake()
36
MOVE_SNAKE = pygame.USEREVENT + 1
37
pygame.time.set_timer(MOVE_SNAKE, 200)
38
39
# apples
40
apple = Apple()
41
apples = pygame.sprite.Group()
42
apples.add(apple)
43
44
# eggs
45
eggs = pygame.sprite.Group()
46
47
game_on = True
48
while game_on:
49
    for event in pygame.event.get():
50
        if event.type == pygame.KEYDOWN:
51
            if event.key == pygame.K_ESCAPE:
52
                game_on = False
53
            if event.key == pygame.K_w:
54
                snake.change_direction(Direction.UP)
55
            if event.key == pygame.K_s:
56
                snake.change_direction(Direction.DOWN)
57
            if event.key == pygame.K_a:
58
                snake.change_direction(Direction.LEFT)
59
            if event.key == pygame.K_d:
60
                snake.change_direction(Direction.RIGHT)
61
62
        elif event.type == MOVE_SNAKE:
63
            snake.update()
64
        elif event.type == pygame.QUIT:
65
            game_on = False
66
67
    # collision detection of snake's head and apples
68
    collision_with_apple = pygame.sprite.spritecollideany(snake, apples)
69
    if collision_with_apple != None:
70
        collision_with_apple.kill()
71
        snake.eat_apple()
72
        apple = Apple()
73
        apples.add(apple)
74
        Points += 1
75
76
        # adding eggs
77
        if (Points % 5) == 0:
78
            egg = Egg(snake.segments[-1].last_position)
79
            eggs.add(egg)
80
81
    # collision detection of snake's head and eggs
82
    collision_with_egg = pygame.sprite.spritecollideany(snake, eggs)
83
    if collision_with_egg != None:
84
        game_on = False
85
86
    # drawing background
87
    display.blit(background, (0, 0))
88
    # drawing segments
89
    snake.draw_segments(display)
90
    # drawing snake's head
91
    display.blit(snake.image, snake.rect)
92
    # drawing apples
93
    for apple in apples:
94
        display.blit(apple.image, apple.rect)
95
96
    # drawing eggs
97
    for egg in eggs:
98
        display.blit(egg.image, egg.rect)
99
100
    # displaying points
101
    result_text = my_font.render(
102
        f'Points: {Points}', False, (0, 0, 0))
103
    display.blit(result_text, (16, 16))
104
    # check if the game is ending
105
    if snake.collision_check():
106
        loose_text = my_font.render(
107
            'You lost', False, (200, 0, 0))
108
        display.blit(loose_text,
109
                     (DISPLAY_WIDTH/2-50, DISPLAY_HEIGHT/2))
110
        game_on = False
111
112
    pygame.display.flip()
113
    clock.tick(30)
114
115
time.sleep(3)
116
pygame.quit()
117