├── .gitignore ├── Part 1 ├── hello.py └── paths.py ├── Part 10 ├── aliens.py ├── assets │ ├── background.png │ ├── final_level.png │ ├── lose_screen.png │ ├── next_level.png │ ├── shield1.png │ ├── shield2.png │ ├── start_screen.png │ ├── them_pellet.png │ ├── them_ship.png │ ├── win_screen.png │ ├── you_pellet.png │ ├── you_ship copy.png │ └── you_ship.png ├── gameLevels.py ├── projectiles.py ├── ships.py └── sounds │ ├── enemy_laser.wav │ └── player_laser.wav ├── Part 2 ├── moving.py └── properties.py ├── Part 3 ├── keyboard.py └── mouse.py ├── Part 4 ├── Code │ ├── assets │ │ ├── game_over.jpg │ │ └── title.jpg │ └── drop.py └── Images │ ├── Diagrams │ ├── DictionariesAndLists.png │ ├── Drop.png │ └── append_platform.png │ └── Screenshots │ ├── Title-Screen.png │ ├── difficulty.png │ └── game_over.png ├── Part 5 └── Code │ ├── assets │ ├── images │ │ ├── Large │ │ │ ├── cat.png │ │ │ ├── chicken.png │ │ │ ├── cow.png │ │ │ ├── dog.png │ │ │ ├── horse.png │ │ │ ├── mouse.png │ │ │ ├── pig.png │ │ │ ├── rooster.png │ │ │ └── sheep.png │ │ ├── cat.png │ │ ├── chicken.png │ │ ├── cow.png │ │ ├── dog.png │ │ ├── horse.png │ │ ├── mouse.png │ │ ├── pig.png │ │ ├── rooster.png │ │ ├── sheep.png │ │ └── stop.png │ └── sounds │ │ ├── MP3 │ │ ├── cat.mp3 │ │ ├── chicken.mp3 │ │ ├── cow.mp3 │ │ ├── dog.mp3 │ │ ├── farm.mp3 │ │ ├── horse.mp3 │ │ ├── mouse.mp3 │ │ ├── pig.mp3 │ │ ├── rooster.mp3 │ │ └── sheep.mp3 │ │ └── OGG │ │ ├── cat.ogg │ │ ├── chicken.ogg │ │ ├── cow.ogg │ │ ├── dog.ogg │ │ ├── farm.ogg │ │ ├── horse.ogg │ │ ├── mouse.ogg │ │ ├── pig.ogg │ │ ├── rooster.ogg │ │ └── sheep.ogg │ └── sounds.py ├── Part 6 └── Code │ ├── assets │ ├── back.png │ ├── background.jpg │ ├── earth.png │ ├── jupiter.png │ ├── logo.png │ ├── mars.png │ ├── mercury.png │ ├── neptune.png │ ├── saturn.png │ ├── tabs.png │ ├── uranus.png │ └── venus.png │ ├── simulator.py │ └── solarsystem.py ├── Part 7 └── collisions.py ├── Part 8 ├── assets │ ├── Barrel.png │ ├── Barrel_break.png │ ├── Fred-Left-Hit.png │ ├── Fred-Left.png │ ├── Fred-Right-Hit.png │ ├── Fred-Right.png │ ├── background.png │ ├── gameover.png │ └── startgame.png ├── freds_bad_day.py └── objects.py ├── Part 9 ├── aliens.py ├── assets │ ├── background.png │ ├── start_screen.png │ ├── them_pellet.png │ ├── them_ship.png │ ├── you_pellet.png │ └── you_ship.png ├── projectiles.py ├── ships.py └── sounds │ ├── enemy_laser.wav │ └── player_laser.wav └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | *.psd 2 | *.pyc 3 | -------------------------------------------------------------------------------- /Part 1/hello.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | 3 | pygame.init() 4 | 5 | window = pygame.display.set_mode( (500, 400) ) 6 | 7 | while True: 8 | 9 | ''' 10 | #Our Rectangle code 11 | pygame.draw.rect(window, (255,0,0), (0, 0, 50, 50)) 12 | pygame.draw.rect(window, (0,255,0), (40, 0, 50, 50)) FROM HERE 13 | pygame.draw.rect(window, (0,0,255), (80, 0, 50, 50)) 14 | pygame.draw.rect(window, (0,255,0), (40, 0, 50, 50)) #To here 15 | ''' 16 | 17 | ''' 18 | #Our Circle code 19 | # pygame.draw.circle(window, (255,255,0), (250, 200), 20, 0) 20 | 21 | #Filled 22 | pygame.draw.circle(window, (255,255,0), (200, 200), 20, 0) 23 | 24 | #Not filled 25 | pygame.draw.circle(window, (255,255,0), (300, 200), 20, 2) 26 | ''' 27 | 28 | #Our Ellipse Code 29 | 30 | # pygame.draw.ellipse(window, (255, 0, 0), (100, 100, 100, 50)) 31 | # pygame.draw.ellipse(window, (0, 255, 0), (100, 150, 80, 40)) 32 | # pygame.draw.ellipse(window, (0, 0, 255), (100, 190, 60, 30)) 33 | 34 | pygame.draw.rect(window, (255, 0, 0), (100, 100, 100, 50), 2) 35 | pygame.draw.ellipse(window, (255, 0, 0), (100, 100, 100, 50)) 36 | 37 | pygame.draw.rect(window, (0, 255, 0), (100, 150, 80, 40), 2) 38 | pygame.draw.ellipse(window, (0, 255, 0), (100, 150, 80, 40)) 39 | 40 | pygame.draw.rect(window, (0, 0, 255), (100, 190, 60, 30), 2) 41 | pygame.draw.ellipse(window, (0, 0, 255), (100, 190, 60, 30)) 42 | 43 | #Circle 44 | pygame.draw.ellipse(window, (0, 0, 255), (100, 250, 40, 40)) 45 | 46 | pygame.display.update() -------------------------------------------------------------------------------- /Part 1/paths.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | 3 | pygame.init() 4 | 5 | window = pygame.display.set_mode( (500, 400) ) 6 | 7 | while True: 8 | 9 | # A triangle made of lines 10 | # pygame.draw.line(window, (255,255,255), (50, 50), (75, 75), 1) 11 | # pygame.draw.line(window, (255,255,255), (75, 75), (25, 75), 1) 12 | # pygame.draw.line(window, (255,255,255), (25, 75), (50, 50), 1) 13 | 14 | # A triangle made of points 15 | 16 | pygame.draw.lines(window, (255,255,255), True, ((50, 50), (75, 75), (25, 75)), 1) 17 | 18 | #A pentagon 19 | pygame.draw.lines(window, (255,255,255), True, ( (50, 50), (75, 75), (63, 100), (38, 100), (25, 75) ), 1) 20 | 21 | 22 | pygame.display.update() -------------------------------------------------------------------------------- /Part 10/aliens.py: -------------------------------------------------------------------------------- 1 | import pygame, sys, random, math 2 | import pygame.locals as GAME_GLOBALS 3 | import pygame.event as GAME_EVENTS 4 | import pygame.time as GAME_TIME 5 | import ships 6 | 7 | import gameLevels 8 | 9 | windowWidth = 1024 10 | windowHeight = 614 11 | timeTick = 0 12 | 13 | pygame.init() 14 | pygame.font.init() 15 | clock = pygame.time.Clock() 16 | surface = pygame.display.set_mode((windowWidth, windowHeight), pygame.FULLSCREEN|pygame.HWSURFACE|pygame.DOUBLEBUF) 17 | 18 | pygame.display.set_caption('Alien\'s Are Gonna Kill Me!') 19 | textFont = pygame.font.SysFont("monospace", 50) 20 | 21 | gameStarted = False 22 | gameStartedTime = 0 23 | gameFinishedTime = 0 24 | gameOver = False 25 | gameWon = False 26 | 27 | currentLevel = 0 28 | currentWave = 0 29 | lastSpawn = 0 30 | nextLevelTS = 0 31 | 32 | #Mouse Variables 33 | mousePosition = (0,0) 34 | mouseStates = None 35 | mouseDown = False 36 | 37 | #Image Variables 38 | startScreen = pygame.image.load("assets/start_screen.png") 39 | background = pygame.image.load("assets/background.png") 40 | loseScreen = pygame.image.load("assets/lose_screen.png") 41 | winScreen = pygame.image.load("assets/win_screen.png") 42 | nextWave = pygame.image.load("assets/next_level.png") 43 | finalWave = pygame.image.load("assets/final_level.png") 44 | 45 | #Ships 46 | ship = ships.Player(windowWidth / 2, windowHeight, pygame, surface) 47 | enemyShips = [] 48 | 49 | leftOverBullets = [] 50 | 51 | #Sound Setup 52 | pygame.mixer.init() 53 | 54 | def launchWave(): 55 | 56 | global lastSpawn, currentWave, currentLevel, gameOver, gameWon, nextLevelTS 57 | 58 | thisLevel = gameLevels.level[currentLevel]["structure"] 59 | 60 | if currentWave < len(thisLevel): 61 | 62 | thisWave = thisLevel[currentWave] 63 | 64 | for idx, enemyAtThisPosition in enumerate(thisWave): 65 | if enemyAtThisPosition is 1: 66 | enemyShips.append(ships.Enemy(((windowWidth / len(thisWave)) * idx), -60, pygame, surface, 1)) 67 | 68 | elif currentLevel + 1 < len(gameLevels.level) : 69 | currentLevel += 1 70 | currentWave = 0 71 | ship.shields = ship.maxShields 72 | nextLevelTS = timeTick + 5000 73 | else: 74 | gameWon = True 75 | 76 | lastSpawn = timeTick 77 | currentWave += 1 78 | 79 | def updateGame(): 80 | 81 | global mouseDown, gameOver, gameWon, leftOverBullets 82 | 83 | if mouseStates[0] is 1 and mouseDown is False: 84 | ship.fire() 85 | mouseDown = True 86 | elif mouseStates[0] is 0 and mouseDown is True: 87 | mouseDown = False 88 | 89 | ship.setPosition(mousePosition) 90 | 91 | enemiesToRemove = [] 92 | 93 | for idx, enemy in enumerate(enemyShips): 94 | 95 | if enemy.y < windowHeight: 96 | enemy.move() 97 | enemy.tryToFire() 98 | shipIsDestroyed = enemy.checkForHit(ship) 99 | enemyIsDestroyed = ship.checkForHit(enemy) 100 | 101 | if enemyIsDestroyed is True: 102 | enemiesToRemove.append(idx) 103 | 104 | if shipIsDestroyed is True: 105 | gameOver = True 106 | gameWon = False 107 | return 108 | 109 | else: 110 | enemiesToRemove.append(idx) 111 | 112 | oC = 0 113 | 114 | for idx in enemiesToRemove: 115 | for remainingBullets in enemyShips[idx - oC].bullets: 116 | leftOverBullets.append(remainingBullets) 117 | 118 | del enemyShips[idx - oC] 119 | oC += 1 120 | 121 | oC = 0 122 | 123 | for idx, aBullet in enumerate(leftOverBullets): 124 | aBullet.move() 125 | hitShip = aBullet.checkForHit(ship) 126 | 127 | if hitShip is True or aBullet.y > windowHeight: 128 | del leftOverBullets[idx - oC] 129 | oC += 1 130 | 131 | def drawGame(): 132 | 133 | global leftOverBullets, nextLevelTS, timeTick, gameWon 134 | 135 | surface.blit(background, (0, 0)) 136 | ship.draw() 137 | ship.drawBullets() 138 | 139 | for aBullet in leftOverBullets: 140 | aBullet.draw() 141 | 142 | healthColor = [(62, 180, 76), (180, 62, 62)] 143 | whichColor = 0 144 | 145 | if(ship.health <= 1): 146 | whichColor = 1 147 | 148 | for enemy in enemyShips: 149 | enemy.draw() 150 | enemy.drawBullets() 151 | 152 | pygame.draw.rect(surface, healthColor[whichColor], (0, windowHeight - 5, (windowWidth / ship.maxHealth) * ship.health, 5)) 153 | pygame.draw.rect(surface, (62, 145, 180), (0, windowHeight - 10, (windowWidth / ship.maxShields) * ship.shields, 5)) 154 | 155 | if timeTick < nextLevelTS: 156 | if gameWon is True: 157 | surface.blit(finalWave, (250, 150)) 158 | else: 159 | surface.blit(nextWave, (250, 150)) 160 | 161 | def restartGame(): 162 | global gameOver, gameStart, currentLevel, currentWave, lastSpawn, nextLevelTS, leftOverBullets, gameWon, enemyShips, ship 163 | 164 | gameOver = False 165 | gameWon = False 166 | currentLevel = 0 167 | currentWave = 0 168 | lastSpawn = 0 169 | nextLevelTS = 0 170 | leftOverBullets = [] 171 | enemyShips = [] 172 | ship.health = ship.maxHealth 173 | ship.shields = ship.maxShields 174 | ship.bullets = [] 175 | 176 | def quitGame(): 177 | pygame.quit() 178 | sys.exit() 179 | 180 | # 'main' loop 181 | while True: 182 | timeTick = GAME_TIME.get_ticks() 183 | mousePosition = pygame.mouse.get_pos() 184 | mouseStates = pygame.mouse.get_pressed() 185 | 186 | if gameStarted is True and gameOver is False: 187 | 188 | updateGame() 189 | drawGame() 190 | 191 | elif gameStarted is False and gameOver is False: 192 | surface.blit(startScreen, (0, 0)) 193 | 194 | if mouseStates[0] is 1: 195 | 196 | if mousePosition[0] > 445 and mousePosition[0] < 580 and mousePosition[1] > 450 and mousePosition[1] < 510: 197 | pygame.mouse.set_visible(False) 198 | gameStarted = True 199 | 200 | elif mouseStates[0] is 0 and mouseDown is True: 201 | mouseDown = False 202 | 203 | elif gameStarted is True and gameOver is True and gameWon is False: 204 | surface.blit(loseScreen, (0, 0)) 205 | timeLasted = (gameFinishedTime - gameStartedTime) / 1000 206 | 207 | if gameStarted is True and gameWon is True and len(enemyShips) is 0: 208 | surface.blit(winScreen, (0, 0)) 209 | 210 | # Handle user and system events 211 | for event in GAME_EVENTS.get(): 212 | 213 | if event.type == pygame.KEYDOWN: 214 | 215 | if event.key == pygame.K_ESCAPE: 216 | quitGame() 217 | 218 | if event.key == pygame.K_SPACE: 219 | if gameStarted is True and gameOver is True or gameStarted is True and gameWon is True: 220 | restartGame() 221 | 222 | if timeTick - lastSpawn > gameLevels.level[currentLevel]["interval"] * 1000 and gameStarted is True and gameOver is False: 223 | launchWave() 224 | 225 | if event.type == GAME_GLOBALS.QUIT: 226 | quitGame() 227 | 228 | clock.tick(60) 229 | pygame.display.update() 230 | -------------------------------------------------------------------------------- /Part 10/assets/background.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 10/assets/background.png -------------------------------------------------------------------------------- /Part 10/assets/final_level.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 10/assets/final_level.png -------------------------------------------------------------------------------- /Part 10/assets/lose_screen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 10/assets/lose_screen.png -------------------------------------------------------------------------------- /Part 10/assets/next_level.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 10/assets/next_level.png -------------------------------------------------------------------------------- /Part 10/assets/shield1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 10/assets/shield1.png -------------------------------------------------------------------------------- /Part 10/assets/shield2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 10/assets/shield2.png -------------------------------------------------------------------------------- /Part 10/assets/start_screen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 10/assets/start_screen.png -------------------------------------------------------------------------------- /Part 10/assets/them_pellet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 10/assets/them_pellet.png -------------------------------------------------------------------------------- /Part 10/assets/them_ship.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 10/assets/them_ship.png -------------------------------------------------------------------------------- /Part 10/assets/win_screen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 10/assets/win_screen.png -------------------------------------------------------------------------------- /Part 10/assets/you_pellet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 10/assets/you_pellet.png -------------------------------------------------------------------------------- /Part 10/assets/you_ship copy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 10/assets/you_ship copy.png -------------------------------------------------------------------------------- /Part 10/assets/you_ship.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 10/assets/you_ship.png -------------------------------------------------------------------------------- /Part 10/gameLevels.py: -------------------------------------------------------------------------------- 1 | level = [ 2 | { 3 | "structure" :[ [0, 1, 0, 1, 0, 1, 0, 1, 0, 1], 4 | [1, 0, 1, 0, 1, 0, 1, 0, 1, 0], 5 | [0, 1, 0, 1, 0, 1, 0, 1, 0, 1], 6 | [1, 0, 1, 0, 1, 0, 1, 0, 1, 0], 7 | [0, 1, 0, 1, 0, 1, 0, 1, 0, 1], 8 | [1, 0, 1, 0, 1, 0, 1, 0, 1, 0], 9 | [0, 1, 0, 1, 0, 1, 0, 1, 0, 1], 10 | [1, 0, 1, 0, 1, 0, 1, 0, 1, 0], 11 | [0, 1, 0, 1, 0, 1, 0, 1, 0, 1], 12 | [1, 0, 1, 0, 1, 0, 1, 0, 1, 0], 13 | [0, 1, 0, 1, 0, 1, 0, 1, 0, 1], 14 | [1, 0, 1, 0, 1, 0, 1, 0, 1, 0], 15 | [0, 1, 0, 1, 0, 1, 0, 1, 0, 1], 16 | [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 17 | [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 18 | [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 19 | ], 20 | "interval" : 4 21 | }, 22 | { 23 | "structure" :[ [0, 0, 0, 0, 0, 1, 1, 1, 1, 1], 24 | [1, 1, 1, 1, 1, 0, 0, 0, 0, 0], 25 | [0, 0, 0, 0, 0, 1, 1, 1, 1, 1], 26 | [1, 1, 1, 1, 1, 0, 0, 0, 0, 0], 27 | [0, 0, 0, 0, 0, 1, 1, 1, 1, 1], 28 | [1, 1, 1, 1, 1, 0, 0, 0, 0, 0], 29 | [0, 0, 0, 0, 0, 1, 1, 1, 1, 1], 30 | [1, 1, 1, 1, 1, 0, 0, 0, 0, 0], 31 | [0, 0, 0, 0, 0, 1, 1, 1, 1, 1], 32 | [1, 1, 1, 1, 1, 0, 0, 0, 0, 0], 33 | [0, 0, 0, 0, 0, 1, 1, 1, 1, 1], 34 | [1, 1, 1, 1, 1, 0, 0, 0, 0, 0], 35 | [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 36 | [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 37 | [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 38 | ], 39 | "interval" : 5 40 | }, 41 | { 42 | "structure" :[ [0, 1, 0, 1, 0, 1, 0, 1, 0, 1], 43 | [1, 0, 1, 0, 1, 0, 1, 0, 1, 0], 44 | [0, 1, 0, 1, 0, 1, 0, 1, 0, 1], 45 | [1, 0, 1, 0, 1, 0, 1, 0, 1, 0], 46 | [0, 1, 0, 1, 0, 1, 0, 1, 0, 1], 47 | [1, 0, 1, 0, 1, 0, 1, 0, 1, 0], 48 | [0, 1, 0, 1, 0, 1, 0, 1, 0, 1], 49 | [1, 0, 1, 0, 1, 0, 1, 0, 1, 0], 50 | [0, 1, 0, 1, 0, 1, 0, 1, 0, 1], 51 | [1, 0, 1, 0, 1, 0, 1, 0, 1, 0], 52 | [0, 1, 0, 1, 0, 1, 0, 1, 0, 1], 53 | [1, 0, 1, 0, 1, 0, 1, 0, 1, 0], 54 | [0, 1, 0, 1, 0, 1, 0, 1, 0, 1], 55 | [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 56 | [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 57 | [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 58 | [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 59 | ], 60 | "interval" : 4 61 | }, 62 | { 63 | "structure" :[ [1, 0, 0, 0, 0, 0, 0, 0, 0, 0], 64 | [0, 1, 0, 0, 0, 0, 0, 0, 0, 0], 65 | [0, 0, 1, 0, 0, 0, 0, 0, 0, 0], 66 | [0, 0, 0, 1, 0, 0, 0, 0, 0, 0], 67 | [0, 0, 0, 0, 1, 0, 0, 0, 0, 0], 68 | [0, 0, 0, 0, 0, 1, 0, 0, 0, 0], 69 | [0, 0, 0, 0, 0, 0, 1, 0, 0, 0], 70 | [0, 0, 0, 0, 0, 0, 0, 1, 0, 0], 71 | [0, 0, 0, 0, 0, 0, 0, 0, 1, 0], 72 | [0, 0, 0, 0, 0, 0, 0, 0, 0, 1], 73 | [0, 0, 0, 0, 0, 0, 0, 0, 1, 0], 74 | [0, 0, 0, 0, 0, 0, 0, 1, 0, 0], 75 | [0, 0, 0, 0, 0, 0, 1, 0, 0, 0], 76 | [0, 0, 0, 0, 0, 1, 0, 0, 0, 0], 77 | [0, 0, 0, 0, 1, 0, 0, 0, 0, 0], 78 | [0, 0, 0, 1, 0, 0, 0, 0, 0, 0], 79 | [0, 0, 1, 0, 0, 0, 0, 0, 0, 0], 80 | [0, 1, 0, 0, 0, 0, 0, 0, 0, 0], 81 | [1, 0, 0, 0, 0, 0, 0, 0, 0, 0], 82 | [0, 1, 0, 0, 0, 0, 0, 0, 0, 0], 83 | [0, 0, 1, 0, 0, 0, 0, 0, 0, 0], 84 | [0, 0, 0, 1, 0, 0, 0, 0, 0, 0], 85 | [0, 0, 0, 0, 1, 0, 0, 0, 0, 0], 86 | [0, 0, 0, 0, 0, 1, 0, 0, 0, 0], 87 | [0, 0, 0, 0, 0, 0, 1, 0, 0, 0], 88 | [0, 0, 0, 0, 0, 0, 0, 1, 0, 0], 89 | [0, 0, 0, 0, 0, 0, 0, 0, 1, 0], 90 | [0, 0, 0, 0, 0, 0, 0, 0, 0, 1], 91 | [0, 0, 0, 0, 0, 0, 0, 0, 1, 0], 92 | [0, 0, 0, 0, 0, 0, 0, 1, 0, 0], 93 | [0, 0, 0, 0, 0, 0, 1, 0, 0, 0], 94 | [0, 0, 0, 0, 0, 1, 0, 0, 0, 0], 95 | [0, 0, 0, 0, 1, 0, 0, 0, 0, 0], 96 | [0, 0, 0, 1, 0, 0, 0, 0, 0, 0], 97 | [0, 0, 1, 0, 0, 0, 0, 0, 0, 0], 98 | [0, 1, 0, 0, 0, 0, 0, 0, 0, 0], 99 | [1, 0, 0, 0, 0, 0, 0, 0, 0, 0], 100 | [0, 1, 0, 0, 0, 0, 0, 0, 0, 0], 101 | [0, 0, 1, 0, 0, 0, 0, 0, 0, 0], 102 | [0, 0, 0, 1, 0, 0, 0, 0, 0, 0], 103 | [0, 0, 0, 0, 1, 0, 0, 0, 0, 0], 104 | [0, 0, 0, 0, 0, 1, 0, 0, 0, 0], 105 | [0, 0, 0, 0, 0, 0, 1, 0, 0, 0], 106 | [0, 0, 0, 0, 0, 0, 0, 1, 0, 0], 107 | [0, 0, 0, 0, 0, 0, 0, 0, 1, 0], 108 | [0, 0, 0, 0, 0, 0, 0, 0, 0, 1], 109 | [0, 0, 0, 0, 0, 0, 0, 0, 1, 0], 110 | [0, 0, 0, 0, 0, 0, 0, 1, 0, 0], 111 | [0, 0, 0, 0, 0, 0, 1, 0, 0, 0], 112 | [0, 0, 0, 0, 0, 1, 0, 0, 0, 0], 113 | [0, 0, 0, 0, 1, 0, 0, 0, 0, 0], 114 | [0, 0, 0, 1, 0, 0, 0, 0, 0, 0], 115 | [0, 0, 1, 0, 0, 0, 0, 0, 0, 0], 116 | [0, 1, 0, 0, 0, 0, 0, 0, 0, 0], 117 | [1, 0, 0, 0, 0, 0, 0, 0, 0, 0], 118 | [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 119 | [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 120 | [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 121 | [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 122 | ], 123 | "interval" : 4 124 | }, 125 | { 126 | "structure" :[ [0, 0, 0, 0, 1, 1, 0, 0, 0, 0], 127 | [0, 0, 0, 1, 0, 0, 1, 0, 0, 0], 128 | [0, 0, 1, 0, 0, 0, 0, 1, 0, 0], 129 | [0, 1, 0, 0, 0, 0, 0, 0, 1, 0], 130 | [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], 131 | [0, 1, 0, 0, 0, 0, 0, 0, 1, 0], 132 | [0, 0, 1, 0, 0, 0, 0, 1, 0, 0], 133 | [0, 0, 0, 1, 0, 0, 1, 0, 0, 0], 134 | [0, 0, 0, 0, 1, 1, 0, 0, 0, 0], 135 | [0, 0, 0, 1, 0, 0, 1, 0, 0, 0], 136 | [0, 0, 1, 0, 0, 0, 0, 1, 0, 0], 137 | [0, 1, 0, 0, 0, 0, 0, 0, 1, 0], 138 | [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], 139 | [0, 1, 0, 0, 0, 0, 0, 0, 1, 0], 140 | [0, 0, 1, 0, 0, 0, 0, 1, 0, 0], 141 | [0, 0, 0, 1, 0, 0, 1, 0, 0, 0], 142 | [0, 0, 0, 0, 1, 1, 0, 0, 0, 0], 143 | [0, 0, 0, 1, 0, 0, 1, 0, 0, 0], 144 | [0, 0, 1, 0, 0, 0, 0, 1, 0, 0], 145 | [0, 1, 0, 0, 0, 0, 0, 0, 1, 0], 146 | [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], 147 | [0, 1, 0, 0, 0, 0, 0, 0, 1, 0], 148 | [0, 0, 1, 0, 0, 0, 0, 1, 0, 0], 149 | [0, 0, 0, 1, 0, 0, 1, 0, 0, 0], 150 | [0, 0, 0, 0, 1, 1, 0, 0, 0, 0], 151 | [0, 0, 0, 1, 0, 0, 1, 0, 0, 0], 152 | [0, 0, 1, 0, 0, 0, 0, 1, 0, 0], 153 | [0, 1, 0, 0, 0, 0, 0, 0, 1, 0], 154 | [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], 155 | [0, 1, 0, 0, 0, 0, 0, 0, 1, 0], 156 | [0, 0, 1, 0, 0, 0, 0, 1, 0, 0], 157 | [0, 0, 0, 1, 0, 0, 1, 0, 0, 0], 158 | [0, 0, 0, 0, 1, 1, 0, 0, 0, 0], 159 | [0, 0, 0, 1, 0, 0, 1, 0, 0, 0], 160 | [0, 0, 1, 0, 0, 0, 0, 1, 0, 0], 161 | [0, 1, 0, 0, 0, 0, 0, 0, 1, 0], 162 | [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], 163 | [0, 1, 0, 0, 0, 0, 0, 0, 1, 0], 164 | [0, 0, 1, 0, 0, 0, 0, 1, 0, 0], 165 | [0, 0, 0, 1, 0, 0, 1, 0, 0, 0], 166 | [0, 0, 0, 0, 1, 1, 0, 0, 0, 0] 167 | ], 168 | "interval" : 3 169 | } 170 | ] -------------------------------------------------------------------------------- /Part 10/projectiles.py: -------------------------------------------------------------------------------- 1 | class Bullet(): 2 | 3 | x = 0 4 | y = 0 5 | image = None 6 | pygame = None 7 | surface = None 8 | width = 0 9 | height = 0 10 | speed = 0.0 11 | 12 | def loadImages(self): 13 | self.image = self.pygame.image.load(self.image) 14 | 15 | def draw(self): 16 | self.surface.blit(self.image, (self.x, self.y)) 17 | 18 | def move(self): 19 | self.y += self.speed 20 | 21 | def checkForHit(self, thingToCheckAgainst): 22 | if self.x > thingToCheckAgainst.x and self.x < thingToCheckAgainst.x + thingToCheckAgainst.width: 23 | if self.y > thingToCheckAgainst.y and self.y < thingToCheckAgainst.y + thingToCheckAgainst.height: 24 | thingToCheckAgainst.registerHit() 25 | return True 26 | 27 | return False 28 | 29 | def __init__(self, x, y, pygame, surface, speed, image): 30 | self.x = x 31 | self.y = y 32 | self.pygame = pygame 33 | self.surface = surface 34 | self.image = image 35 | self.loadImages() 36 | self.speed = speed 37 | 38 | dimensions = self.image.get_rect().size 39 | self.width = dimensions[0] 40 | self.height = dimensions[1] 41 | 42 | self.x -= self.width / 2 -------------------------------------------------------------------------------- /Part 10/ships.py: -------------------------------------------------------------------------------- 1 | import projectiles, random 2 | 3 | class Player(): 4 | 5 | x = 0 6 | y = 0 7 | firing = False 8 | image = None 9 | shieldImage = None 10 | drawShield = False 11 | soundEffect = 'sounds/player_laser.wav' 12 | pygame = None 13 | surface = None 14 | width = 0 15 | height = 0 16 | bullets = [] 17 | bulletImage = "assets/you_pellet.png" 18 | bulletSpeed = -10 19 | health = 5 20 | maxHealth = health 21 | shields = 3 22 | maxShields = shields 23 | 24 | def loadImages(self): 25 | self.image = self.pygame.image.load("assets/you_ship.png") 26 | self.shieldImage = self.pygame.image.load("assets/shield2.png") 27 | 28 | def draw(self): 29 | self.surface.blit(self.image, (self.x, self.y)) 30 | if self.drawShield == True: 31 | self.surface.blit(self.shieldImage, (self.x - 3, self.y - 2)) 32 | self.drawShield = False 33 | 34 | def setPosition(self, pos): 35 | self.x = pos[0] - self.width / 2 36 | 37 | def fire(self): 38 | self.bullets.append(projectiles.Bullet(self.x + self.width / 2, self.y, self.pygame, self.surface, self.bulletSpeed, self.bulletImage)) 39 | a = self.pygame.mixer.Sound(self.soundEffect) 40 | a.set_volume(0.2) 41 | a.play() 42 | 43 | def drawBullets(self): 44 | for b in self.bullets: 45 | b.move() 46 | b.draw() 47 | 48 | def registerHit(self): 49 | if self.shields == 0: 50 | self.health -= 1 51 | else : 52 | self.shields -= 1 53 | self.drawShield = True 54 | 55 | def checkForHit(self, thingToCheckAgainst): 56 | bulletsToRemove = [] 57 | 58 | for idx, b in enumerate(self.bullets): 59 | if b.x > thingToCheckAgainst.x and b.x < thingToCheckAgainst.x + thingToCheckAgainst.width: 60 | if b.y > thingToCheckAgainst.y and b.y < thingToCheckAgainst.y + thingToCheckAgainst.height: 61 | thingToCheckAgainst.registerHit() 62 | bulletsToRemove.append(idx) 63 | bC = 0 64 | for usedBullet in bulletsToRemove: 65 | del self.bullets[usedBullet - bC] 66 | bC += 1 67 | 68 | if thingToCheckAgainst.health <= 0: 69 | return True 70 | 71 | def __init__(self, x, y, pygame, surface): 72 | self.x = x 73 | self.y = y 74 | self.pygame = pygame 75 | self.surface = surface 76 | self.loadImages() 77 | 78 | dimensions = self.image.get_rect().size 79 | self.width = dimensions[0] 80 | self.height = dimensions[1] 81 | 82 | self.x -= self.width / 2 83 | self.y -= self.height + 10 84 | 85 | class Enemy(Player): 86 | 87 | x = 0 88 | y = 0 89 | firing = False 90 | image = None 91 | soundEffect = 'sounds/enemy_laser.wav' 92 | bulletImage = "assets/them_pellet.png" 93 | bulletSpeed = 10 94 | speed = 4 95 | shields = 0 96 | 97 | def move(self): 98 | self.y += self.speed 99 | 100 | def tryToFire(self): 101 | shouldFire = random.random() 102 | 103 | if shouldFire <= 0.01: 104 | self.fire() 105 | 106 | def loadImages(self): 107 | self.image = self.pygame.image.load("assets/them_ship.png") 108 | 109 | def __init__(self, x, y, pygame, surface, health): 110 | self.x = x 111 | self.y = y 112 | self.pygame = pygame 113 | self.surface = surface 114 | self.loadImages() 115 | self.bullets = [] 116 | self.health = health 117 | 118 | dimensions = self.image.get_rect().size 119 | self.width = dimensions[0] 120 | self.height = dimensions[1] 121 | 122 | self.x += self.width / 2 -------------------------------------------------------------------------------- /Part 10/sounds/enemy_laser.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 10/sounds/enemy_laser.wav -------------------------------------------------------------------------------- /Part 10/sounds/player_laser.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 10/sounds/player_laser.wav -------------------------------------------------------------------------------- /Part 2/moving.py: -------------------------------------------------------------------------------- 1 | import pygame, sys, random 2 | import pygame.locals as GAME_GLOBALS 3 | import pygame.event as GAME_EVENTS 4 | 5 | pygame.init() 6 | clock = pygame.time.Clock() 7 | 8 | windowWidth = 640 9 | windowHeight = 480 10 | 11 | surface = pygame.display.set_mode((windowWidth, windowHeight)) 12 | 13 | pygame.display.set_caption('Pygame Shapes!') 14 | 15 | squareX = 0 16 | squareY = 0 17 | 18 | #Code Block 2 Variables 19 | greenSquareX = windowWidth / 2 20 | greenSquareY = windowHeight / 2 21 | 22 | #Code Block 3 Variables 23 | blueSquareX = 0.0 24 | blueSquareY = 0.0 25 | blueSquareVX = 1 26 | blueSquareVY = 1 27 | 28 | while True: 29 | 30 | surface.fill((0,0,0)) 31 | 32 | # Code block 1 - Drawing Shapes in Time and Space 33 | pygame.draw.rect(surface, (255,0,0), (random.randint(0, windowWidth), random.randint(0, windowHeight), 10, 10 ) ) 34 | 35 | #Code Block 2 - Moving Shapes Across Space in Time 36 | ''' 37 | pygame.draw.rect(surface, (0, 255, 0), (greenSquareX, greenSquareY, 10, 10)) 38 | 39 | greenSquareX += 1 40 | #greenSquareY += 1 41 | ''' 42 | #Code block 3 - Moving Shapes with Acceleration 43 | ''' 44 | pygame.draw.rect(surface, (0, 0, 255), (blueSquareX, blueSquareY, 10, 10)) 45 | 46 | blueSquareX += blueSquareVX 47 | blueSquareY += blueSquareVY 48 | 49 | blueSquareVX -= 0.2 50 | blueSquareVY += 0.1 51 | ''' 52 | 53 | for event in GAME_EVENTS.get(): 54 | 55 | if event.type == GAME_GLOBALS.QUIT: 56 | 57 | pygame.quit() 58 | 59 | sys.exit() 60 | 61 | clock.tick(60) 62 | pygame.display.update() 63 | -------------------------------------------------------------------------------- /Part 2/properties.py: -------------------------------------------------------------------------------- 1 | import pygame, sys, random 2 | import pygame.locals as GAME_GLOBALS 3 | import pygame.event as GAME_EVENTS 4 | 5 | pygame.init() 6 | clock = pygame.time.Clock() 7 | 8 | windowWidth = 640 9 | windowHeight = 480 10 | 11 | surface = pygame.display.set_mode((windowWidth, windowHeight)) 12 | 13 | pygame.display.set_caption('Pygame Shapes!') 14 | 15 | # Code block 1 variables 16 | 17 | rectX = windowWidth / 2 18 | rectY = windowHeight / 2 19 | rectWidth = 50 20 | rectHeight = 50 21 | 22 | # Code block 2 variables 23 | 24 | squaresRed = random.randint(0, 255) 25 | squaresBlue = random.randint(0, 255) 26 | squaresGreen = random.randint(0, 255) 27 | 28 | while True: 29 | 30 | surface.fill((0,0,0)) 31 | 32 | # Code block 1 33 | 34 | pygame.draw.rect(surface, (255,255,0), (rectX - rectWidth / 2, rectY - rectHeight / 2, rectWidth, rectHeight)) 35 | 36 | rectWidth -= 1 37 | rectHeight -= 1 38 | 39 | #Code block 2 40 | ''' 41 | pygame.draw.rect(surface, (squaresRed, squaresGreen, squaresBlue), (50, 50, windowWidth / 2, windowHeight / 2)) 42 | 43 | if squaresRed >= 255: 44 | squaresRed = random.randint(0, 255) 45 | else: 46 | squaresRed += 1 47 | 48 | if squaresGreen >= 255: 49 | squaresGreen = random.randint(0, 255) 50 | else: 51 | squaresGreen += 1 52 | 53 | if squaresBlue >= 255: 54 | squaresBlue = random.randint(0, 255) 55 | else: 56 | squaresBlue += 1 57 | ''' 58 | 59 | for event in GAME_EVENTS.get(): 60 | 61 | if event.type == GAME_GLOBALS.QUIT: 62 | 63 | pygame.quit() 64 | 65 | sys.exit() 66 | 67 | clock.tick(60) 68 | pygame.display.update() 69 | -------------------------------------------------------------------------------- /Part 3/keyboard.py: -------------------------------------------------------------------------------- 1 | import pygame, sys 2 | import pygame.locals as GAME_GLOBALS 3 | import pygame.event as GAME_EVENTS 4 | 5 | # Pygame Variables 6 | pygame.init() 7 | clock = pygame.time.Clock() 8 | 9 | windowWidth = 800 10 | windowHeight = 800 11 | 12 | surface = pygame.display.set_mode((windowWidth, windowHeight)) 13 | pygame.display.set_caption('Pygame Keyboard!') 14 | 15 | # Square Variables 16 | playerSize = 20 17 | playerX = (windowWidth / 2) - (playerSize / 2) 18 | playerY = windowHeight - playerSize 19 | playerVX = 1.0 20 | playerVY = 0.0 21 | jumpHeight = 25.0 22 | moveSpeed = 1.0 23 | maxSpeed = 10.0 24 | gravity = 1.0 25 | 26 | # Keyboard Variables 27 | leftDown = False 28 | rightDown = False 29 | haveJumped = False 30 | 31 | def move(): 32 | 33 | global playerX, playerY, playerVX, playerVY, haveJumped, gravity 34 | 35 | # Move left 36 | if leftDown: 37 | #If we're already moving to the right, reset the moving speed and invert the direction 38 | if playerVX > 0.0: 39 | playerVX = moveSpeed 40 | playerVX = -playerVX 41 | # Make sure our square doesn't leave our window to the left 42 | if playerX > 0: 43 | playerX += playerVX 44 | 45 | # Move right 46 | if rightDown: 47 | # If we're already moving to the left reset the moving speed again 48 | if playerVX < 0.0: 49 | playerVX = moveSpeed 50 | # Make sure our square doesn't leave our window to the right 51 | if playerX + playerSize < windowWidth: 52 | playerX += playerVX 53 | 54 | if playerVY > 1.0: 55 | playerVY = playerVY * 0.9 56 | else : 57 | playerVY = 0.0 58 | haveJumped = False 59 | 60 | # Is our square in the air? Better add some gravity to bring it back down! 61 | if playerY < windowHeight - playerSize: 62 | playerY += gravity 63 | gravity = gravity * 1.1 64 | else : 65 | playerY = windowHeight - playerSize 66 | gravity = 1.0 67 | 68 | playerY -= playerVY 69 | 70 | if playerVX > 0.0 and playerVX < maxSpeed or playerVX < 0.0 and playerVX > -maxSpeed: 71 | if haveJumped == False: 72 | playerVX = playerVX * 1.1 73 | 74 | # How to quit our program 75 | def quitGame(): 76 | pygame.quit() 77 | sys.exit() 78 | 79 | while True: 80 | 81 | surface.fill((0,0,0)) 82 | 83 | pygame.draw.rect(surface, (255,0,0), (playerX, playerY, playerSize, playerSize)) 84 | 85 | # Get a list of all events that happened since the last redraw 86 | for event in GAME_EVENTS.get(): 87 | 88 | if event.type == pygame.KEYDOWN: 89 | 90 | if event.key == pygame.K_LEFT: 91 | leftDown = True 92 | if event.key == pygame.K_RIGHT: 93 | rightDown = True 94 | if event.key == pygame.K_UP: 95 | if not haveJumped: 96 | haveJumped = True 97 | playerVY += jumpHeight 98 | if event.key == pygame.K_ESCAPE: 99 | quitGame() 100 | 101 | if event.type == pygame.KEYUP: 102 | if event.key == pygame.K_LEFT: 103 | leftDown = False 104 | playerVX = moveSpeed 105 | if event.key == pygame.K_RIGHT: 106 | rightDown = False 107 | playerVX = moveSpeed 108 | 109 | if event.type == GAME_GLOBALS.QUIT: 110 | quitGame() 111 | 112 | move() 113 | 114 | clock.tick(60) 115 | pygame.display.update() 116 | -------------------------------------------------------------------------------- /Part 3/mouse.py: -------------------------------------------------------------------------------- 1 | import pygame, sys 2 | import pygame.locals as GAME_GLOBALS 3 | import pygame.event as GAME_EVENTS 4 | 5 | # Pygame Variables 6 | pygame.init() 7 | clock = pygame.time.Clock() 8 | 9 | windowWidth = 800 10 | windowHeight = 800 11 | 12 | surface = pygame.display.set_mode((windowWidth, windowHeight)) 13 | 14 | pygame.display.set_caption('Pygame Mouse!') 15 | 16 | # Mouse Variables 17 | mousePosition = None 18 | mousePressed = False 19 | 20 | # Square Variables 21 | squareSize = 40 22 | squareColor = (255, 0, 0) 23 | squareX = windowWidth / 2 24 | squareY = windowHeight - squareSize 25 | draggingSquare = False 26 | gravity = 5.0 27 | 28 | def checkBounds(): 29 | 30 | global squareColor, squareX, squareY, draggingSquare 31 | 32 | if mousePressed == True: 33 | # Is our cursor over our square? 34 | if mousePosition[0] > squareX and mousePosition[0] < squareX + squareSize: 35 | 36 | if mousePosition[1] > squareY and mousePosition[1] < squareY + squareSize: 37 | 38 | draggingSquare = True 39 | pygame.mouse.set_visible(0) 40 | 41 | else : 42 | squareColor = (255,0,0) 43 | pygame.mouse.set_visible(1) 44 | draggingSquare = False 45 | 46 | def checkGravity(): 47 | 48 | global gravity, squareY, squareSize, windowHeight 49 | 50 | # Is our square in the air and have we let go of it? 51 | if squareY < windowHeight - squareSize and mousePressed == False: 52 | squareY += gravity 53 | gravity = gravity * 1.1 54 | else : 55 | squareY = windowHeight - squareSize 56 | gravity = 5.0 57 | 58 | def drawSquare(): 59 | 60 | global squareColor, squareX, squareY, draggingSquare 61 | 62 | if draggingSquare == True: 63 | 64 | squareColor = (0, 255, 0) 65 | squareX = mousePosition[0] - squareSize / 2 66 | squareY = mousePosition[1] - squareSize / 2 67 | 68 | pygame.draw.rect(surface, squareColor, (squareX, squareY, squareSize, squareSize)) 69 | 70 | # How to quit our program 71 | def quitGame(): 72 | pygame.quit() 73 | sys.exit() 74 | 75 | while True: 76 | 77 | mousePosition = pygame.mouse.get_pos() 78 | 79 | surface.fill((0,0,0)) 80 | 81 | # Check whether mouse is pressed down 82 | if pygame.mouse.get_pressed()[0] == True: 83 | mousePressed = True 84 | else : 85 | mousePressed = False 86 | 87 | checkBounds() 88 | checkGravity() 89 | drawSquare() 90 | 91 | clock.tick(60) 92 | pygame.display.update() 93 | 94 | for event in GAME_EVENTS.get(): 95 | 96 | if event.type == pygame.KEYDOWN: 97 | if event.key == pygame.K_ESCAPE: 98 | quitGame() 99 | 100 | if event.type == GAME_GLOBALS.QUIT: 101 | quitGame() 102 | -------------------------------------------------------------------------------- /Part 4/Code/assets/game_over.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 4/Code/assets/game_over.jpg -------------------------------------------------------------------------------- /Part 4/Code/assets/title.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 4/Code/assets/title.jpg -------------------------------------------------------------------------------- /Part 4/Code/drop.py: -------------------------------------------------------------------------------- 1 | import pygame, sys, random 2 | import pygame.locals as GAME_GLOBALS 3 | import pygame.event as GAME_EVENTS 4 | import pygame.time as GAME_TIME 5 | 6 | pygame.init() 7 | clock = pygame.time.Clock() 8 | 9 | title_image = pygame.image.load("assets/title.jpg") 10 | game_over_image = pygame.image.load("assets/game_over.jpg") 11 | 12 | windowWidth = 400 13 | windowHeight = 600 14 | 15 | surface = pygame.display.set_mode((windowWidth, windowHeight)) 16 | pygame.display.set_caption('Drop!') 17 | 18 | leftDown = False 19 | rightDown = False 20 | 21 | gameStarted = False 22 | gameEnded = False 23 | gamePlatforms = [] 24 | platformSpeed = 3 25 | platformDelay = 2000 26 | lastPlatform = 0 27 | platformsDroppedThrough = -1 28 | dropping = False 29 | 30 | gameBeganAt = 0 31 | timer = 0 32 | 33 | player = { 34 | "x" : windowWidth / 2, 35 | "y" : 0, 36 | "height" : 25, 37 | "width" : 10, 38 | "vy" : 5 39 | } 40 | 41 | def drawPlayer(): 42 | 43 | pygame.draw.rect(surface, (255,0,0), (player["x"], player["y"], player["width"], player["height"])) 44 | 45 | def movePlayer(): 46 | 47 | global platformsDroppedThrough, dropping 48 | 49 | leftOfPlayerOnPlatform = True 50 | rightOfPlayerOnPlatform = True 51 | 52 | if surface.get_at(( int(player["x"]), int(player["y"]) + player["height"])) == (0,0,0,255): 53 | leftOfPlayerOnPlatform = False 54 | 55 | if surface.get_at(( int(player["x"]) + player["width"], int(player["y"]) + player["height"])) == (0,0,0,255): 56 | rightOfPlayerOnPlatform = False 57 | 58 | if leftOfPlayerOnPlatform is False and rightOfPlayerOnPlatform is False and (player["y"] + player["height"]) + player["vy"] < windowHeight: 59 | player["y"] += player["vy"] 60 | 61 | if dropping is False: 62 | dropping = True 63 | platformsDroppedThrough += 1 64 | 65 | else : 66 | 67 | foundPlatformTop = False 68 | yOffset = 0 69 | dropping = False 70 | 71 | while foundPlatformTop is False: 72 | 73 | if surface.get_at(( int(player["x"]), ( int(player["y"]) + player["height"]) - yOffset )) == (0,0,0,255): 74 | player["y"] -= yOffset 75 | foundPlatformTop = True 76 | elif (player["y"] + player["height"]) - yOffset > 0: 77 | yOffset += 1 78 | else : 79 | 80 | gameOver() 81 | break 82 | 83 | if leftDown is True: 84 | if player["x"] > 0 and player["x"] - 5 > 0: 85 | player["x"] -= 5 86 | elif player["x"] > 0 and player["x"] - 5 < 0: 87 | player["x"] = 0 88 | 89 | if rightDown is True: 90 | if player["x"] + player["width"] < windowWidth and (player["x"] + player["width"]) + 5 < windowWidth: 91 | player["x"] += 5 92 | elif player["x"] + player["width"] < windowWidth and (player["x"] + player["width"]) + 5 > windowWidth: 93 | player["x"] = windowWidth - player["width"] 94 | 95 | def createPlatform(): 96 | 97 | global lastPlatform, platformDelay 98 | 99 | platformY = windowHeight 100 | gapPosition = random.randint(0, windowWidth - 40) 101 | 102 | gamePlatforms.append({"pos" : [0, platformY], "gap" : gapPosition}) 103 | lastPlatform = GAME_TIME.get_ticks() 104 | 105 | if platformDelay > 800: 106 | platformDelay -= 50 107 | 108 | def movePlatforms(): 109 | # print("Platforms") 110 | 111 | for idx, platform in enumerate(gamePlatforms): 112 | 113 | platform["pos"][1] -= platformSpeed 114 | 115 | if platform["pos"][1] < -10: 116 | gamePlatforms.pop(idx) 117 | 118 | 119 | def drawPlatforms(): 120 | 121 | for platform in gamePlatforms: 122 | 123 | pygame.draw.rect(surface, (255,255,255), (platform["pos"][0], platform["pos"][1], windowWidth, 10)) 124 | pygame.draw.rect(surface, (0,0,0), (platform["gap"], platform["pos"][1], 40, 10) ) 125 | 126 | 127 | def gameOver(): 128 | global gameStarted, gameEnded 129 | 130 | platformSpeed = 0 131 | gameStarted = False 132 | gameEnded = True 133 | 134 | def restartGame(): 135 | 136 | global gamePlatforms, player, gameBeganAt, platformsDroppedThrough, platformDelay 137 | 138 | gamePlatforms = [] 139 | player["x"] = windowWidth / 2 140 | player["y"] = 0 141 | gameBeganAt = GAME_TIME.get_ticks() 142 | platformsDroppedThrough = -1 143 | platformDelay = 2000 144 | 145 | def quitGame(): 146 | pygame.quit() 147 | sys.exit() 148 | 149 | # 'main' loop 150 | while True: 151 | 152 | surface.fill((0,0,0)) 153 | 154 | for event in GAME_EVENTS.get(): 155 | 156 | if event.type == pygame.KEYDOWN: 157 | 158 | if event.key == pygame.K_LEFT: 159 | leftDown = True 160 | if event.key == pygame.K_RIGHT: 161 | rightDown = True 162 | if event.key == pygame.K_ESCAPE: 163 | quitGame() 164 | 165 | if event.type == pygame.KEYUP: 166 | if event.key == pygame.K_LEFT: 167 | leftDown = False 168 | if event.key == pygame.K_RIGHT: 169 | rightDown = False 170 | 171 | if event.key == pygame.K_SPACE: 172 | if gameStarted == False: 173 | restartGame() 174 | gameStarted = True 175 | 176 | if event.type == GAME_GLOBALS.QUIT: 177 | quitGame() 178 | 179 | if gameStarted is True: 180 | # Play game 181 | timer = GAME_TIME.get_ticks() - gameBeganAt 182 | 183 | movePlatforms() 184 | drawPlatforms() 185 | movePlayer() 186 | drawPlayer() 187 | 188 | elif gameEnded is True: 189 | # Draw game over screen 190 | surface.blit(game_over_image, (0, 150)) 191 | 192 | else : 193 | # Welcome Screen 194 | surface.blit(title_image, (0, 150)) 195 | 196 | if GAME_TIME.get_ticks() - lastPlatform > platformDelay: 197 | createPlatform() 198 | 199 | clock.tick(60) 200 | pygame.display.update() 201 | -------------------------------------------------------------------------------- /Part 4/Images/Diagrams/DictionariesAndLists.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 4/Images/Diagrams/DictionariesAndLists.png -------------------------------------------------------------------------------- /Part 4/Images/Diagrams/Drop.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 4/Images/Diagrams/Drop.png -------------------------------------------------------------------------------- /Part 4/Images/Diagrams/append_platform.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 4/Images/Diagrams/append_platform.png -------------------------------------------------------------------------------- /Part 4/Images/Screenshots/Title-Screen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 4/Images/Screenshots/Title-Screen.png -------------------------------------------------------------------------------- /Part 4/Images/Screenshots/difficulty.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 4/Images/Screenshots/difficulty.png -------------------------------------------------------------------------------- /Part 4/Images/Screenshots/game_over.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 4/Images/Screenshots/game_over.png -------------------------------------------------------------------------------- /Part 5/Code/assets/images/Large/cat.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 5/Code/assets/images/Large/cat.png -------------------------------------------------------------------------------- /Part 5/Code/assets/images/Large/chicken.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 5/Code/assets/images/Large/chicken.png -------------------------------------------------------------------------------- /Part 5/Code/assets/images/Large/cow.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 5/Code/assets/images/Large/cow.png -------------------------------------------------------------------------------- /Part 5/Code/assets/images/Large/dog.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 5/Code/assets/images/Large/dog.png -------------------------------------------------------------------------------- /Part 5/Code/assets/images/Large/horse.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 5/Code/assets/images/Large/horse.png -------------------------------------------------------------------------------- /Part 5/Code/assets/images/Large/mouse.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 5/Code/assets/images/Large/mouse.png -------------------------------------------------------------------------------- /Part 5/Code/assets/images/Large/pig.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 5/Code/assets/images/Large/pig.png -------------------------------------------------------------------------------- /Part 5/Code/assets/images/Large/rooster.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 5/Code/assets/images/Large/rooster.png -------------------------------------------------------------------------------- /Part 5/Code/assets/images/Large/sheep.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 5/Code/assets/images/Large/sheep.png -------------------------------------------------------------------------------- /Part 5/Code/assets/images/cat.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 5/Code/assets/images/cat.png -------------------------------------------------------------------------------- /Part 5/Code/assets/images/chicken.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 5/Code/assets/images/chicken.png -------------------------------------------------------------------------------- /Part 5/Code/assets/images/cow.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 5/Code/assets/images/cow.png -------------------------------------------------------------------------------- /Part 5/Code/assets/images/dog.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 5/Code/assets/images/dog.png -------------------------------------------------------------------------------- /Part 5/Code/assets/images/horse.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 5/Code/assets/images/horse.png -------------------------------------------------------------------------------- /Part 5/Code/assets/images/mouse.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 5/Code/assets/images/mouse.png -------------------------------------------------------------------------------- /Part 5/Code/assets/images/pig.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 5/Code/assets/images/pig.png -------------------------------------------------------------------------------- /Part 5/Code/assets/images/rooster.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 5/Code/assets/images/rooster.png -------------------------------------------------------------------------------- /Part 5/Code/assets/images/sheep.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 5/Code/assets/images/sheep.png -------------------------------------------------------------------------------- /Part 5/Code/assets/images/stop.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 5/Code/assets/images/stop.png -------------------------------------------------------------------------------- /Part 5/Code/assets/sounds/MP3/cat.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 5/Code/assets/sounds/MP3/cat.mp3 -------------------------------------------------------------------------------- /Part 5/Code/assets/sounds/MP3/chicken.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 5/Code/assets/sounds/MP3/chicken.mp3 -------------------------------------------------------------------------------- /Part 5/Code/assets/sounds/MP3/cow.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 5/Code/assets/sounds/MP3/cow.mp3 -------------------------------------------------------------------------------- /Part 5/Code/assets/sounds/MP3/dog.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 5/Code/assets/sounds/MP3/dog.mp3 -------------------------------------------------------------------------------- /Part 5/Code/assets/sounds/MP3/farm.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 5/Code/assets/sounds/MP3/farm.mp3 -------------------------------------------------------------------------------- /Part 5/Code/assets/sounds/MP3/horse.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 5/Code/assets/sounds/MP3/horse.mp3 -------------------------------------------------------------------------------- /Part 5/Code/assets/sounds/MP3/mouse.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 5/Code/assets/sounds/MP3/mouse.mp3 -------------------------------------------------------------------------------- /Part 5/Code/assets/sounds/MP3/pig.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 5/Code/assets/sounds/MP3/pig.mp3 -------------------------------------------------------------------------------- /Part 5/Code/assets/sounds/MP3/rooster.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 5/Code/assets/sounds/MP3/rooster.mp3 -------------------------------------------------------------------------------- /Part 5/Code/assets/sounds/MP3/sheep.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 5/Code/assets/sounds/MP3/sheep.mp3 -------------------------------------------------------------------------------- /Part 5/Code/assets/sounds/OGG/cat.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 5/Code/assets/sounds/OGG/cat.ogg -------------------------------------------------------------------------------- /Part 5/Code/assets/sounds/OGG/chicken.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 5/Code/assets/sounds/OGG/chicken.ogg -------------------------------------------------------------------------------- /Part 5/Code/assets/sounds/OGG/cow.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 5/Code/assets/sounds/OGG/cow.ogg -------------------------------------------------------------------------------- /Part 5/Code/assets/sounds/OGG/dog.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 5/Code/assets/sounds/OGG/dog.ogg -------------------------------------------------------------------------------- /Part 5/Code/assets/sounds/OGG/farm.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 5/Code/assets/sounds/OGG/farm.ogg -------------------------------------------------------------------------------- /Part 5/Code/assets/sounds/OGG/horse.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 5/Code/assets/sounds/OGG/horse.ogg -------------------------------------------------------------------------------- /Part 5/Code/assets/sounds/OGG/mouse.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 5/Code/assets/sounds/OGG/mouse.ogg -------------------------------------------------------------------------------- /Part 5/Code/assets/sounds/OGG/pig.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 5/Code/assets/sounds/OGG/pig.ogg -------------------------------------------------------------------------------- /Part 5/Code/assets/sounds/OGG/rooster.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 5/Code/assets/sounds/OGG/rooster.ogg -------------------------------------------------------------------------------- /Part 5/Code/assets/sounds/OGG/sheep.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 5/Code/assets/sounds/OGG/sheep.ogg -------------------------------------------------------------------------------- /Part 5/Code/sounds.py: -------------------------------------------------------------------------------- 1 | import pygame, sys, random 2 | import pygame.locals as GAME_GLOBALS 3 | import pygame.event as GAME_EVENTS 4 | import pygame.time as GAME_TIME 5 | 6 | windowWidth = 600 7 | windowHeight = 650 8 | 9 | pygame.init() 10 | surface = pygame.display.set_mode((windowWidth, windowHeight)) 11 | pygame.display.set_caption('Soundboard') 12 | 13 | buttons = [] 14 | stopButton = { "image" : pygame.image.load("assets/images/stop.png"), "position" : (275, 585)} 15 | 16 | mousePosition = None 17 | volume = 1.0 18 | 19 | pygame.mixer.init() 20 | pygame.mixer.music.load('assets/sounds/OGG/farm.ogg') 21 | pygame.mixer.music.play(-1) 22 | 23 | def drawButtons(): 24 | 25 | for button in buttons: 26 | surface.blit(button["image"], button["position"]) 27 | 28 | surface.blit(stopButton["image"], stopButton['position']) 29 | 30 | def drawVolume(): 31 | 32 | pygame.draw.rect(surface, (229, 229, 229), (450, 610, 100, 5)) 33 | 34 | volumePosition = (100 / 100) * (volume * 100) 35 | 36 | pygame.draw.rect(surface, (204, 204, 204), (450 + volumePosition, 600, 10, 25)) 37 | 38 | def handleClick(): 39 | 40 | global mousePosition, volume 41 | 42 | for button in buttons: 43 | 44 | buttonSize = button['image'].get_rect().size 45 | buttonPosition = button['position'] 46 | 47 | if mousePosition[0] > buttonPosition[0] and mousePosition[0] < buttonPosition[0] + buttonSize[0]: 48 | 49 | if mousePosition[1] > buttonPosition[1] and mousePosition[1] < buttonPosition[1] + buttonSize[1]: 50 | button['sound'].set_volume(volume) 51 | button['sound'].play() 52 | 53 | if mousePosition[0] > stopButton['position'][0] and mousePosition[0] < stopButton['position'][0] + stopButton['image'].get_rect().size[0]: 54 | if mousePosition[1] > stopButton['position'][1] and mousePosition[1] < stopButton['position'][1] + stopButton['image'].get_rect().size[1]: 55 | pygame.mixer.stop() 56 | 57 | def checkVolume(): 58 | 59 | global mousePosition, volume 60 | 61 | if pygame.mouse.get_pressed()[0] == True: 62 | 63 | if mousePosition[1] > 600 and mousePosition[1] < 625: 64 | if mousePosition[0] > 450 and mousePosition[0] < 550: 65 | volume = float((mousePosition[0] - 450)) / 100 66 | 67 | def quitGame(): 68 | pygame.quit() 69 | sys.exit() 70 | 71 | # Create Buttons 72 | buttons.append({ "image" : pygame.image.load("assets/images/sheep.png"), "position" : (25, 25), "sound" : pygame.mixer.Sound('assets/sounds/OGG/sheep.ogg')}) 73 | buttons.append({ "image" : pygame.image.load("assets/images/rooster.png"), "position" : (225, 25), "sound" : pygame.mixer.Sound('assets/sounds/OGG/rooster.ogg')}) 74 | buttons.append({ "image" : pygame.image.load("assets/images/pig.png"), "position" : (425, 25), "sound" : pygame.mixer.Sound('assets/sounds/OGG/pig.ogg')}) 75 | buttons.append({ "image" : pygame.image.load("assets/images/mouse.png"), "position" : (25, 225), "sound" : pygame.mixer.Sound('assets/sounds/OGG/mouse.ogg')}) 76 | buttons.append({ "image" : pygame.image.load("assets/images/horse.png"), "position" : (225, 225), "sound" : pygame.mixer.Sound('assets/sounds/OGG/horse.ogg')}) 77 | buttons.append({ "image" : pygame.image.load("assets/images/dog.png"), "position" : (425, 225), "sound" : pygame.mixer.Sound('assets/sounds/OGG/dog.ogg')}) 78 | buttons.append({ "image" : pygame.image.load("assets/images/cow.png"), "position" : (25, 425), "sound" : pygame.mixer.Sound('assets/sounds/OGG/cow.ogg')}) 79 | buttons.append({ "image" : pygame.image.load("assets/images/chicken.png"), "position" : (225, 425), "sound" : pygame.mixer.Sound('assets/sounds/OGG/chicken.ogg')}) 80 | buttons.append({ "image" : pygame.image.load("assets/images/cat.png"), "position" : (425, 425), "sound" : pygame.mixer.Sound('assets/sounds/OGG/cat.ogg')}) 81 | 82 | # 'main' loop 83 | while True: 84 | 85 | surface.fill((255,255,255)) 86 | 87 | mousePosition = pygame.mouse.get_pos() 88 | 89 | for event in GAME_EVENTS.get(): 90 | 91 | if event.type == pygame.KEYDOWN: 92 | 93 | if event.key == pygame.K_ESCAPE: 94 | quitGame() 95 | 96 | if event.type == GAME_GLOBALS.QUIT: 97 | quitGame() 98 | 99 | if event.type == pygame.MOUSEBUTTONUP: 100 | handleClick() 101 | 102 | drawButtons() 103 | checkVolume() 104 | drawVolume() 105 | 106 | pygame.display.update() 107 | -------------------------------------------------------------------------------- /Part 6/Code/assets/back.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 6/Code/assets/back.png -------------------------------------------------------------------------------- /Part 6/Code/assets/background.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 6/Code/assets/background.jpg -------------------------------------------------------------------------------- /Part 6/Code/assets/earth.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 6/Code/assets/earth.png -------------------------------------------------------------------------------- /Part 6/Code/assets/jupiter.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 6/Code/assets/jupiter.png -------------------------------------------------------------------------------- /Part 6/Code/assets/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 6/Code/assets/logo.png -------------------------------------------------------------------------------- /Part 6/Code/assets/mars.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 6/Code/assets/mars.png -------------------------------------------------------------------------------- /Part 6/Code/assets/mercury.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 6/Code/assets/mercury.png -------------------------------------------------------------------------------- /Part 6/Code/assets/neptune.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 6/Code/assets/neptune.png -------------------------------------------------------------------------------- /Part 6/Code/assets/saturn.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 6/Code/assets/saturn.png -------------------------------------------------------------------------------- /Part 6/Code/assets/tabs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 6/Code/assets/tabs.png -------------------------------------------------------------------------------- /Part 6/Code/assets/uranus.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 6/Code/assets/uranus.png -------------------------------------------------------------------------------- /Part 6/Code/assets/venus.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 6/Code/assets/venus.png -------------------------------------------------------------------------------- /Part 6/Code/simulator.py: -------------------------------------------------------------------------------- 1 | import pygame, sys, random, math 2 | import pygame.locals as GAME_GLOBALS 3 | import pygame.event as GAME_EVENTS 4 | import pygame.time as GAME_TIME 5 | import solarsystem 6 | 7 | windowWidth = 1024 8 | windowHeight = 768 9 | 10 | pygame.init() 11 | clock = pygame.time.Clock() 12 | surface = pygame.display.set_mode((windowWidth, windowHeight), pygame.FULLSCREEN) 13 | 14 | pygame.display.set_caption('Solar System Simulator') 15 | 16 | previousMousePosition = [0,0] 17 | mousePosition = None 18 | mouseDown = False 19 | 20 | background = pygame.image.load("assets/background.jpg") 21 | logo = pygame.image.load("assets/logo.png") 22 | UITab = pygame.image.load("assets/tabs.png") 23 | UICoordinates = [{"name" : "mercury", "coordinates" : (132,687)}, {"name" : "venus", "coordinates" : (229,687)}, {"name" : "earth", "coordinates" : (326,687)}, {"name" : "mars", "coordinates" : (423,687)}, {"name" : "jupiter", "coordinates" : (520,687)}, {"name" : "saturn", "coordinates" : (617,687)}, {"name" : "neptune", "coordinates" : (713,687)}, {"name" : "uranus", "coordinates" : (810,687)}] 24 | 25 | celestialBodies = [] 26 | currentBody = None 27 | 28 | drawAttractions = True 29 | 30 | gravity = 10.0 31 | 32 | def drawUI(): 33 | surface.blit(UITab, (131,687)) 34 | surface.blit(solarsystem.images["mercury"], (158,714)) 35 | surface.blit(solarsystem.images["venus"], (247,706)) 36 | surface.blit(solarsystem.images["earth"], (344,704)) 37 | surface.blit(solarsystem.images["mars"], (451,714)) 38 | surface.blit(solarsystem.images["jupiter"], (524,692)) 39 | surface.blit(solarsystem.images["saturn"], (620,695)) 40 | surface.blit(solarsystem.images["neptune"], (724,697)) 41 | surface.blit(solarsystem.images["uranus"], (822,697)) 42 | 43 | def drawPlanets(): 44 | 45 | for planet in celestialBodies: 46 | planet["position"][0] += planet["velocity"][0] 47 | planet["position"][1] += planet["velocity"][1] 48 | surface.blit(solarsystem.images[planet["name"]], (planet["position"][0] - planet["radius"], planet["position"][1] - planet["radius"])) 49 | 50 | def drawCurrentBody(): 51 | 52 | currentBody["position"][0] = mousePosition[0] 53 | currentBody["position"][1] = mousePosition[1] 54 | 55 | surface.blit(solarsystem.images[currentBody["name"]], (currentBody["position"][0] - currentBody["radius"], currentBody["position"][1] - currentBody["radius"])) 56 | 57 | def calculateMovement(): 58 | 59 | for planet in celestialBodies: 60 | 61 | for otherPlanet in celestialBodies: 62 | 63 | if otherPlanet is not planet: 64 | 65 | direction = (otherPlanet["position"][0] - planet["position"][0], otherPlanet["position"][1] - planet["position"][1]) # The difference in the X, Y coordinates of the objects 66 | magnitude = math.hypot(otherPlanet["position"][0] - planet["position"][0], otherPlanet["position"][1] - planet["position"][1]) # The distance between the two objects 67 | nDirection = (direction[0] / magnitude, direction[1] / magnitude) # Normalised Vector pointing in the direction of the force 68 | 69 | ## We need to limit the gravity to stop things flying off to infinity... and beyond! 70 | if magnitude < 5: 71 | magnitude = 5 72 | elif magnitude > 30: 73 | magnitude = 30 74 | 75 | strength = ((gravity * planet["mass"] * otherPlanet["mass"]) / (magnitude * magnitude)) / otherPlanet["mass"] # How strong should the attraction be? 76 | 77 | appliedForce = (nDirection[0] * strength, nDirection[1] * strength) 78 | 79 | otherPlanet["velocity"][0] -= appliedForce[0] 80 | otherPlanet["velocity"][1] -= appliedForce[1] 81 | 82 | if drawAttractions is True: 83 | pygame.draw.line(surface, (255,255,255), (planet["position"][0],planet["position"][1]), (otherPlanet["position"][0],otherPlanet["position"][1]), 1) 84 | 85 | def checkUIForClick(coordinates): 86 | 87 | for tab in UICoordinates: 88 | tabX = tab["coordinates"][0] 89 | 90 | if coordinates[0] > tabX and coordinates[0] < tabX + 82: 91 | return tab["name"] 92 | 93 | return False 94 | 95 | def handleMouseDown(): 96 | global mousePosition, currentBody 97 | 98 | if(mousePosition[1] >= 687): 99 | newPlanet = checkUIForClick(mousePosition) 100 | 101 | if newPlanet is not False: 102 | currentBody = solarsystem.makeNewPlanet(newPlanet) 103 | 104 | 105 | def quitGame(): 106 | pygame.quit() 107 | sys.exit() 108 | 109 | # 'main' loop 110 | while True: 111 | 112 | mousePosition = pygame.mouse.get_pos() 113 | surface.blit(background, (0,0)) 114 | 115 | # Handle user and system events 116 | for event in GAME_EVENTS.get(): 117 | 118 | if event.type == pygame.KEYDOWN: 119 | 120 | if event.key == pygame.K_ESCAPE: 121 | quitGame() 122 | 123 | if event.type == pygame.KEYUP: 124 | 125 | if event.key == pygame.K_r: 126 | celestialBodies = [] 127 | if event.key == pygame.K_a: 128 | if drawAttractions is True: 129 | drawAttractions = False 130 | elif drawAttractions is False: 131 | drawAttractions = True 132 | 133 | if event.type == pygame.MOUSEBUTTONDOWN: 134 | mouseDown = True 135 | handleMouseDown() 136 | 137 | if event.type == pygame.MOUSEBUTTONUP: 138 | mouseDown = False 139 | 140 | if event.type == GAME_GLOBALS.QUIT: 141 | quitGame() 142 | 143 | # Draw the UI; Update the movement of the planets; Draw the planets in their new positions. 144 | drawUI() 145 | calculateMovement() 146 | drawPlanets() 147 | 148 | # If our user has created a new planet, draw it where the mouse is 149 | if currentBody is not None: 150 | drawCurrentBody() 151 | 152 | # If our user has released the mouse, add the new planet to the celestialBodies list and let gravity do its thing 153 | if mouseDown is False: 154 | currentBody["velocity"][0] = (mousePosition[0] - previousMousePosition[0]) / 4 155 | currentBody["velocity"][1] = (mousePosition[1] - previousMousePosition[1]) / 4 156 | celestialBodies.append(currentBody) 157 | currentBody = None 158 | 159 | # Draw the logo for the first four seconds of the program 160 | if GAME_TIME.get_ticks() < 4000: 161 | surface.blit(logo, (108,77)) 162 | 163 | # Store the previous mouse coordinates to create a vector when we release a new planet 164 | previousMousePosition = mousePosition 165 | 166 | clock.tick(60) 167 | pygame.display.update() -------------------------------------------------------------------------------- /Part 6/Code/solarsystem.py: -------------------------------------------------------------------------------- 1 | import pygame, copy 2 | 3 | images = { 4 | "mercury" : pygame.image.load("assets/mercury.png"), 5 | "venus" : pygame.image.load("assets/venus.png"), 6 | "earth" : pygame.image.load("assets/earth.png"), 7 | "mars" : pygame.image.load("assets/mars.png"), 8 | "jupiter" : pygame.image.load("assets/jupiter.png"), 9 | "saturn" : pygame.image.load("assets/saturn.png"), 10 | "neptune" : pygame.image.load("assets/neptune.png"), 11 | "uranus" : pygame.image.load("assets/uranus.png"), 12 | } 13 | 14 | planets = [{ 15 | "name" : "mercury", 16 | "radius" : 15.0, 17 | "mass" : 0.6, 18 | "velocity" : [0,0], 19 | "position" : [0,0] 20 | }, 21 | { 22 | "name" : "venus", 23 | "radius" : 23.0, 24 | "mass" : 0.95, 25 | "velocity" : [0,0], 26 | "position" : [0,0] 27 | }, 28 | { 29 | "name" : "earth", 30 | "radius" : 24.0, 31 | "mass" : 1.0, 32 | "velocity" : [0,0], 33 | "position" : [0,0] 34 | }, 35 | { 36 | "name" : "mars", 37 | "radius" : 15.0, 38 | "mass" : 0.4, 39 | "velocity" : [0,0], 40 | "position" : [0,0] 41 | }, 42 | { 43 | "name" : "jupiter", 44 | "radius" : 37.0, 45 | "mass" : 15.0, 46 | "velocity" : [0,0], 47 | "position" : [0,0] 48 | }, 49 | { 50 | "name" : "saturn", 51 | "radius" : 30.0, 52 | "mass" : 4, 53 | "velocity" : [0,0], 54 | "position" : [0,0] 55 | }, 56 | { 57 | "name" : "neptune", 58 | "radius" : 30.0, 59 | "mass" : 4.2, 60 | "velocity" : [0,0], 61 | "position" : [0,0] 62 | }, 63 | { 64 | "name" : "uranus", 65 | "radius" : 30.0, 66 | "mass" : 3.8, 67 | "velocity" : [0,0], 68 | "position" : [0,0] 69 | }] 70 | 71 | def makeNewPlanet(which): 72 | 73 | for pieceOfRock in planets: 74 | 75 | if pieceOfRock["name"] == which: 76 | return copy.deepcopy(pieceOfRock) 77 | 78 | return False 79 | 80 | -------------------------------------------------------------------------------- /Part 7/collisions.py: -------------------------------------------------------------------------------- 1 | import pygame, sys, random, math 2 | import pygame.locals as GAME_GLOBALS 3 | import pygame.event as GAME_EVENTS 4 | import pygame.time as GAME_TIME 5 | 6 | windowWidth = 1024 7 | windowHeight = 768 8 | 9 | pygame.init() 10 | clock = pygame.time.Clock() 11 | surface = pygame.display.set_mode((windowWidth, windowHeight)) 12 | 13 | pygame.display.set_caption('Collisions') 14 | 15 | previousMousePosition = [0,0] 16 | mousePosition = None 17 | mouseDown = False 18 | 19 | collidables = [] 20 | currentObject = None 21 | expanding = True 22 | 23 | drawAttractions = False 24 | 25 | gravity = 1.0 26 | 27 | def drawCollidables(): 28 | 29 | for anObject in collidables: 30 | anObject["position"][0] += anObject["velocity"][0] 31 | anObject["position"][1] += anObject["velocity"][1] 32 | 33 | pygame.draw.circle(surface, (255,255,255), (int(anObject["position"][0]), int(anObject["position"][1])), int(anObject["radius"]), 0) 34 | 35 | def drawCurrentObject(): 36 | 37 | global expanding, currentObject 38 | 39 | currentObject["position"][0] = mousePosition[0] 40 | currentObject["position"][1] = mousePosition[1] 41 | 42 | if expanding is True and currentObject["radius"] < 30: 43 | currentObject["radius"] += 0.2 44 | 45 | if currentObject["radius"] >= 30: 46 | expanding = False 47 | currentObject["radius"] = 9.9 48 | 49 | elif expanding is False and currentObject["radius"] > 1: 50 | currentObject["radius"] -= 0.2 51 | 52 | if currentObject["radius"] <= 1: 53 | expanding = True 54 | currentObject["radius"] = 1.1 55 | 56 | currentObject["mass"] = currentObject["radius"] 57 | 58 | pygame.draw.circle(surface, (255,0,0), (int(currentObject["position"][0]), int(currentObject["position"][1])), int(currentObject["radius"]), 0) 59 | 60 | def calculateMovement(): 61 | 62 | for anObject in collidables: 63 | 64 | for theOtherObject in collidables: 65 | 66 | if anObject is not theOtherObject: 67 | 68 | direction = (theOtherObject["position"][0] - anObject["position"][0], theOtherObject["position"][1] - anObject["position"][1]) 69 | magnitude = math.hypot(theOtherObject["position"][0] - anObject["position"][0], theOtherObject["position"][1] - anObject["position"][1]) 70 | nDirection = (direction[0] / magnitude, direction[1] / magnitude) 71 | 72 | if magnitude < 5: 73 | magnitude = 5 74 | elif magnitude > 15: 75 | magnitude = 15 76 | 77 | strength = ((gravity * anObject["mass"] * theOtherObject["mass"]) / (magnitude * magnitude)) / theOtherObject["mass"] 78 | 79 | appliedForce = (nDirection[0] * strength, nDirection[1] * strength) 80 | 81 | theOtherObject["velocity"][0] -= appliedForce[0] 82 | theOtherObject["velocity"][1] -= appliedForce[1] 83 | 84 | if drawAttractions is True: 85 | pygame.draw.line(surface, (255,255,255), (anObject["position"][0],anObject["position"][1]), (theOtherObject["position"][0],theOtherObject["position"][1]), 1) 86 | 87 | def handleCollisions(): 88 | 89 | h = 0 90 | 91 | while h < len(collidables): 92 | 93 | i = 0 94 | 95 | anObject = collidables[h] 96 | 97 | while i < len(collidables): 98 | 99 | otherObject = collidables[i] 100 | 101 | if anObject != otherObject: 102 | 103 | distance = math.hypot(otherObject["position"][0] - anObject["position"][0], otherObject["position"][1] - anObject["position"][1]) 104 | 105 | if distance < otherObject["radius"] + anObject["radius"]: 106 | 107 | # First we get the angle of the collision between the two objects 108 | collisionAngle = math.atan2(anObject["position"][1] - otherObject["position"][1], anObject["position"][0] - otherObject["position"][0]) 109 | 110 | #Then we need to calculate the speed of each object 111 | anObjectSpeed = math.sqrt(anObject["velocity"][0] * anObject["velocity"][0] + anObject["velocity"][1] * anObject["velocity"][1]) 112 | theOtherObjectSpeed = math.sqrt(otherObject["velocity"][0] * otherObject["velocity"][0] + otherObject["velocity"][1] * otherObject["velocity"][1]) 113 | 114 | # Now, we work out the direction of the objects in radians 115 | anObjectDirection = math.atan2(anObject["velocity"][1], anObject["velocity"][0]) 116 | theOtherObjectDirection = math.atan2(otherObject["velocity"][1], otherObject["velocity"][0]) 117 | 118 | # Now we calculate the new X/Y values of each object for the collision 119 | anObjectsNewVelocityX = anObjectSpeed * math.cos(anObjectDirection - collisionAngle) 120 | anObjectsNewVelocityY = anObjectSpeed * math.sin(anObjectDirection - collisionAngle) 121 | 122 | otherObjectsNewVelocityX = theOtherObjectSpeed * math.cos(theOtherObjectDirection - collisionAngle) 123 | otherObjectsNewVelocityY = theOtherObjectSpeed * math.sin(theOtherObjectDirection - collisionAngle) 124 | 125 | # We adjust the velocity based on the mass of the objects 126 | anObjectsFinalVelocityX = ((anObject["mass"] - otherObject["mass"]) * anObjectsNewVelocityX + (otherObject["mass"] + otherObject["mass"]) * otherObjectsNewVelocityX)/(anObject["mass"] + otherObject["mass"]) 127 | otherObjectsFinalVelocityX = ((anObject["mass"] + anObject["mass"]) * anObjectsNewVelocityX + (otherObject["mass"] - anObject["mass"]) * otherObjectsNewVelocityX)/(anObject["mass"] + otherObject["mass"]) 128 | 129 | # Now we set those values 130 | anObject["velocity"][0] = anObjectsFinalVelocityX 131 | otherObject["velocity"][0] = otherObjectsFinalVelocityX 132 | 133 | 134 | i += 1 135 | 136 | h += 1 137 | 138 | def handleMouseDown(): 139 | global currentObject 140 | 141 | currentObject = { 142 | "radius" : 3, 143 | "mass" : 3, 144 | "velocity" : [0,0], 145 | "position" : [0,0] 146 | } 147 | 148 | def quitGame(): 149 | pygame.quit() 150 | sys.exit() 151 | 152 | # 'main' loop 153 | while True: 154 | 155 | surface.fill((0,0,0)) 156 | mousePosition = pygame.mouse.get_pos() 157 | 158 | # Handle user and system events 159 | for event in GAME_EVENTS.get(): 160 | 161 | if event.type == pygame.KEYDOWN: 162 | 163 | if event.key == pygame.K_ESCAPE: 164 | quitGame() 165 | 166 | if event.type == pygame.KEYUP: 167 | 168 | if event.key == pygame.K_r: 169 | collidables = [] 170 | if event.key == pygame.K_a: 171 | if drawAttractions is True: 172 | drawAttractions = False 173 | elif drawAttractions is False: 174 | drawAttractions = True 175 | 176 | if event.type == pygame.MOUSEBUTTONDOWN: 177 | mouseDown = True 178 | handleMouseDown() 179 | 180 | if event.type == pygame.MOUSEBUTTONUP: 181 | mouseDown = False 182 | 183 | if event.type == GAME_GLOBALS.QUIT: 184 | quitGame() 185 | 186 | calculateMovement() 187 | handleCollisions() 188 | drawCollidables() 189 | 190 | if currentObject is not None: 191 | drawCurrentObject() 192 | 193 | # If our user has released the mouse, add the new anObject to the collidables list and let gravity do its thing 194 | if mouseDown is False: 195 | currentObject["velocity"][0] = (mousePosition[0] - previousMousePosition[0]) / 4 196 | currentObject["velocity"][1] = (mousePosition[1] - previousMousePosition[1]) / 4 197 | collidables.append(currentObject) 198 | currentObject = None 199 | 200 | # Store the previous mouse coordinates to create a vector when we release a new anObject 201 | previousMousePosition = mousePosition 202 | 203 | clock.tick(60) 204 | pygame.display.update() -------------------------------------------------------------------------------- /Part 8/assets/Barrel.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 8/assets/Barrel.png -------------------------------------------------------------------------------- /Part 8/assets/Barrel_break.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 8/assets/Barrel_break.png -------------------------------------------------------------------------------- /Part 8/assets/Fred-Left-Hit.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 8/assets/Fred-Left-Hit.png -------------------------------------------------------------------------------- /Part 8/assets/Fred-Left.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 8/assets/Fred-Left.png -------------------------------------------------------------------------------- /Part 8/assets/Fred-Right-Hit.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 8/assets/Fred-Right-Hit.png -------------------------------------------------------------------------------- /Part 8/assets/Fred-Right.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 8/assets/Fred-Right.png -------------------------------------------------------------------------------- /Part 8/assets/background.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 8/assets/background.png -------------------------------------------------------------------------------- /Part 8/assets/gameover.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 8/assets/gameover.png -------------------------------------------------------------------------------- /Part 8/assets/startgame.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 8/assets/startgame.png -------------------------------------------------------------------------------- /Part 8/freds_bad_day.py: -------------------------------------------------------------------------------- 1 | import pygame, sys, random, math 2 | import pygame.locals as GAME_GLOBALS 3 | import pygame.event as GAME_EVENTS 4 | import pygame.time as GAME_TIME 5 | import objects 6 | 7 | windowWidth = 1000 8 | windowHeight = 768 9 | 10 | pygame.init() 11 | pygame.font.init() 12 | clock = pygame.time.Clock() 13 | surface = pygame.display.set_mode((windowWidth, windowHeight), pygame.FULLSCREEN) 14 | 15 | pygame.display.set_caption('Fred\'s Bad Day') 16 | textFont = pygame.font.SysFont("monospace", 50) 17 | 18 | gameStarted = False 19 | gameStartedTime = 0 20 | gameFinishedTime = 0 21 | gameOver = False 22 | 23 | startScreen = pygame.image.load("assets/startgame.png") 24 | endScreen = pygame.image.load("assets/gameover.png") 25 | 26 | background = pygame.image.load("assets/background.png") 27 | Fred = objects.Fred(windowWidth / 2) 28 | Barrels = [] 29 | lastBarrel = 0 30 | lastBarrelSlot = 0 31 | barrelInterval = 1500 32 | 33 | goLeft = False 34 | goRight = False 35 | 36 | def quitGame(): 37 | pygame.quit() 38 | sys.exit() 39 | 40 | def newBarrel(): 41 | global Barrels, lastBarrel, lastBarrelSlot 42 | 43 | slot = random.randint(0, 12) 44 | 45 | while slot == lastBarrelSlot: 46 | slot = random.randint(0, 12) 47 | 48 | theBarrel = objects.Barrel(slot) 49 | theBarrel.loadImages(pygame) 50 | 51 | Barrels.append(theBarrel) 52 | lastBarrel = GAME_TIME.get_ticks() 53 | lastBarrelSlot = slot 54 | 55 | Fred.loadImages(pygame) 56 | 57 | # 'main' loop 58 | while True: 59 | 60 | timeTick = GAME_TIME.get_ticks() 61 | 62 | if gameStarted is True and gameOver is False: 63 | 64 | surface.blit(background, (0, 0)) 65 | 66 | Fred.draw(surface, timeTick) 67 | 68 | barrelsToRemove = [] 69 | 70 | for idx, barrel in enumerate(Barrels): 71 | barrel.move(windowHeight) 72 | barrel.draw(surface, pygame) 73 | 74 | if barrel.isBroken is False: 75 | 76 | hasCollided = barrel.checkForCollision(Fred); 77 | 78 | if hasCollided is True: 79 | barrel.split(timeTick) 80 | Fred.isHit = True 81 | Fred.timeHit = timeTick 82 | if Fred.health >= 10: 83 | Fred.health -= 10 84 | else : 85 | gameOver = True 86 | gameFinishedTime = timeTick 87 | 88 | elif timeTick - barrel.timeBroken > 1000: 89 | 90 | barrelsToRemove.append(idx) 91 | continue 92 | 93 | if barrel.needsRemoving is True: 94 | barrelsToRemove.append(idx) 95 | continue 96 | 97 | pygame.draw.rect(surface, (175,59,59), (0, windowHeight - 10, (windowWidth / 100) * Fred.health , 10)) 98 | 99 | for index in barrelsToRemove: 100 | del Barrels[index] 101 | 102 | if goLeft is True: 103 | Fred.moveLeft(0) 104 | 105 | if goRight is True: 106 | Fred.moveRight(windowWidth) 107 | 108 | elif gameStarted is False and gameOver is False: 109 | surface.blit(startScreen, (0, 0)) 110 | 111 | elif gameStarted is True and gameOver is True: 112 | surface.blit(endScreen, (0, 0)) 113 | timeLasted = (gameFinishedTime - gameStartedTime) / 1000 114 | 115 | if timeLasted < 10: 116 | timeLasted = "0" + str(timeLasted) 117 | else : 118 | timeLasted = str(timeLasted) 119 | 120 | renderedText = textFont.render(timeLasted, 1, (175,59,59)) 121 | surface.blit(renderedText, (495, 430)) 122 | 123 | # Handle user and system events 124 | for event in GAME_EVENTS.get(): 125 | 126 | if event.type == pygame.KEYDOWN: 127 | 128 | if event.key == pygame.K_ESCAPE: 129 | quitGame() 130 | elif event.key == pygame.K_LEFT: 131 | goLeft = True 132 | goRight = False 133 | elif event.key == pygame.K_RIGHT: 134 | goLeft = False 135 | goRight = True 136 | elif event.key == pygame.K_RETURN: 137 | if gameStarted is False and gameOver is False: 138 | gameStarted = True 139 | gameStartedTime = timeTick 140 | elif gameStarted is True and gameOver is True: 141 | Fred.reset(windowWidth / 2) 142 | 143 | Barrels = [] 144 | barrelInterval = 1500 145 | 146 | gameOver = False 147 | 148 | if event.type == pygame.KEYUP: 149 | 150 | if event.key == pygame.K_LEFT: 151 | goLeft = False 152 | if event.key == pygame.K_RIGHT: 153 | goRight = False 154 | 155 | if event.type == GAME_GLOBALS.QUIT: 156 | quitGame() 157 | 158 | clock.tick(60) 159 | pygame.display.update() 160 | 161 | if GAME_TIME.get_ticks() - lastBarrel > barrelInterval and gameStarted is True: 162 | newBarrel() 163 | if barrelInterval > 150: 164 | barrelInterval -= 50 165 | -------------------------------------------------------------------------------- /Part 8/objects.py: -------------------------------------------------------------------------------- 1 | class Fred(): 2 | 3 | x = 0 4 | y = 625 5 | 6 | isHit = False 7 | timeHit = 0 8 | health = 100 9 | 10 | leftImage = None 11 | rightImage = None 12 | leftImageHit = None 13 | rightImageHit = None 14 | 15 | direction = 1 16 | speed = 8 17 | pygame = None 18 | 19 | def reset(self, x): 20 | self.x = x 21 | self.y = 625 22 | 23 | self.isHit = False 24 | self.timeHit = 0 25 | self.health = 100 26 | 27 | self.direction = 1 28 | self.speed = 8 29 | self.pygame = None 30 | 31 | def moveLeft(self, leftBound): 32 | 33 | if self.direction is not 0: 34 | self.direction = 0 35 | 36 | if((self.x - self.speed) > leftBound): 37 | self.x -= self.speed 38 | 39 | def moveRight(self, rightBound): 40 | 41 | if self.direction is not 1: 42 | self.direction = 1 43 | 44 | if((self.x + self.speed) + 58 < rightBound): 45 | self.x += self.speed 46 | 47 | def loadImages(self, pygame): 48 | self.leftImage = pygame.image.load("assets/Fred-Left.png") 49 | self.rightImage = pygame.image.load("assets/Fred-Right.png") 50 | self.leftImageHit = pygame.image.load("assets/Fred-Left-Hit.png") 51 | self.rightImageHit = pygame.image.load("assets/Fred-Right-Hit.png") 52 | 53 | def draw(self, surface, time): 54 | 55 | if time - self.timeHit > 800: 56 | self.timeHit = 0 57 | self.isHit = False 58 | 59 | if self.direction is 1: 60 | if self.isHit is False: 61 | surface.blit(self.rightImage, (self.x, self.y)) 62 | else : 63 | surface.blit(self.rightImageHit, (self.x, self.y)) 64 | else : 65 | if self.isHit is False: 66 | surface.blit(self.leftImage, (self.x, self.y)) 67 | else : 68 | surface.blit(self.leftImageHit, (self.x, self.y)) 69 | 70 | def __init__(self, x): 71 | self.x = x 72 | 73 | class Barrel(): 74 | 75 | slots = [(4, 103), (82, 27), (157, 104), (234, 27), (310, 104), (388, 27), (463, 104), (539, 27), (615, 104), (691, 27), (768, 104), (845, 27), (920, 104)] 76 | slot = 0 77 | x = 0 78 | y = 0 79 | 80 | image = None 81 | brokenImage = None 82 | 83 | isBroken = False 84 | timeBroken = 0 85 | needsRemoving = False 86 | 87 | size = [33,22] 88 | ratio = 0.66 89 | 90 | vy = 1.5 91 | gravity = 1.05 92 | maxY = 20 93 | 94 | def split(self, time): 95 | self.isBroken = True 96 | self.timeBroken = time 97 | self.vy = 5 98 | self.x -= 10 99 | 100 | def checkForCollision(self, fred): 101 | 102 | hitX = False 103 | hitY = False 104 | 105 | if fred.x > self.x and fred.x < self.x + 75: 106 | hitX = True 107 | elif fred.x + 57 > self.x and fred.x + 57 < self.x + 75: 108 | hitX = True 109 | if fred.y + 120 > self.y and fred.y < self.y: 110 | hitY = True 111 | elif fred.y < self.y + 48: 112 | hitY = True 113 | if hitX is True and hitY is True: 114 | return True 115 | 116 | def loadImages(self, pygame): 117 | self.image = pygame.image.load("assets/Barrel.png") 118 | self.brokenImage = pygame.image.load("assets/Barrel_break.png") 119 | 120 | def move(self, windowHeight): 121 | 122 | if self.vy < self.maxY: 123 | self.vy = self.vy * self.gravity 124 | self.y += self.vy 125 | 126 | if self.y > windowHeight: 127 | self.needsRemoving = True 128 | 129 | def draw(self, surface, pygame): 130 | if self.isBroken is True: 131 | surface.blit(self.brokenImage, (self.x, self.y)) 132 | else : 133 | surface.blit(self.image, (self.x, self.y)) 134 | 135 | def __init__(self, slot): 136 | self.slot = slot 137 | self.x = self.slots[slot][0] 138 | self.y = self.slots[slot][1] + 24 139 | -------------------------------------------------------------------------------- /Part 9/aliens.py: -------------------------------------------------------------------------------- 1 | import pygame, sys, random, math 2 | import pygame.locals as GAME_GLOBALS 3 | import pygame.event as GAME_EVENTS 4 | import pygame.time as GAME_TIME 5 | import ships 6 | 7 | windowWidth = 1024 8 | windowHeight = 614 9 | 10 | pygame.init() 11 | pygame.font.init() 12 | clock = pygame.time.Clock() 13 | surface = pygame.display.set_mode((windowWidth, windowHeight)) 14 | 15 | pygame.display.set_caption('Alien\'s Are Gonna Kill Me!') 16 | textFont = pygame.font.SysFont("monospace", 50) 17 | 18 | gameStarted = False 19 | gameStartedTime = 0 20 | gameFinishedTime = 0 21 | gameOver = False 22 | 23 | #Mouse Variables 24 | mousePosition = (0,0) 25 | mouseStates = None 26 | mouseDown = False 27 | 28 | #Image Variables 29 | startScreen = pygame.image.load("assets/start_screen.png") 30 | background = pygame.image.load("assets/background.png") 31 | 32 | #Ships 33 | ship = ships.Player(windowWidth / 2, windowHeight, pygame, surface) 34 | enemyShips = [] 35 | 36 | lastEnemyCreated = 0 37 | enemyInterval = random.randint(1000, 2500) 38 | 39 | #Sound Setup 40 | pygame.mixer.init() 41 | 42 | def updateGame(): 43 | 44 | global mouseDown, gameOver 45 | 46 | if mouseStates[0] is 1 and mouseDown is False: 47 | ship.fire() 48 | mouseDown = True 49 | elif mouseStates[0] is 0 and mouseDown is True: 50 | mouseDown = False 51 | 52 | ship.setPosition(mousePosition) 53 | 54 | enemiesToRemove = [] 55 | 56 | for idx, enemy in enumerate(enemyShips): 57 | 58 | if enemy.y < windowHeight: 59 | enemy.move() 60 | enemy.tryToFire() 61 | shipIsDestroyed = enemy.checkForHit(ship) 62 | enemyIsDestroyed = ship.checkForHit(enemy) 63 | 64 | if enemyIsDestroyed is True: 65 | enemiesToRemove.append(idx) 66 | 67 | if shipIsDestroyed is True: 68 | gameOver = True 69 | quitGame() 70 | 71 | else: 72 | enemiesToRemove.append(idx) 73 | 74 | for idx in enemiesToRemove: 75 | del enemyShips[idx] 76 | 77 | def drawGame(): 78 | surface.blit(background, (0, 0)) 79 | ship.draw() 80 | ship.drawBullets() 81 | 82 | for enemy in enemyShips: 83 | enemy.draw() 84 | enemy.drawBullets() 85 | 86 | def quitGame(): 87 | pygame.quit() 88 | sys.exit() 89 | 90 | # 'main' loop 91 | while True: 92 | 93 | timeTick = GAME_TIME.get_ticks() 94 | mousePosition = pygame.mouse.get_pos() 95 | mouseStates = pygame.mouse.get_pressed() 96 | 97 | if gameStarted is True and gameOver is False: 98 | 99 | updateGame() 100 | drawGame() 101 | 102 | elif gameStarted is False and gameOver is False: 103 | surface.blit(startScreen, (0, 0)) 104 | 105 | if mouseStates[0] is 1: 106 | 107 | if mousePosition[0] > 445 and mousePosition[0] < 580 and mousePosition[1] > 450 and mousePosition[1] < 510: 108 | 109 | gameStarted = True 110 | 111 | elif mouseStates[0] is 0 and mouseDown is True: 112 | mouseDown = False 113 | 114 | elif gameStarted is True and gameOver is True: 115 | surface.blit(startScreen, (0, 0)) 116 | timeLasted = (gameFinishedTime - gameStartedTime) / 1000 117 | 118 | # Handle user and system events 119 | for event in GAME_EVENTS.get(): 120 | 121 | if event.type == pygame.KEYDOWN: 122 | 123 | if event.key == pygame.K_ESCAPE: 124 | quitGame() 125 | 126 | if GAME_TIME.get_ticks() - lastEnemyCreated > enemyInterval and gameStarted is True: 127 | enemyShips.append(ships.Enemy(random.randint(0, windowWidth), -60, pygame, surface, 1)) 128 | lastEnemyCreated = GAME_TIME.get_ticks() 129 | 130 | if event.type == GAME_GLOBALS.QUIT: 131 | quitGame() 132 | 133 | clock.tick(60) 134 | pygame.display.update() 135 | -------------------------------------------------------------------------------- /Part 9/assets/background.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 9/assets/background.png -------------------------------------------------------------------------------- /Part 9/assets/start_screen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 9/assets/start_screen.png -------------------------------------------------------------------------------- /Part 9/assets/them_pellet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 9/assets/them_pellet.png -------------------------------------------------------------------------------- /Part 9/assets/them_ship.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 9/assets/them_ship.png -------------------------------------------------------------------------------- /Part 9/assets/you_pellet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 9/assets/you_pellet.png -------------------------------------------------------------------------------- /Part 9/assets/you_ship.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 9/assets/you_ship.png -------------------------------------------------------------------------------- /Part 9/projectiles.py: -------------------------------------------------------------------------------- 1 | class Bullet(): 2 | 3 | x = 0 4 | y = 0 5 | image = None 6 | pygame = None 7 | surface = None 8 | width = 0 9 | height = 0 10 | speed = 0.0 11 | 12 | def loadImages(self): 13 | self.image = self.pygame.image.load(self.image) 14 | 15 | def draw(self): 16 | self.surface.blit(self.image, (self.x, self.y)) 17 | 18 | def move(self): 19 | self.y += self.speed 20 | 21 | def __init__(self, x, y, pygame, surface, speed, image): 22 | self.x = x 23 | self.y = y 24 | self.pygame = pygame 25 | self.surface = surface 26 | self.image = image 27 | self.loadImages() 28 | self.speed = speed 29 | 30 | dimensions = self.image.get_rect().size 31 | self.width = dimensions[0] 32 | self.height = dimensions[1] 33 | 34 | self.x -= self.width / 2 -------------------------------------------------------------------------------- /Part 9/ships.py: -------------------------------------------------------------------------------- 1 | import projectiles, random 2 | 3 | class Player(): 4 | 5 | x = 0 6 | y = 0 7 | firing = False 8 | image = None 9 | soundEffect = 'sounds/player_laser.wav' 10 | pygame = None 11 | surface = None 12 | width = 0 13 | height = 0 14 | bullets = [] 15 | bulletImage = "assets/you_pellet.png" 16 | bulletSpeed = -10 17 | health = 5 18 | 19 | def loadImages(self): 20 | self.image = self.pygame.image.load("assets/you_ship.png") 21 | 22 | def draw(self): 23 | self.surface.blit(self.image, (self.x, self.y)) 24 | 25 | def setPosition(self, pos): 26 | self.x = pos[0] - self.width / 2 27 | # self.y = pos[1] 28 | 29 | def fire(self): 30 | self.bullets.append(projectiles.Bullet(self.x + self.width / 2, self.y, self.pygame, self.surface, self.bulletSpeed, self.bulletImage)) 31 | a = self.pygame.mixer.Sound(self.soundEffect) 32 | a.set_volume(0.2) 33 | a.play() 34 | 35 | 36 | def drawBullets(self): 37 | for b in self.bullets: 38 | b.move() 39 | b.draw() 40 | 41 | def registerHit(self): 42 | self.health -= 1 43 | 44 | def checkForHit(self, thingToCheckAgainst): 45 | bulletsToRemove = [] 46 | 47 | for idx, b in enumerate(self.bullets): 48 | if b.x > thingToCheckAgainst.x and b.x < thingToCheckAgainst.x + thingToCheckAgainst.width: 49 | if b.y > thingToCheckAgainst.y and b.y < thingToCheckAgainst.y + thingToCheckAgainst.height: 50 | thingToCheckAgainst.registerHit() 51 | bulletsToRemove.append(idx) 52 | 53 | for usedBullet in bulletsToRemove: 54 | del self.bullets[usedBullet] 55 | 56 | if thingToCheckAgainst.health <= 0: 57 | return True 58 | 59 | def __init__(self, x, y, pygame, surface): 60 | self.x = x 61 | self.y = y 62 | self.pygame = pygame 63 | self.surface = surface 64 | self.loadImages() 65 | 66 | dimensions = self.image.get_rect().size 67 | self.width = dimensions[0] 68 | self.height = dimensions[1] 69 | 70 | self.x -= self.width / 2 71 | self.y -= self.height + 10 72 | 73 | class Enemy(Player): 74 | 75 | x = 0 76 | y = 0 77 | firing = False 78 | image = None 79 | soundEffect = 'sounds/enemy_laser.wav' 80 | bulletImage = "assets/them_pellet.png" 81 | bulletSpeed = 10 82 | speed = 2 83 | 84 | def move(self): 85 | self.y += self.speed 86 | 87 | def tryToFire(self): 88 | shouldFire = random.random() 89 | 90 | if shouldFire <= 0.01: 91 | self.fire() 92 | 93 | def loadImages(self): 94 | self.image = self.pygame.image.load("assets/them_ship.png") 95 | 96 | def __init__(self, x, y, pygame, surface, health): 97 | self.x = x 98 | self.y = y 99 | self.pygame = pygame 100 | self.surface = surface 101 | self.loadImages() 102 | self.bullets = [] 103 | self.health = health 104 | 105 | dimensions = self.image.get_rect().size 106 | self.width = dimensions[0] 107 | self.height = dimensions[1] 108 | 109 | self.x -= self.width / 2 -------------------------------------------------------------------------------- /Part 9/sounds/enemy_laser.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 9/sounds/enemy_laser.wav -------------------------------------------------------------------------------- /Part 9/sounds/player_laser.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanmtracey/Games-with-Pygame/2d8abedbf9c3d32de79cd15d147f8128a843fcef/Part 9/sounds/player_laser.wav -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #### The code + assets for 2 | # Make Games with Python 3 | ##### A magazine series-turned-book I wrote for Raspberry Pi's 'The MagPi' magazine 4 | 5 | This repo contains all of the code, images, and sounds you'll need to follow the chapters and tutorials of my book, 'Make Games with Python', which you can grab for free (no strings attached, it's CC :D) [here](https://www.raspberrypi.org/magpi/issues/essentials-games-vol1/) but buying it for iOS/Android helps support the Raspberry Pi Foundation's ongoing mission to democratise computing. 6 | 7 | ![Cover image](http://sean.mtracey.org/assets/images/external/make-games-with-pygame-cover-medium.jpg) --------------------------------------------------------------------------------