-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 9848782
Showing
10 changed files
with
749 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# PatljaPg - make simple PyGame examples even simpler | ||
|
||
PetljaPg is a small Python module intented to reduce boilerplate code in entry level PyGame programming. | ||
|
||
Design principles of PetljaPg: | ||
|
||
- PegljaPg should reduce entry barriers for PyGame learners | ||
- PetljaPg should let a coder to code in PyGame with minimal interference of PetljaPg API | ||
- PetljaPg can be easily dropped somewhere on a PyGame learning path | ||
- PetljaPg need not provide any value to non-beginner developers | ||
|
||
|
||
Initial focus areas of PetljaPg are: | ||
- hide complexity of the main loop | ||
- optionaly hide the initialization code | ||
- enabe some user interaction with reduced complexity of event handling |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import pygame as pg | ||
|
||
class pg_frame: | ||
def __init__(self, rate, seq, surf): | ||
self.sequence = seq | ||
self.events = [] | ||
self.rate = rate | ||
self.period = 1 / rate | ||
self.surface = surf | ||
self._mouse_left_index = 0 | ||
self.mouse_pos = None | ||
|
||
|
||
|
||
def _key_count(self, key): | ||
cnt = 0 | ||
for e in self.events: | ||
if e.type == pg.KEYDOWN and e.key == key: | ||
cnt += 1 | ||
return cnt | ||
|
||
def key_left_count(self): | ||
return self._key_count(pg.K_LEFT) | ||
|
||
def key_right_count(self): | ||
return self._key_count(pg.K_RIGHT) | ||
|
||
def key_up_count(self): | ||
return self._key_count(pg.K_UP) | ||
|
||
def key_down_count(self): | ||
return self._key_count(pg.K_DOWN) | ||
|
||
def key_space_count(self): | ||
return self._key_count(pg.K_SPACE) | ||
|
||
def mouse_left_clicked(self): | ||
while (self._mouse_left_index < len(self.events) and | ||
self.events[self._mouse_left_index].type != pg.MOUSEBUTTONDOWN): | ||
self._mouse_left_index += 1 | ||
if self._mouse_left_index < len(self.events): | ||
e = self.events[self._mouse_left_index] | ||
self.mouse_pos = e.pos | ||
self._mouse_left_index += 1 | ||
return True | ||
else: | ||
return None | ||
|
||
|
||
|
||
|
||
def frames(rate, size=None, caption=None): | ||
if pg.get_init(): | ||
if size is not None: | ||
raise ValueError("Argument 'size' is not expected when pygame is allready initialized") | ||
if caption is not None: | ||
raise ValueError("Argument 'caption' is not expected when pygame is allready initialized") | ||
surf = None | ||
manage_init = False | ||
else: | ||
pg.init() | ||
if size is not None: | ||
surf = pg.display.set_mode(size) | ||
else: | ||
surf = pg.display.set_mode((500,500)) | ||
if caption is not None: | ||
pg.display.set_caption(caption) | ||
pg.key.set_repeat(500, 10) | ||
manage_init = True | ||
|
||
sat = pg.time.Clock() | ||
kraj = False | ||
seq = 0 | ||
while not kraj: | ||
frm = pg_frame(rate, seq, surf) | ||
seq += 1 | ||
for dogadjaj in pg.event.get(): | ||
if dogadjaj.type == pg.QUIT: | ||
kraj = True | ||
else: | ||
frm.events.append(dogadjaj) | ||
yield frm | ||
pg.display.update() | ||
sat.tick(rate) | ||
if manage_init: | ||
pg.quit() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import pygame as pg | ||
import petljapg | ||
|
||
tri_boje = [pg.Color("red"), pg.Color("yellow"), pg.Color("green")] | ||
|
||
for frm in petljapg.frames(rate=1, size=(400,400)): | ||
boja = tri_boje[frm.sequence % 3] | ||
pg.draw.circle(frm.surface, boja, (200,200), 100) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import pygame as pg | ||
import petljapg | ||
|
||
x = 150 | ||
y = 150 | ||
|
||
for frm in petljapg.frames(rate=30, size=(500,300)): | ||
platno = frm.surface | ||
platno.fill(pg.Color("white")) | ||
x = x + frm.key_right_count() - frm.key_left_count() | ||
y = y + frm.key_down_count() - frm.key_up_count() | ||
boja = pg.Color("red") | ||
pg.draw.circle(platno, boja, (x, y), 30) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import pygame as pg | ||
import petljapg | ||
|
||
pg.init() # inicijalizujemo rad biblioteke PyGame | ||
pg.display.set_caption("Хватање јабука") # otvaramo prozor | ||
(sirina, visina) = (500, 300) | ||
platno = pg.display.set_mode((sirina, visina)) | ||
|
||
pg.key.set_repeat(10, 10) # podešavamo dogadjaje tastature | ||
|
||
x = 150 | ||
y = 150 | ||
|
||
# pygame je vec inicijalizovan | ||
for frm in petljapg.frames(rate=30): | ||
platno.fill(pg.Color("white")) | ||
x = x + frm.key_right_count() - frm.key_left_count() | ||
y = y + frm.key_down_count() - frm.key_up_count() | ||
boja = pg.Color("red") | ||
pg.draw.circle(platno, boja, (x, y), 30) | ||
|
||
# ako se ne managuje init u frames(), ne manageuje se ni quit | ||
pg.quit() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import pygame as pg | ||
import petljapg | ||
|
||
x = 150 | ||
y = 150 | ||
|
||
tri_boje = [pg.Color("red"), pg.Color("yellow"), pg.Color("green")] | ||
izbor_boje = 0 | ||
|
||
# u elementarnim primerima ne insistiramo na podeli ažuriranja stanja i crtanja | ||
# u nekom trenutku ćemo izvući crtanje u posebnu funkciju | ||
for frm in petljapg.frames(rate=30, size=(500,300)): | ||
platno = frm.surface | ||
platno.fill(pg.Color("black")) | ||
x = x + frm.key_right_count() - frm.key_left_count() | ||
y = y + frm.key_down_count() - frm.key_up_count() | ||
izbor_boje = (izbor_boje + frm.key_space_count()) % 3 | ||
boja = tri_boje[izbor_boje] | ||
pg.draw.circle(platno, boja, (x, y), 30) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import pygame as pg | ||
import petljapg | ||
|
||
|
||
x = 150 | ||
y = 150 | ||
vx = 0 | ||
vy = 0 | ||
|
||
# crtanje možemo lako odvojiti u posebnu funkciju, ali na to | ||
# možemo da pređemo kada nam bude zaista zatrebalo, ne moramo | ||
# od početka | ||
|
||
def crtaj(platno): | ||
platno.fill(pg.Color("white")) | ||
boja = pg.Color("red") | ||
pg.draw.circle(platno, boja, (int(x), int(y)), 30) | ||
|
||
for frm in petljapg.frames(rate=30, size=(500,300)): | ||
sirina, visina = frm.surface.get_size() | ||
|
||
vx = vx + frm.key_right_count() - frm.key_left_count() | ||
x = (x + vx * frm.period) % sirina | ||
|
||
vy = vy + frm.key_down_count() - frm.key_up_count() | ||
y = (y + vy * frm.period) % visina | ||
|
||
crtaj(frm.surface) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import pygame as pg | ||
import petljapg | ||
|
||
x = 150 | ||
y = 150 | ||
|
||
#Evo kako bismo mogli da pojednostavimo reagovanje na klika miša | ||
for frm in petljapg.frames(rate=30, size=(500,300)): | ||
platno = frm.surface | ||
platno.fill(pg.Color("white")) | ||
if frm.mouse_left_clicked(): | ||
x, y = frm.mouse_pos | ||
pg.draw.circle(platno, pg.Color("blue"), (x, y), 50) | ||
|