-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgame10.py
More file actions
54 lines (40 loc) · 1.15 KB
/
game10.py
File metadata and controls
54 lines (40 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# importing necessary libraries
import pygame
from pygame.locals import *
import random
# initiating pygame modules
pygame.init()
# setting the window size
width = 600
height = 600
screen = pygame.display.set_mode((width, height))
# setting window caption
pygame.display.set_caption("Generative Art With Mouse Input")
# defining colors using RGB
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
# setting size for shapes
size = 10
# loop for trapping key and mouse events
while True :
# handling different events
for event in pygame.event.get() :
# if quit event is present
if event.type == pygame.QUIT :
pygame.quit()
exit(0)
# if mouse is pressed
if event.type == MOUSEBUTTONDOWN :
# creating random numbers
a, b = random.randint(-1, 1), random.randint(-1, 1)
# obtaining mouse pressed position
x, y = pygame.mouse.get_pos()
# calculating new coordinates
x1 = x + (a * size)
y1 = y + (b * size)
# drawing line using draw.line with new coordinates
pygame.draw.line(screen, green, (x, y), (x1,
y1), size)
# updating display
pygame.display.update()