Pages

Se afișează postările cu eticheta 2018. Afișați toate postările
Se afișează postările cu eticheta 2018. Afișați toate postările

luni, 4 iunie 2018

PyGame : First interface - part 8.

The PyGame - first interface is a series of python tutorials using PyGame python module and other python modules that help us create a good interface.
If you look at the Wikipedia website then the first phrase is clear:
  In computing, an interface is a shared boundary across which two or more separate components of a computer system exchange information.
Today I will start with ThorPy python module.
This python module comes with the version 1.5.9.
You can read more about this python and test examples at the official webpage.
You can install this python module with the pip tool:

c:\Python364\Scripts>pip install thorpy
Collecting thorpy
  Downloading https://files.pythonhosted.org/packages/09/aa/...
.../thorpy-1.6.tar.gz (373kB)
    100% |████████████████████████████████| 378kB 374kB/s
<5 .10="" python_version=""><2 .0.0=""><2 .0.0="">...
Installing collected packages: thorpy
Successfully installed thorpy-1.6

This is the result of submenus from official website.

duminică, 28 ianuarie 2018

PyGame : Test with an animated image.

This is a simple tutorial about how to create a bouncing ball effect with pygame python module.
The source code is very simple and you need a transparent image named earth.png .
The variables I used is size, speed, ball, ballrect.
I used ballrect with  get_rect().
The result get pygame Rect object.
This  has several virtual attributes which can be used to move and align the Rect:

  • x,y
  • top, left, bottom, right
  • topleft, bottomleft, topright, bottomright
  • midtop, midleft, midbottom, midright
  • center, centerx, centery
  • size, width, height
  • w,h

All of these attributes can be assigned to ballrect variable.
import sys
import pygame
pygame.init()
 
size = width, height = 640, 420
speed = [1, 1]
black = 0, 0, 0
 
screen = pygame.display.set_mode(size)
 
ball = pygame.image.load("earth.png")
ballrect = ball.get_rect()
 
while 1:
    for event in pygame.event.get():
        if event.type == pygame.QUIT: sys.exit()
 
    ballrect = ballrect.move(speed)
    if ballrect.left < 0 or ballrect.right > width:
        speed[0] = -speed[0]
    if ballrect.top < 0 or ballrect.bottom > height:
        speed[1] = -speed[1]
 
    screen.fill(black)
    screen.blit(ball, ballrect)
    pygame.display.flip()

pygame.display.update()
The result of this source code :