From f39a9bd15e9adf19360ad71fed8554d55ab80ef9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Fri, 13 Mar 2020 01:50:33 +0100 Subject: [PATCH] Pygame --- README.md | 68 +++++++++++++++++++++++++++--------------------------- index.html | 68 +++++++++++++++++++++++++++--------------------------- 2 files changed, 68 insertions(+), 68 deletions(-) diff --git a/README.md b/README.md index 7dbc09b..3056b00 100644 --- a/README.md +++ b/README.md @@ -2919,10 +2919,10 @@ Pygame import pygame as pg pg.init() screen = pg.display.set_mode((500, 500)) -rect = pg.Rect(235, 235, 30, 30) +rect = pg.Rect(240, 240, 20, 20) while all(event.type != pg.QUIT for event in pg.event.get()): - keys = {pg.K_UP: (0, -3), pg.K_RIGHT: (3, 0), pg.K_DOWN: (0, 3), pg.K_LEFT: (-3, 0)} - for delta in {keys.get(i) for i, on in enumerate(pg.key.get_pressed()) if on}: + deltas = {pg.K_UP: (0, -3), pg.K_RIGHT: (3, 0), pg.K_DOWN: (0, 3), pg.K_LEFT: (-3, 0)} + for delta in {deltas.get(i) for i, on in enumerate(pg.key.get_pressed()) if on}: rect = rect.move(delta) if delta else rect screen.fill((0, 0, 0)) pg.draw.rect(screen, (255, 255, 255), rect) @@ -2932,59 +2932,59 @@ while all(event.type != pg.QUIT for event in pg.event.get()): ### Rect **Object for storing rectangular coordinates.** ```python - = pg.Rect(topleft_x, topleft_y, width, height) - = .topleft/topright/bottomright/bottomleft/center - = .x/y/centerx/centery - = .move() # Or: .move(, ) + = pg.Rect(topleft_x, topleft_y, width, height) + = .x/y/centerx/centery + = .topleft/topright/bottomright/bottomleft/center + = .move() ``` ```python - = .collidepoint() # Or: .collidepoint(, ) - = .colliderect() -index = .collidelist() # Returns index of first coliding Rect or -1. -indices = .collidelistall() # Returns indices of all colinding Rects. + = .collidepoint() # Tests if a point is inside a rectangle. + = .colliderect() # Tests if two rectangles overlap. + = .collidelist() # Returns index of first colliding Rect or -1. + = .collidelistall() # Returns indices of all colliding Rects. ``` ### 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. + = pg.display.set_mode((width, height)) # Returns 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. +.set_at((x, y), color) # Updates pixel. +.fill(color) # 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)) + = pg.transform.flip(, xbool, ybool) + = pg.transform.rotate(, degrees) + = pg.transform.scale(, (width, height)) ``` ```python -pg.draw.line(, color, start_pos, end_pos, width) -pg.draw.arc(, color, , start_angle, stop_angle) -pg.draw.rect(, color, ) -pg.draw.polygon(, color, points) -pg.draw.ellipse(, color, ) +pg.draw.line(, color, start_pos, end_pos, width) +pg.draw.arc(, color, , start_radians, stop_radians) +pg.draw.rect(, color, ) +pg.draw.polygon(, color, points) +pg.draw.ellipse(, color, ) ``` ### Font ```python - = pg.font.SysFont(name, size, bold=False, italic=False) - = pg.font.Font('', size) - = .render(text, antialias, color, background=None) + = pg.font.SysFont(name, size, bold=False, italic=False) + = pg.font.Font('', size) + = .render(text, antialias, color, background=None) ``` ### Sound ``` - = pg.mixer.Sound('') # Loads a sound file. -.play() # Starts playing sound. + = pg.mixer.Sound('') # Loads a sound file. +.play() # Starts playing the sound. ``` ### Basic Mario Brothers Example @@ -2992,9 +2992,9 @@ pg.draw.ellipse(, color, ) 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 = 50, P(5, 10) # Screen size, Speed limit +P = collections.namedtuple('P', 'x y') # Position +D = enum.Enum('D', 'n e s w') # Direction +SIZE, MAX_SPEED = 50, P(5, 10) # Screen size, Speed limit def main(): def get_screen(): diff --git a/index.html b/index.html index a23dbd4..bea5a0d 100644 --- a/index.html +++ b/index.html @@ -2482,10 +2482,10 @@ simpleaudio.play_buffer(samples_b, 1, import pygame as pg pg.init() screen = pg.display.set_mode((500, 500)) -rect = pg.Rect(235, 235, 30, 30) +rect = pg.Rect(240, 240, 20, 20) while all(event.type != pg.QUIT for event in pg.event.get()): - keys = {pg.K_UP: (0, -3), pg.K_RIGHT: (3, 0), pg.K_DOWN: (0, 3), pg.K_LEFT: (-3, 0)} - for delta in {keys.get(i) for i, on in enumerate(pg.key.get_pressed()) if on}: + deltas = {pg.K_UP: (0, -3), pg.K_RIGHT: (3, 0), pg.K_DOWN: (0, 3), pg.K_LEFT: (-3, 0)} + for delta in {deltas.get(i) for i, on in enumerate(pg.key.get_pressed()) if on}: rect = rect.move(delta) if delta else rect screen.fill((0, 0, 0)) pg.draw.rect(screen, (255, 255, 255), rect) @@ -2493,54 +2493,54 @@ rect = pg.Rect(235, 2 -

