import pygame, sys # Imports pygame and sys libraries from pygame.locals import * # Imports pygame local library for constant variables pygame.init() # Has to be used before any python functions, activates python library screen = pygame.display.set_mode((400, 300)) # Sets the entire screen, width/height background = pygame.Surface((400, 300)) pygame.display.set_caption("Hello world!") # Sets the title for the game in the game window surface = pygame.Surface((10, 10)) # Creates a surface surface.fill((0,255,0)) # Colours the surface green, without fill a surface defaults to black #print_message = pygame.USEREVENT + 0 # 24 - 32 for user events. This has a value of 24. +1 for value of 25 #pygame.time.set_timer(print_message, 3000) # 3000 milliseconds pos = [180, 130] # square position while True: # Main game loop, stop when game is quit events = pygame.event.get() for event in events: if event.type == QUIT: # Checks for quit event pygame.quit() # Quits pygame, use before sys.exit, deactivates python library, opposite of pygame.init() sys.exit() # Quits sys #if event.type == print_message: # Prints hello world every 3s #print("Hello World") while 1: pressed_keys = pygame.key.get_pressed() if pressed_keys[K_a]: pos[0] -= 1 if pressed_keys[K_d]: pos[0] += 1 if pressed_keys[K_w]: pos[1] -= 1 if pressed_keys[K_s]: pos[1] += 1 break screen.blit(background, (0, 0)) # Paints over the square screen.blit(surface, pos) # Draws a surface object onto another pygame.display.update() # Updates the game display # pygame.Surface.copy() - used to create an exact copy of another surface object # pygame.Surface.get_size() - returns the width and height of a surface object # pygame.time.set_timer(print_message, 0) - stops the event for printing hello world every 3s # self.rect.colliderect(sprite.rect) - returns true if two objacts overlap, otherwise returns false # pygame.sprite.spritecollideany(sprite, group) - detects if a sprite is in collision with any group sprites # pygame.sprite.groupcollide(group1, group2) - can also have 3rd & 4th parameters (dokill1, dokill2) # takes true or false values and if a value is true, the sprites from corresponding group are removed """ if pygame.sprite.spritecollideany(P1, enemies): DISPLAY.SURF.fill(RED) pygame.display.update() for entity in all_sprites: entity.kill() time.sleep(2) pygame.quit() sys.exit() """ """ fonts: pygame.font.Font() is for .ttf files like pygame.font.Font("arial.ttf", 20), also has font size with "20" font = pygame.font.Font(None, size) will make program use system default font pygame.font.SysFont() is alternative if no .ttf files pygame.font.get_fonts() will return every font it finds on your system Initates the font with a variable and renders with text: dialogue_font = pygame.font.SysFont("arial", 15) dialogue = dialogue_font.render("Hey there, Beautiful weather today!", True, (0,0,0)) screen.blit(dialogue, (40,40)) "True" parameter stands for anti-aliasing which makes edges smoother """ # music.play() with no parameters/0 plays audio clip once, -1 plays indefinitely. any other number repeats it that amount of times # music.queue(), music.stop(), music.pause(), music.unpause() # music.set_endevent() takes a parameter and triggers corresponding event once music stops playing """ SONG_END_EVENT = pygame.USEREVENT + 1 while True: for event in pygame.event.get(): if event.type == SONG_END: execute code here """ # pygame.mixer.music.rewind() # pygame.mixer.music.set_volume() – Values: 0.0-1.0. Settings reset when new music plays # pygame.mixer.music.get_busy() – Checks if music is playing, returns True/False # sound_effect.play() to play sound effects # pygame.draw.polygon(surface, color, pointlist, width) # pygame.draw.line(surface, color, start_point, end_point, width) # pygame.draw.lines(surface, color, closed, pointlist, width) # pygame.draw.circle(surface, color, center_point, radius, width) # pygame.draw.ellipse(surface, color, bounding_rectangle, width) # pygame.draw.rect(surface, color, rectangle_tuple, width) # surface - Window object to which we want to draw the shape # start_point/end_point - a tuple/list consisting of an x and y value pair, which defines the starting and ending position of where the line will be drawn # width lines - default value 1, width < 1 lione not drawn, values above 1 increases line thickness # width polygons - default value 0, means polygon filled with colour, values above 0 makes it unfilled and width controls border thickness # closed - specifies whether first and last points have a line between them, false by default # pointlist - a list of x-y coordinate pairs, where each adjacent pair is connected to the other with a line # center - Tuple/List which defines coordinate pair for the center of origin of the Circle. # bounding_rectangle - Takes a Rect object which defines the rectangle it will be confined in. # rectangle_tuple - x coordinate, y coordinate, width of rectangle, height of rectangle