what is pygame userevent

Solutions on MaxInterview for what is pygame userevent by the best coders in the world

showing results for - "what is pygame userevent"
Mila
21 Jul 2017
1import pygame
2from pygame import display, font, time, mouse, draw
3from pygame.math import Vector2
4import random
5import sys
6
7class Snake:
8    def __init__(self):
9        self.body = [Vector2(5, 10), Vector2(6, 10), Vector2(7, 10)]
10        self.direction = Vector2(1, 0)
11    def draw_snake(self):
12        for block in self.body:
13            x_pos = int(block.x * cSize)
14            y_pos = int(block.y * cSize)
15            block_rect = pygame.Rect(x_pos, y_pos, cSize, cSize)
16            draw.rect(screen, (183, 111, 122), block_rect)
17
18    def move_snake(self):
19        body_copy = self.body[:-1]
20        body_copy.insert(0, body_copy[0] + self.direction)
21        self.body = body_copy[:]
22class Fruit:
23    def __init__(self):
24        self.x = random.randint(0, cNumber - 1)
25        self.y = random.randint(0, cNumber - 1)
26        self.pos = Vector2(self.x, self.y)
27
28    def draw_fruit(self):
29        fruit_rect = pygame.Rect(int(self.pos.x * cSize), int(self.pos.y * cSize), cSize, cSize)
30        draw.rect(screen, (255, 75, 30), fruit_rect)
31
32pygame.init()
33clock = time.Clock()
34
35# Game Variables
36WIDTH = 800
37HEIGHT = 600
38FPS = 60
39cSize = 36
40cNumber = 18
41
42# Game Window
43screen = display.set_mode((cNumber * cSize, cNumber * cSize))
44display.set_caption('Snake Game in pyGame')
45
46fruit = Fruit()
47snake = Snake()
48
49SCREEN_UPDATE = pygame.USEREVENT
50time.set_timer(SCREEN_UPDATE, 150)
51# Main Game Loop
52while True:
53    for event in pygame.event.get():
54        if event.type == pygame.QUIT:
55            pygame.quit()
56            sys.exit()
57        if event.type == pygame.MOUSEBUTTONDOWN:
58            pos = mouse.get_pos()
59            print(pos)
60        if event.type == SCREEN_UPDATE:
61            snake.move_snake
62
63    screen.fill((200, 230, 100))
64    fruit.draw_fruit()
65    snake.draw_snake()
66    display.update()
67    clock.tick(FPS)