Rect

Object for storing rectangular coordinates.

<Rect>  = pg.Rect(topleft_x, topleft_y, width, height)
-<tuple> = <Rect>.topleft/topright/bottomright/bottomleft/center
-<int>   = <Rect>.x/y/centerx/centery
-<Rect>  = <Rect>.move(<tuple>)                   # Or: <Rect>.move(<int>, <int>)
+

Rect

Object for storing rectangular coordinates.

<Rect> = pg.Rect(topleft_x, topleft_y, width, height)
+<int>  = <Rect>.x/y/centerx/centery
+<tup.> = <Rect>.topleft/topright/bottomright/bottomleft/center
+<Rect> = <Rect>.move(<tuple>)
 
-
<bool>  = <Rect>.collidepoint(<tuple>)           # Or: <Rect>.collidepoint(<int>, <int>)
-<bool>  = <Rect>.colliderect(<Rect>)
-index   = <Rect>.collidelist(<list_of_Rect>)     # Returns index of first coliding Rect or -1.
-indices = <Rect>.collidelistall(<list_of_Rect>)  # Returns indices of all colinding Rects.
+
<bool> = <Rect>.collidepoint(<tuple>)           # Tests if a point is inside a rectangle.
+<bool> = <Rect>.colliderect(<Rect>)             # Tests if two rectangles overlap.
+<int>  = <Rect>.collidelist(<list_of_Rect>)     # Returns index of first colliding Rect or -1.
+<list> = <Rect>.collidelistall(<list_of_Rect>)  # Returns indices of all colliding Rects.
 
-

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

Object for representing images.

<Surf> = pg.display.set_mode((width, height))   # Returns the display surface.
+<Surf> = pg.Surface((width, height))            # Creates a new surface.
+<Surf> = 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))                 # Draws passed surface to the surface.
-<Surface> = <Surface>.subsurface(<Rect>)          # Returns subsurface.
+
<Surf>.set_at((x, y), color)                    # Updates pixel.
+<Surf>.fill(color)                              # Fills the whole surface.
+<Surf>.blit(<Surface>, (x, y))                  # Draws passed surface to the surface.
+<Surf> = <Surf>.subsurface(<Rect>)              # Returns subsurface.
 
-
<Surface> = pg.transform.flip(<Surface>, xbool, ybool)
-<Surface> = pg.transform.rotate(<Surface>, angle)
-<Surface> = pg.transform.scale(<Surface>, (width, height))
+
<Surf> = pg.transform.flip(<Surf>, xbool, ybool)
+<Surf> = pg.transform.rotate(<Surf>, degrees)
+<Surf> = pg.transform.scale(<Surf>, (width, height))
 
-
pg.draw.line(<Surface>, color, start_pos, end_pos, width)
-pg.draw.arc(<Surface>, color, <Rect>, start_angle, stop_angle)
-pg.draw.rect(<Surface>, color, <Rect>)
-pg.draw.polygon(<Surface>, color, points)
-pg.draw.ellipse(<Surface>, color, <Rect>)
+
pg.draw.line(<Surf>, color, start_pos, end_pos, width)
+pg.draw.arc(<Surf>, color, <Rect>, start_radians, stop_radians)
+pg.draw.rect(<Surf>, color, <Rect>)
+pg.draw.polygon(<Surf>, color, points)
+pg.draw.ellipse(<Surf>, color, <Rect>)
 
-

Font

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

Font

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

Sound

<Sound> = pg.mixer.Sound('<path>')  # Loads a sound file.
-<Sound>.play()                      # Starts playing sound.
+

Sound

<Sound> = pg.mixer.Sound('<path>')              # Loads a sound file.
+<Sound>.play()                                  # Starts playing the sound.
 

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 = 50, P(5, 10)             # Screen size, Speed limit
+P = collections.namedtuple('P', 'x y')          # Position
+D = enum.Enum('D', 'n e s w')                   # Direction
+SIZE, MAX_SPEED = 50, P(5, 10)                  # Screen size, Speed limit
 
 def main():
     def get_screen():