From e6c56f2422d344624bcee21c9fca6b421fd20aa3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Wed, 11 Mar 2020 11:44:19 +0100 Subject: [PATCH] PyGame --- README.md | 76 ++++++++++++++++++++++++------------------------------ index.html | 64 +++++++++++++++++++-------------------------- 2 files changed, 59 insertions(+), 81 deletions(-) diff --git a/README.md b/README.md index b5fdea6..fb956c1 100644 --- a/README.md +++ b/README.md @@ -2927,31 +2927,6 @@ while all(event.type != pg.QUIT for event in pg.event.get()): pg.display.flip() ``` -### Surface -**Object for representing images.** -```python - = pg.display.set_mode((width, height)) # Retruns the display surface. - = pg.Surface((width, height)) # Creates a new surface. - = pg.image.load('').convert() # Loads an image. - = .convert() # Converts to screen format. - = .convert_alpha() # Converts to screen format including alphas. -``` -* **If no arguments are passed the new Surface will have the same pixel format as the display Surface. This is always the fastest format for blitting. It is a good idea to convert all Surfaces before they are blitted many times.** - -```python -.set_at((x, y), ) # Updates pixel. -.fill() # Fills the whole surface. -.blit(, (x, y)/) # Draws passed surface to the surface. -.blit(, (x, y)/) # Draws one image onto another. - = .subsurface() # Returns subsurface. -``` - -```python - = pg.transform.flip(, xbool, ybool) - = pg.transform.rotate(, angle) - = pg.transform.scale(, (width, height)) -``` - ### Rect **Object for storing rectangular coordinates.** ```python @@ -2978,7 +2953,28 @@ indices = .collidelistall() # Returns indices of all colind [(key, value), ...] = .collidedictall() ``` -### Draw +### Surface +**Object for representing images.** +```python + = pg.display.set_mode((width, height)) # Retruns the display surface. + = pg.Surface((width, height)) # Creates a new surface. + = pg.image.load('').convert() # Loads an image. +``` + +```python +.set_at((x, y), ) # Updates pixel. +.fill() # Fills the whole surface. +.blit(, (x, y)/) # Draws passed surface to the surface. + = .subsurface() # Returns subsurface. +``` + +```python + = pg.transform.flip(, xbool, ybool) + = pg.transform.rotate(, angle) + = pg.transform.scale(, (width, height)) +``` + +#### Drawing: ```python pg.draw.rect(, color, ) pg.draw.polygon(, color, points) @@ -2989,24 +2985,17 @@ pg.draw.line(, color, start_pos, end_pos, width) pg.draw.lines(, color, points) ``` -### Mixer +#### Fonts: ```python -pygame.mixer.init # initialize the mixer module -pygame.mixer.pre_init # preset the mixer init arguments -pygame.mixer.stop # stop playback of all sound channels -pygame.mixer.set_num_channels # set the total number of playback channels -pygame.mixer.set_reserved # reserve channels from being automatically used -pygame.mixer.find_channel # find an unused channel -pygame.mixer.Sound # Create a new Sound object from a file or buffer object -pygame.mixer.Channel # Create a Channel object for controlling playback + = pg.font.SysFont(name, size, bold=False, italic=False) + = pg.font.Font('', size) + = .render(text, antialias, color, background=None) ``` -```python -pygame.mixer.music.load('test.wav') -pygame.mixer.music.play() -pygame.mixer.music.rewind() -pygame.mixer.music.stop() -pygame.mixer.music.set_volume() +### Sound +``` + = pg.mixer.Sound('') # Loads a sound file. +.play() # Starts playing sound. ``` ### Basic Mario Brothers Example @@ -3016,7 +3005,7 @@ from random import randint P = collections.namedtuple('P', 'x y') # Position D = enum.Enum('D', 'n e s w') # Direction -SIZE, MAX_SPEED = 25, P(5, 10) # Screen size, Speed limit +SIZE, MAX_SPEED = 50, P(5, 10) # Screen size, Speed limit def main(): def get_screen(): @@ -3038,13 +3027,14 @@ def main(): run(get_screen(), get_images(), get_mario(), get_tiles()) def run(screen, images, mario, tiles): + clock = pygame.time.Clock() while all(event.type != pygame.QUIT for event in pygame.event.get()): keys = {pygame.K_UP: D.n, pygame.K_RIGHT: D.e, pygame.K_DOWN: D.s, pygame.K_LEFT: D.w} pressed = {keys.get(i) for i, on in enumerate(pygame.key.get_pressed()) if on} update_speed(mario, tiles, pressed) update_position(mario, tiles) draw(screen, images, mario, tiles, pressed) - pygame.time.wait(28) + clock.tick(28) def update_speed(mario, tiles, pressed): x, y = mario.spd diff --git a/index.html b/index.html index 3ac6c8d..33dd4ba 100644 --- a/index.html +++ b/index.html @@ -2491,27 +2491,6 @@ rect = pg.Rect(235, 2 -

Surface

Object for representing images.

