-
Notifications
You must be signed in to change notification settings - Fork 2
/
4-sprite.py
executable file
·89 lines (63 loc) · 2.22 KB
/
4-sprite.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# -*- coding: utf-8 -*-
#4: RECT, SPRITE AND GROUP
import pygame as pg
import numpy as np
pg.init()
screen = pg.display.set_mode((640, 480))
position=(0,0)
image=pg.image.load('data/ball.png')
image2=pg.image.load('data/ball2.png')
# A pygame.Rect is an object that is convenient
# for dealing with 2d surfaces
rect= image.get_rect() #Rectangle created from the ball image
print "Size of the image:", rect.size
print "Center of the image:", rect.center
print "Top-right corner of the image:", rect.topright
if rect.collidepoint( (21,7)):
print "The image contains the point (21,7)",
def create_balls(screen):
#Instead of blitting images, we can draw them as groups of sprites
group=pg.sprite.Group() #A group of sprites
for i in range(10):
#A sprite, which has two attributes:
#.image, which is a surface
#.rect, whose top-left corner and size gives the sprite position and size
sprite=pg.sprite.Sprite()
sprite.image=image.copy()
sprite.rect=sprite.image.get_rect() #Create the rect from the image
#Put all the balls at the center of the screen
sprite.rect.center=screen.get_rect().center
#Add the sprites to the group
group.add(sprite)
return group
def move_ball(group):
for sprite in group:
#Create a 2-vector of integers comprised between -1 and 1
displacement=np.random.random_integers(-1,1,2)
#Move the sprite by that vector
sprite.rect.center+= displacement
def touch_ball(group):
#Change the ball image if the mouse lays over it
mousepos=pg.mouse.get_pos()
for sprite in group:
if sprite.rect.collidepoint(mousepos):
sprite.image=image2
else:
sprite.image=image
group=create_balls(screen)
alive=True
while alive:
events=pg.event.get()
for e in events:
if e.type==pg.KEYDOWN:
if e.unicode:
print "KEY PRESSED:", e.unicode
if e.unicode=='q':
print "Quit"
alive=False
move_ball(group)
touch_ball(group)
screen.fill( (0,0,0))
#Draw all the sprites in group on screen, at the positions given by their rects
group.draw(screen)
pg.display.flip()