forked from FrBrGeorge/PyGame
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathballs.py
More file actions
168 lines (137 loc) · 4.37 KB
/
balls.py
File metadata and controls
168 lines (137 loc) · 4.37 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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env python
# coding: utf
import pygame
import random
SIZE = 640, 480
def intn(*arg):
return map(int,arg)
def Init(sz):
'''Turn PyGame on'''
global screen, screenrect
pygame.init()
screen = pygame.display.set_mode(sz)
screenrect = screen.get_rect()
class GameMode:
'''Basic game mode class'''
def __init__(self):
self.background = pygame.Color("black")
def Events(self,event):
'''Event parser'''
pass
def Draw(self, screen):
screen.fill(self.background)
def Logic(self, screen):
'''What to calculate'''
pass
def Leave(self):
'''What to do when leaving this mode'''
pass
def Init(self):
'''What to do when entering this mode'''
pass
class Ball:
'''Simple ball class'''
def __init__(self, filename, pos = (0.0, 0.0), speed = (0.0, 0.0)):
'''Create a ball from image'''
self.fname = filename
self.surface = pygame.image.load(filename)
self.rect = self.surface.get_rect()
self.speed = speed
self.pos = pos
self.newpos = pos
self.active = True
def draw(self, surface):
surface.blit(self.surface, self.rect)
def action(self):
'''Proceed some action'''
if self.active:
self.pos = self.pos[0]+self.speed[0], self.pos[1]+self.speed[1]
def logic(self, surface):
x,y = self.pos
dx, dy = self.speed
if x < self.rect.width/2:
x = self.rect.width/2
dx = -dx
elif x > surface.get_width() - self.rect.width/2:
x = surface.get_width() - self.rect.width/2
dx = -dx
if y < self.rect.height/2:
y = self.rect.height/2
dy = -dy
elif y > surface.get_height() - self.rect.height/2:
y = surface.get_height() - self.rect.height/2
dy = -dy
self.pos = x,y
self.speed = dx,dy
self.rect.center = intn(*self.pos)
class Universe:
'''Game universe'''
def __init__(self, msec, tickevent = pygame.USEREVENT):
'''Run a universe with msec tick'''
self.msec = msec
self.tickevent = tickevent
def Start(self):
'''Start running'''
pygame.time.set_timer(self.tickevent, self.msec)
def Finish(self):
'''Shut down an universe'''
pygame.time.set_timer(self.tickevent, 0)
class GameWithObjects(GameMode):
def __init__(self, objects=[]):
GameMode.__init__(self)
self.objects = objects
def locate(self, pos):
return [obj for obj in self.objects if obj.rect.collidepoint(pos)]
def Events(self, event):
GameMode.Events(self, event)
if event.type == Game.tickevent:
for obj in self.objects:
obj.action()
def Logic(self, surface):
GameMode.Logic(self, surface)
for obj in self.objects:
obj.logic(surface)
def Draw(self, surface):
GameMode.Draw(self, surface)
for obj in self.objects:
obj.draw(surface)
class GameWithDnD(GameWithObjects):
def __init__(self, *argp, **argn):
GameWithObjects.__init__(self, *argp, **argn)
self.oldpos = 0,0
self.drag = None
def Events(self, event):
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
click = self.locate(event.pos)
if click:
self.drag = click[0]
self.drag.active = False
self.oldpos = event.pos
elif event.type == pygame.MOUSEMOTION and event.buttons[0]:
if self.drag:
self.drag.pos = event.pos
self.drag.speed = event.rel
elif event.type == pygame.MOUSEBUTTONUP and event.button == 1:
self.drag.active = True
self.drag = None
GameWithObjects.Events(self, event)
Init(SIZE)
Game = Universe(50)
Run = GameWithDnD()
for i in xrange(5):
x, y = random.randrange(screenrect.w), random.randrange(screenrect.h)
dx, dy = 1+random.random()*5, 1+random.random()*5
Run.objects.append(Ball("ball.gif",(x,y),(dx,dy)))
Game.Start()
Run.Init()
again = True
while again:
event = pygame.event.wait()
if event.type == pygame.QUIT:
again = False
Run.Events(event)
Run.Logic(screen)
Run.Draw(screen)
pygame.display.flip()
Game.Finish()
pygame.quit()