Skip to content

Commit

Permalink
Added blitting images
Browse files Browse the repository at this point in the history
  • Loading branch information
Grzetan committed Jun 7, 2021
1 parent 3093a06 commit cefcd3d
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 11 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,22 @@ Example:
self.pygameZoom.draw_polygon((255, 255, 255), [(0.653, 789.234), (100,100), (345, 890.2)],0)
```

# Blitting images in pygameZoom

To blit an image add this line to draw shapes section:

```python
image = pygame.image.load("image.jpg")
self.pygameZoom.blit(image, (x, y))
```
x and y are the coordinates of the top right corner.

Remember!!!
When you zoom into blitted image, this image will slowly lose quality.
Zooming without quality loss work only with shapes.

--------------------------

Great se we finished writing our class. Don't forget to make an instance of Window class.

```python
Expand Down
23 changes: 13 additions & 10 deletions exmaple.py → example.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pygame
import math
import sys
from pygameZoom import PygameZoom


class Window:
Expand All @@ -11,27 +12,29 @@ def __init__(self):
self.CLOCK = pygame.time.Clock()
self.FPS = 30
self.run = True
self.vz = VZ(500, 400)
self.vz.set_background((255, 0, 0))
self.pygameZoom = PygameZoom(500, 400)
self.pygameZoom.set_background((255, 0, 0))
self.loop()

def drawTree(self, a, b, pos, deepness):
if deepness:
c = a + int(math.cos(math.radians(pos)) * deepness * 10.0)
d = b + int(math.sin(math.radians(pos)) * deepness * 10.0)
self.vz.draw_line((127, 255, 0), a, b, c, d, 1)
self.pygameZoom.draw_line((127, 255, 0), a, b, c, d, 1)
self.drawTree(c, d, pos - 25, deepness - 1)
self.drawTree(c, d, pos + 25, deepness - 1)

def refresh_window(self):
self.WIN.fill(0)
self.drawTree(500, 800, -90, 12)
self.vz.draw_ellipse((255, 255, 0), (200, 200, 100, 100))
self.vz.draw_circle((255, 255, 0), 200, 200, 20)
self.vz.draw_rect((0, 0, 0), 100, 100, 100, 100)
self.vz.draw_line((255, 255, 255), 0, 0, 200, 200)
# self.vz.draw_polygon((0,0,255), [(200,400),(300,2),(400,400)])
self.vz.render(self.WIN, (100, 100))
# self.drawTree(500, 800, -90, 12)
# self.pygameZoom.draw_ellipse((255, 255, 0), (200, 200, 100, 100))
# self.pygameZoom.draw_circle((255, 255, 0), 200, 200, 20)
# self.pygameZoom.draw_rect((0, 0, 0), 100, 100, 100, 100)
# self.pygameZoom.draw_line((255, 255, 255), 0, 0, 200, 200)
# self.pygameZoom.draw_polygon((0,0,255), [(200,400),(300,2),(400,400)])
surface = pygame.image.load("download.jpeg")
self.pygameZoom.blit(surface, (100, 100))
self.pygameZoom.render(self.WIN, (100, 100))
pygame.display.update()

def events(self):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name='pygameZoom',
version='0.0.1',
version='0.0.3',
description='Zoom into pygame figures without quality loss',
long_description=description,
long_description_content_type="text/markdown",
Expand Down
14 changes: 14 additions & 0 deletions src/pygameZoom.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,17 @@ def draw(self, surface):
points.append(self.outer.map_point(p[0], p[1]))
pygame.draw.polygon(surface, self.color, points, self.width)

class Blit(object):
def __init__(self, surface, start,outer):
super().__init__()
self.surface = surface
self.start = start
self.outer = outer

def draw(self, surface):
scaled = pygame.transform.scale(self.surface, (int(self.surface.get_width() * self.outer.zoom), int(self.surface.get_height() * self.outer.zoom)))
surface.blit(scaled, self.outer.map_point(self.start[0], self.start[1]))

def map_point(self, x, y):
new_x = (x - self.boundaries[0]) * self.zoom
new_y = (y - self.boundaries[2]) * self.zoom
Expand All @@ -108,6 +119,9 @@ def draw_ellipse(self, color, rect, width=0):
def draw_polygon(self, color, points, width=0):
self.shapes.append(self.Polygon(color, points, width, self))

def blit(self, surface, start):
self.shapes.append(self.Blit(surface, start,self))

def set_background(self, value):
self.background = value

Expand Down

0 comments on commit cefcd3d

Please sign in to comment.