-
Notifications
You must be signed in to change notification settings - Fork 19
/
pygame_test.py
131 lines (103 loc) · 3.39 KB
/
pygame_test.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#! /usr/bin/env python
'''
An example of using the collision_resolver module
Pops up a window populated by randomly placed little squares.
You control the big square with the direction keys.
'''
from random import randint
import pygame
from pygame.locals import *
RESOLUTION = (600,400)
green = (5,255,5)
avatar = None
avatarGroup = None
sceen = None
background = None
origBackground = None
screen = None
def main():
clock = pygame.time.Clock()
prepare()
while True:
clock.tick(2)
retval = iterate()
if not retval:
return
def prepare():
global screen
pygame.init()
screen = pygame.display.set_mode(RESOLUTION)
Start()
def iterate():
avatar.moveState[0] = randint(-9,9)
avatar.moveState[1] = randint(-9,9)
for ev in pygame.event.get():
if ev.type == QUIT:
return False
if ev.type == KEYDOWN and ev.key == K_ESCAPE:
return False
#clear
avatarGroup.clear( screen, background )
#update
avatarGroup.update()
#draw
avatarGroup.draw(screen)
pygame.display.update()
return True
def Start():
global avatar, avatarGroup, background, origBackground, screen
background = pygame.Surface( RESOLUTION )
off_black = (40,10,0)
background.fill( off_black )
# avatar will be a green square in the center of the screen
avatar = Avatar()
fixedBackgroundSprites = pygame.sprite.Group()
for block in GenerateRandomBlocks(30, RESOLUTION):
if not block.rect.colliderect(avatar.rect):
fixedBackgroundSprites.add( block )
fixedBackgroundSprites.draw( background )
avatar.collidables = fixedBackgroundSprites
avatarGroup = pygame.sprite.Group()
avatarGroup.add( avatar )
screen.blit( background, (0,0) )
pygame.display.flip()
origBackground = background.copy()
class Avatar(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface( (100,100) )
self.image.fill( green )
self.rect = self.image.get_rect()
self.rect.center = (RESOLUTION[0]/2, RESOLUTION[1]/2)
self.moveState = [0,0]
self.collidables = None
def update(self):
if self.moveState[0] or self.moveState[1]:
screen = pygame.display.get_surface()
screen.blit( origBackground, (0,0) )
self.rect.move_ip(*self.moveState)
class SimpleSprite(pygame.sprite.Sprite):
def __init__(self, surface):
pygame.sprite.Sprite.__init__(self)
self.image = surface
self.rect = self.image.get_rect()
def GenerateRandomBlocks( howMany, positionBounds ):
lowerColorBound = (100,100,100)
upperColorBound = (200,200,200)
lowerXBound, lowerYBound = 0,0
upperXBound, upperYBound = positionBounds
lowerWidthBound, lowerHeightBound = 30,30
upperWidthBound, upperHeightBound = 60,60
for i in range(howMany):
color = [ randint(lowerColorBound[i],upperColorBound[i])
for i in range(3) ]
pos = [ randint(lowerXBound, upperXBound),
randint(lowerYBound,upperYBound) ]
size = [ randint(lowerWidthBound, upperWidthBound),
randint(lowerHeightBound, upperHeightBound) ]
s = SimpleSprite( pygame.Surface(size) )
s.image.fill( color )
s.rect.topleft = pos
yield s
if __name__ == '__main__':
main()