1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | # Importing pygame import pygame # Initializing pygame pygame.init() # Setting window and title window = pygame.display.set_mode((800,450)) pygame.display.set_caption('Mover') # Defining colors black = (0,0,0) white = (255, 255, 255) # Image mario = pygame.image.load('m1.png').convert_alpha() bg = pygame.image.load('bg.jpg') bg = pygame.transform.scale(bg, (800,450)) gun = pygame.image.load('gun.png') gun = pygame.transform.scale(gun, (140, 95)) # Running running = True # Defining the clock clock = pygame.time.Clock() # Identifying coordinates x, y = 0,0 moveX, moveY = 0,0 # Main Loop while running: for event in pygame.event.get(): # If X is clicked, running = False, which causes loop to exit. if event.type == pygame.QUIT: running = False # If key is pressed, add {} to (var) if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: moveX = -5 if event.key == pygame.K_RIGHT: moveX = 5 if event.key == pygame.K_UP: moveY = -5 if event.key == pygame.K_DOWN: moveY = 5 # Same if key, except all (var) = 0 if event.type == pygame.KEYUP: if event.key == pygame.K_LEFT: moveX = 0 if event.key == pygame.K_RIGHT: moveX = 0 if event.key == pygame.K_UP: moveY = 0 if event.key == pygame.K_DOWN: moveY = 0 # Make background color white window.blit(bg, (0,0)) # Define x and y x += moveX y += moveY # Draw a rectangle, set it to relative coordinates # pygame.draw.rect(window, black, (x, y, 25, 25)) # Set image to coordinates window.blit(gun, (x,y)) # Set frames per second clock.tick(50) # Required command pygame.display.flip() # Exit when loop is False pygame.quit() |
Python | O.M.G lol pygame is SO GREAT.
SO I FINALLY LEARNT HOW TO ADD IMAGES, USE KEY EVENTS, AND SET BACKGROUNDS, I MADE SOMETHING THAT WILL REVOLUTIONIZE THE WORLD (aka it's useless)