<Surface> = pg.display.set_mode((width, height))  # Retruns the display surface.
-<Surface> = pg.Surface((width, height))           # Creates a new surface.
-<Surface> = pg.image.load('<path>').convert()     # Loads an image.
-<Surface> = <Surface>.convert()                   # Converts to screen format.
-<Surface> = <Surface>.convert_alpha()             # Converts to screen format including alphas.
-
- - -
    -
  • If no arguments are passed the new Surface will have the same pixel format as the display Surface. This is always the fastest format for blitting. It is a good idea to convert all Surfaces before they are blitted many times.
  • -
-
<Surface>.set_at((x, y), <color>)                 # Updates pixel.
-<Surface>.fill(<color>)                           # Fills the whole surface.
-<Surface>.blit(<Surface>, (x, y)/<Rect>)          # Draws passed surface to the surface. 
-<Surface>.blit(<Surface>, (x, y)/<Rect>)          # Draws one image onto another.
-<Surface> = <Surface>.subsurface(<Rect>)          # Returns subsurface.
-
-
<Surface> = pg.transform.flip(<Surface>, xbool, ybool)
-<Surface> = pg.transform.rotate(<Surface>, angle)
-<Surface> = pg.transform.scale(<Surface>, (width, height))
-

Rect

Object for storing rectangular coordinates.

<Rect>  = pg.Rect(topleft_x, topleft_y, width, height)  # x, y, w/width, h/height
 <int>   = <Rect>.x/y/centerx/centery/bottom/left/right/top
 <tuple> = <Rect>.topleft/center/topright/bottomright/bottomleft
@@ -2532,7 +2511,22 @@ indices = <Rect>.collidelistall(<list_of_Rect>)  Draw
pg.draw.rect(<Surface>, color, <Rect>)
+

Surface

Object for representing images.

<Surface> = pg.display.set_mode((width, height))  # Retruns the display surface.
+<Surface> = pg.Surface((width, height))           # Creates a new surface.
+<Surface> = pg.image.load('<path>').convert()     # Loads an image.
+
+ + +
<Surface>.set_at((x, y), <color>)                 # Updates pixel.
+<Surface>.fill(<color>)                           # Fills the whole surface.
+<Surface>.blit(<Surface>, (x, y)/<Rect>)          # Draws passed surface to the surface.
+<Surface> = <Surface>.subsurface(<Rect>)          # Returns subsurface.
+
+
<Surface> = pg.transform.flip(<Surface>, xbool, ybool)
+<Surface> = pg.transform.rotate(<Surface>, angle)
+<Surface> = pg.transform.scale(<Surface>, (width, height))
+
+

Drawing:

pg.draw.rect(<Surface>, color, <Rect>)
 pg.draw.polygon(<Surface>, color, points)
 pg.draw.circle(<Surface>, color, center, radius)
 pg.draw.ellipse(<Surface>, color, <Rect>)
@@ -2541,28 +2535,21 @@ pg.draw.line(<Surface>, color, start_pos, end_pos, width)
 pg.draw.lines(<Surface>, color, points)
 
-

Mixer

pygame.mixer.init              # initialize the mixer module
-pygame.mixer.pre_init          # preset the mixer init arguments
-pygame.mixer.stop              # stop playback of all sound channels
-pygame.mixer.set_num_channels  # set the total number of playback channels
-pygame.mixer.set_reserved      # reserve channels from being automatically used
-pygame.mixer.find_channel      # find an unused channel
-pygame.mixer.Sound             # Create a new Sound object from a file or buffer object
-pygame.mixer.Channel           # Create a Channel object for controlling playback
+

Fonts:

<Font>    = pg.font.SysFont(name, size, bold=False, italic=False)
+<Font>    = pg.font.Font('<path>', size)
+<Surface> = <Font>.render(text, antialias, color, background=None)
+
+ +

Sound

<Sound> = pg.mixer.Sound('<path>')  # Loads a sound file.
+<Sound>.play()                      # Starts playing sound.
 
-
pygame.mixer.music.load('test.wav')
-pygame.mixer.music.play()
-pygame.mixer.music.rewind()
-pygame.mixer.music.stop()
-pygame.mixer.music.set_volume(<float>)
-

Basic Mario Brothers Example

import collections, dataclasses, enum, io, math, pygame, urllib.request, itertools as it
 from random import randint
 
 P = collections.namedtuple('P', 'x y')     # Position
 D = enum.Enum('D', 'n e s w')              # Direction
-SIZE, MAX_SPEED = 25, P(5, 10)             # Screen size, Speed limit
+SIZE, MAX_SPEED = 50, P(5, 10)             # Screen size, Speed limit
 
 def main():
     def get_screen():
@@ -2584,13 +2571,14 @@ SIZE, MAX_SPEED = 25, P(def run(screen, images, mario, tiles):
+    clock = pygame.time.Clock()
     while all(event.type != pygame.QUIT for event in pygame.event.get()):
         keys = {pygame.K_UP: D.n, pygame.K_RIGHT: D.e, pygame.K_DOWN: D.s, pygame.K_LEFT: D.w}
         pressed = {keys.get(i) for i, on in enumerate(pygame.key.get_pressed()) if on}
         update_speed(mario, tiles, pressed)
         update_position(mario, tiles)
         draw(screen, images, mario, tiles, pressed)
-        pygame.time.wait(28)
+        clock.tick(28)
 
 def update_speed(mario, tiles, pressed):
     x, y = mario.spd