-
Notifications
You must be signed in to change notification settings - Fork 2
/
3-numpy.py
executable file
·55 lines (40 loc) · 1.27 KB
/
3-numpy.py
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
55
# -*- coding: utf-8 -*-
#2: SURFACES - FILLING, LOADING, BLITTING
import pygame as pg
import numpy as np
pg.init()
screen = pg.display.set_mode((640, 480))
def paint(screen,mode=0):
size=(640,480,3) #X,Y,Color (RGB)
array=np.zeros(size) #Initialize the array with zeros
for i in range(size[0]):
for j in range(size[1]):
x,y=(i+.5)/size[0],(j+.5)/size[1]
#Color channels must be comprised between 0 and 255 and integer
if mode==0:
#Normal mode
color=(255*x,255*y,0 )
else:
#Psychedelic mode
color=(255*x,255*y,255./(x**2+y**2) )
array[i,j,:]=np.array(color,dtype='int')
pg.surfarray.blit_array(screen,array)
paint(screen,0)
alive=True
while alive:
events=pg.event.get()
for e in events:
if e.type==pg.QUIT:
#When the user closes the window
alive=False
if e.type==pg.KEYDOWN:
if e.unicode:
print "KEY PRESSED:", e.unicode
if e.unicode=='q':
print "Quit"
alive=False
if e.unicode=='p':
print 'Psychedelic mode'
paint(screen,1)
#Renew the screen
pg.display.flip()