├── .gitattributes ├── Jumpy.zip ├── LICENSE ├── assets ├── bg.png ├── bird.png ├── death.mp3 ├── jump.mp3 ├── jump.png ├── music.mp3 └── wood.png ├── enemy.py ├── jumpy_tut1.py ├── jumpy_tut10.py ├── jumpy_tut11.py ├── jumpy_tut12.py ├── jumpy_tut13.py ├── jumpy_tut14.py ├── jumpy_tut15.py ├── jumpy_tut2.py ├── jumpy_tut3.py ├── jumpy_tut4.py ├── jumpy_tut5.py ├── jumpy_tut6.py ├── jumpy_tut7.py ├── jumpy_tut8.py ├── jumpy_tut9.py └── spritesheet.py /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /Jumpy.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/russs123/Jumpy/220aed15c759763a170ae8277b4ea2597246b544/Jumpy.zip -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 russs123 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /assets/bg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/russs123/Jumpy/220aed15c759763a170ae8277b4ea2597246b544/assets/bg.png -------------------------------------------------------------------------------- /assets/bird.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/russs123/Jumpy/220aed15c759763a170ae8277b4ea2597246b544/assets/bird.png -------------------------------------------------------------------------------- /assets/death.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/russs123/Jumpy/220aed15c759763a170ae8277b4ea2597246b544/assets/death.mp3 -------------------------------------------------------------------------------- /assets/jump.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/russs123/Jumpy/220aed15c759763a170ae8277b4ea2597246b544/assets/jump.mp3 -------------------------------------------------------------------------------- /assets/jump.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/russs123/Jumpy/220aed15c759763a170ae8277b4ea2597246b544/assets/jump.png -------------------------------------------------------------------------------- /assets/music.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/russs123/Jumpy/220aed15c759763a170ae8277b4ea2597246b544/assets/music.mp3 -------------------------------------------------------------------------------- /assets/wood.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/russs123/Jumpy/220aed15c759763a170ae8277b4ea2597246b544/assets/wood.png -------------------------------------------------------------------------------- /enemy.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | import random 3 | 4 | class Enemy(pygame.sprite.Sprite): 5 | def __init__(self, SCREEN_WIDTH, y, sprite_sheet, scale): 6 | pygame.sprite.Sprite.__init__(self) 7 | #define variables 8 | self.animation_list = [] 9 | self.frame_index = 0 10 | self.update_time = pygame.time.get_ticks() 11 | self.direction = random.choice([-1, 1]) 12 | if self.direction == 1: 13 | self.flip = True 14 | else: 15 | self.flip = False 16 | 17 | #load images from spritesheet 18 | animation_steps = 8 19 | for animation in range(animation_steps): 20 | image = sprite_sheet.get_image(animation, 32, 32, scale, (0, 0, 0)) 21 | image = pygame.transform.flip(image, self.flip, False) 22 | image.set_colorkey((0, 0, 0)) 23 | self.animation_list.append(image) 24 | 25 | #select starting image and create rectangle from it 26 | self.image = self.animation_list[self.frame_index] 27 | self.rect = self.image.get_rect() 28 | 29 | if self.direction == 1: 30 | self.rect.x = 0 31 | else: 32 | self.rect.x = SCREEN_WIDTH 33 | self.rect.y = y 34 | 35 | def update(self, scroll, SCREEN_WIDTH): 36 | #update animation 37 | ANIMATION_COOLDOWN = 50 38 | #update image depending on current frame 39 | self.image = self.animation_list[self.frame_index] 40 | #check if enough time has passed since the last update 41 | if pygame.time.get_ticks() - self.update_time > ANIMATION_COOLDOWN: 42 | self.update_time = pygame.time.get_ticks() 43 | self.frame_index += 1 44 | #if the animation has run out then reset back to the start 45 | if self.frame_index >= len(self.animation_list): 46 | self.frame_index = 0 47 | 48 | #move enemy 49 | self.rect.x += self.direction * 2 50 | self.rect.y += scroll 51 | 52 | #check if gone off screen 53 | if self.rect.right < 0 or self.rect.left > SCREEN_WIDTH: 54 | self.kill() -------------------------------------------------------------------------------- /jumpy_tut1.py: -------------------------------------------------------------------------------- 1 | #import libraries 2 | import pygame 3 | 4 | #initialise pygame 5 | pygame.init() 6 | 7 | #game window dimensions 8 | SCREEN_WIDTH = 400 9 | SCREEN_HEIGHT = 600 10 | 11 | #create game window 12 | screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) 13 | pygame.display.set_caption('Jumpy') 14 | 15 | #load images 16 | bg_image = pygame.image.load('assets/bg.png').convert_alpha() 17 | 18 | 19 | 20 | #game loop 21 | run = True 22 | while run: 23 | 24 | #draw background 25 | screen.blit(bg_image, (0, 0)) 26 | 27 | 28 | 29 | #event handler 30 | for event in pygame.event.get(): 31 | if event.type == pygame.QUIT: 32 | run = False 33 | 34 | 35 | #update display window 36 | pygame.display.update() 37 | 38 | 39 | 40 | pygame.quit() 41 | 42 | -------------------------------------------------------------------------------- /jumpy_tut10.py: -------------------------------------------------------------------------------- 1 | #import libraries 2 | import pygame 3 | import random 4 | import os 5 | 6 | #initialise pygame 7 | pygame.init() 8 | 9 | #game window dimensions 10 | SCREEN_WIDTH = 400 11 | SCREEN_HEIGHT = 600 12 | 13 | #create game window 14 | screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) 15 | pygame.display.set_caption('Jumpy') 16 | 17 | #set frame rate 18 | clock = pygame.time.Clock() 19 | FPS = 60 20 | 21 | #game variables 22 | SCROLL_THRESH = 200 23 | GRAVITY = 1 24 | MAX_PLATFORMS = 10 25 | scroll = 0 26 | bg_scroll = 0 27 | game_over = False 28 | score = 0 29 | fade_counter = 0 30 | 31 | if os.path.exists('score.txt'): 32 | with open('score.txt', 'r') as file: 33 | high_score = int(file.read()) 34 | else: 35 | high_score = 0 36 | 37 | #define colours 38 | WHITE = (255, 255, 255) 39 | BLACK = (0, 0, 0) 40 | PANEL = (153, 217, 234) 41 | 42 | #define font 43 | font_small = pygame.font.SysFont('Lucida Sans', 20) 44 | font_big = pygame.font.SysFont('Lucida Sans', 24) 45 | 46 | #load images 47 | jumpy_image = pygame.image.load('assets/jump.png').convert_alpha() 48 | bg_image = pygame.image.load('assets/bg.png').convert_alpha() 49 | platform_image = pygame.image.load('assets/wood.png').convert_alpha() 50 | 51 | #function for outputting text onto the screen 52 | def draw_text(text, font, text_col, x, y): 53 | img = font.render(text, True, text_col) 54 | screen.blit(img, (x, y)) 55 | 56 | #function for drawing info panel 57 | def draw_panel(): 58 | pygame.draw.rect(screen, PANEL, (0, 0, SCREEN_WIDTH, 30)) 59 | pygame.draw.line(screen, WHITE, (0, 30), (SCREEN_WIDTH, 30), 2) 60 | draw_text('SCORE: ' + str(score), font_small, WHITE, 0, 0) 61 | 62 | 63 | #function for drawing the background 64 | def draw_bg(bg_scroll): 65 | screen.blit(bg_image, (0, 0 + bg_scroll)) 66 | screen.blit(bg_image, (0, -600 + bg_scroll)) 67 | 68 | #player class 69 | class Player(): 70 | def __init__(self, x, y): 71 | self.image = pygame.transform.scale(jumpy_image, (45, 45)) 72 | self.width = 25 73 | self.height = 40 74 | self.rect = pygame.Rect(0, 0, self.width, self.height) 75 | self.rect.center = (x, y) 76 | self.vel_y = 0 77 | self.flip = False 78 | 79 | def move(self): 80 | #reset variables 81 | scroll = 0 82 | dx = 0 83 | dy = 0 84 | 85 | #process keypresses 86 | key = pygame.key.get_pressed() 87 | if key[pygame.K_a]: 88 | dx = -10 89 | self.flip = True 90 | if key[pygame.K_d]: 91 | dx = 10 92 | self.flip = False 93 | 94 | #gravity 95 | self.vel_y += GRAVITY 96 | dy += self.vel_y 97 | 98 | #ensure player doesn't go off the edge of the screen 99 | if self.rect.left + dx < 0: 100 | dx = -self.rect.left 101 | if self.rect.right + dx > SCREEN_WIDTH: 102 | dx = SCREEN_WIDTH - self.rect.right 103 | 104 | 105 | #check collision with platforms 106 | for platform in platform_group: 107 | #collision in the y direction 108 | if platform.rect.colliderect(self.rect.x, self.rect.y + dy, self.width, self.height): 109 | #check if above the platform 110 | if self.rect.bottom < platform.rect.centery: 111 | if self.vel_y > 0: 112 | self.rect.bottom = platform.rect.top 113 | dy = 0 114 | self.vel_y = -20 115 | 116 | #check if the player has bounced to the top of the screen 117 | if self.rect.top <= SCROLL_THRESH: 118 | #if player is jumping 119 | if self.vel_y < 0: 120 | scroll = -dy 121 | 122 | #update rectangle position 123 | self.rect.x += dx 124 | self.rect.y += dy + scroll 125 | 126 | return scroll 127 | 128 | def draw(self): 129 | screen.blit(pygame.transform.flip(self.image, self.flip, False), (self.rect.x - 12, self.rect.y - 5)) 130 | pygame.draw.rect(screen, WHITE, self.rect, 2) 131 | 132 | 133 | 134 | #platform class 135 | class Platform(pygame.sprite.Sprite): 136 | def __init__(self, x, y, width): 137 | pygame.sprite.Sprite.__init__(self) 138 | self.image = pygame.transform.scale(platform_image, (width, 10)) 139 | self.rect = self.image.get_rect() 140 | self.rect.x = x 141 | self.rect.y = y 142 | 143 | def update(self, scroll): 144 | 145 | #update platform's vertical position 146 | self.rect.y += scroll 147 | 148 | #check if platform has gone off the screen 149 | if self.rect.top > SCREEN_HEIGHT: 150 | self.kill() 151 | 152 | #player instance 153 | jumpy = Player(SCREEN_WIDTH // 2, SCREEN_HEIGHT - 150) 154 | 155 | #create sprite groups 156 | platform_group = pygame.sprite.Group() 157 | 158 | #create starting platform 159 | platform = Platform(SCREEN_WIDTH // 2 - 50, SCREEN_HEIGHT - 50, 100) 160 | platform_group.add(platform) 161 | 162 | #game loop 163 | run = True 164 | while run: 165 | 166 | clock.tick(FPS) 167 | 168 | if game_over == False: 169 | scroll = jumpy.move() 170 | 171 | #draw background 172 | bg_scroll += scroll 173 | if bg_scroll >= 600: 174 | bg_scroll = 0 175 | draw_bg(bg_scroll) 176 | 177 | #generate platforms 178 | if len(platform_group) < MAX_PLATFORMS: 179 | p_w = random.randint(40, 60) 180 | p_x = random.randint(0, SCREEN_WIDTH - p_w) 181 | p_y = platform.rect.y - random.randint(80, 120) 182 | platform = Platform(p_x, p_y, p_w) 183 | platform_group.add(platform) 184 | 185 | #update platforms 186 | platform_group.update(scroll) 187 | 188 | #update score 189 | if scroll > 0: 190 | score += scroll 191 | 192 | #draw line at previous high score 193 | pygame.draw.line(screen, WHITE, (0, score - high_score + SCROLL_THRESH), (SCREEN_WIDTH, score - high_score + SCROLL_THRESH), 3) 194 | draw_text('HIGH SCORE', font_small, WHITE, SCREEN_WIDTH - 130, score - high_score + SCROLL_THRESH) 195 | 196 | #draw sprites 197 | platform_group.draw(screen) 198 | jumpy.draw() 199 | 200 | #draw panel 201 | draw_panel() 202 | 203 | #check game over 204 | if jumpy.rect.top > SCREEN_HEIGHT: 205 | game_over = True 206 | else: 207 | if fade_counter < SCREEN_WIDTH: 208 | fade_counter += 5 209 | for y in range(0, 6, 2): 210 | pygame.draw.rect(screen, BLACK, (0, y * 100, fade_counter, 100)) 211 | pygame.draw.rect(screen, BLACK, (SCREEN_WIDTH - fade_counter, (y + 1) * 100, SCREEN_WIDTH, 100)) 212 | else: 213 | draw_text('GAME OVER!', font_big, WHITE, 130, 200) 214 | draw_text('SCORE: ' + str(score), font_big, WHITE, 130, 250) 215 | draw_text('PRESS SPACE TO PLAY AGAIN', font_big, WHITE, 40, 300) 216 | #update high score 217 | if score > high_score: 218 | high_score = score 219 | with open('score.txt', 'w') as file: 220 | file.write(str(high_score)) 221 | key = pygame.key.get_pressed() 222 | if key[pygame.K_SPACE]: 223 | #reset variables 224 | game_over = False 225 | score = 0 226 | scroll = 0 227 | fade_counter = 0 228 | #reposition jumpy 229 | jumpy.rect.center = (SCREEN_WIDTH // 2, SCREEN_HEIGHT - 150) 230 | #reset platforms 231 | platform_group.empty() 232 | #create starting platform 233 | platform = Platform(SCREEN_WIDTH // 2 - 50, SCREEN_HEIGHT - 50, 100) 234 | platform_group.add(platform) 235 | 236 | 237 | #event handler 238 | for event in pygame.event.get(): 239 | if event.type == pygame.QUIT: 240 | #update high score 241 | if score > high_score: 242 | high_score = score 243 | with open('score.txt', 'w') as file: 244 | file.write(str(high_score)) 245 | run = False 246 | 247 | 248 | #update display window 249 | pygame.display.update() 250 | 251 | 252 | 253 | pygame.quit() 254 | 255 | -------------------------------------------------------------------------------- /jumpy_tut11.py: -------------------------------------------------------------------------------- 1 | #import libraries 2 | import pygame 3 | import random 4 | import os 5 | 6 | #initialise pygame 7 | pygame.init() 8 | 9 | #game window dimensions 10 | SCREEN_WIDTH = 400 11 | SCREEN_HEIGHT = 600 12 | 13 | #create game window 14 | screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) 15 | pygame.display.set_caption('Jumpy') 16 | 17 | #set frame rate 18 | clock = pygame.time.Clock() 19 | FPS = 60 20 | 21 | #game variables 22 | SCROLL_THRESH = 200 23 | GRAVITY = 1 24 | MAX_PLATFORMS = 10 25 | scroll = 0 26 | bg_scroll = 0 27 | game_over = False 28 | score = 0 29 | fade_counter = 0 30 | 31 | if os.path.exists('score.txt'): 32 | with open('score.txt', 'r') as file: 33 | high_score = int(file.read()) 34 | else: 35 | high_score = 0 36 | 37 | #define colours 38 | WHITE = (255, 255, 255) 39 | BLACK = (0, 0, 0) 40 | PANEL = (153, 217, 234) 41 | 42 | #define font 43 | font_small = pygame.font.SysFont('Lucida Sans', 20) 44 | font_big = pygame.font.SysFont('Lucida Sans', 24) 45 | 46 | #load images 47 | jumpy_image = pygame.image.load('assets/jump.png').convert_alpha() 48 | bg_image = pygame.image.load('assets/bg.png').convert_alpha() 49 | platform_image = pygame.image.load('assets/wood.png').convert_alpha() 50 | 51 | #function for outputting text onto the screen 52 | def draw_text(text, font, text_col, x, y): 53 | img = font.render(text, True, text_col) 54 | screen.blit(img, (x, y)) 55 | 56 | #function for drawing info panel 57 | def draw_panel(): 58 | pygame.draw.rect(screen, PANEL, (0, 0, SCREEN_WIDTH, 30)) 59 | pygame.draw.line(screen, WHITE, (0, 30), (SCREEN_WIDTH, 30), 2) 60 | draw_text('SCORE: ' + str(score), font_small, WHITE, 0, 0) 61 | 62 | 63 | #function for drawing the background 64 | def draw_bg(bg_scroll): 65 | screen.blit(bg_image, (0, 0 + bg_scroll)) 66 | screen.blit(bg_image, (0, -600 + bg_scroll)) 67 | 68 | #player class 69 | class Player(): 70 | def __init__(self, x, y): 71 | self.image = pygame.transform.scale(jumpy_image, (45, 45)) 72 | self.width = 25 73 | self.height = 40 74 | self.rect = pygame.Rect(0, 0, self.width, self.height) 75 | self.rect.center = (x, y) 76 | self.vel_y = 0 77 | self.flip = False 78 | 79 | def move(self): 80 | #reset variables 81 | scroll = 0 82 | dx = 0 83 | dy = 0 84 | 85 | #process keypresses 86 | key = pygame.key.get_pressed() 87 | if key[pygame.K_a]: 88 | dx = -10 89 | self.flip = True 90 | if key[pygame.K_d]: 91 | dx = 10 92 | self.flip = False 93 | 94 | #gravity 95 | self.vel_y += GRAVITY 96 | dy += self.vel_y 97 | 98 | #ensure player doesn't go off the edge of the screen 99 | if self.rect.left + dx < 0: 100 | dx = -self.rect.left 101 | if self.rect.right + dx > SCREEN_WIDTH: 102 | dx = SCREEN_WIDTH - self.rect.right 103 | 104 | 105 | #check collision with platforms 106 | for platform in platform_group: 107 | #collision in the y direction 108 | if platform.rect.colliderect(self.rect.x, self.rect.y + dy, self.width, self.height): 109 | #check if above the platform 110 | if self.rect.bottom < platform.rect.centery: 111 | if self.vel_y > 0: 112 | self.rect.bottom = platform.rect.top 113 | dy = 0 114 | self.vel_y = -20 115 | 116 | #check if the player has bounced to the top of the screen 117 | if self.rect.top <= SCROLL_THRESH: 118 | #if player is jumping 119 | if self.vel_y < 0: 120 | scroll = -dy 121 | 122 | #update rectangle position 123 | self.rect.x += dx 124 | self.rect.y += dy + scroll 125 | 126 | return scroll 127 | 128 | def draw(self): 129 | screen.blit(pygame.transform.flip(self.image, self.flip, False), (self.rect.x - 12, self.rect.y - 5)) 130 | pygame.draw.rect(screen, WHITE, self.rect, 2) 131 | 132 | 133 | 134 | #platform class 135 | class Platform(pygame.sprite.Sprite): 136 | def __init__(self, x, y, width, moving): 137 | pygame.sprite.Sprite.__init__(self) 138 | self.image = pygame.transform.scale(platform_image, (width, 10)) 139 | self.moving = moving 140 | self.move_counter = random.randint(0, 50) 141 | self.direction = random.choice([-1, 1]) 142 | self.speed = random.randint(1, 2) 143 | self.rect = self.image.get_rect() 144 | self.rect.x = x 145 | self.rect.y = y 146 | 147 | def update(self, scroll): 148 | #moving platform side to side if it is a moving platform 149 | if self.moving == True: 150 | self.move_counter += 1 151 | self.rect.x += self.direction * self.speed 152 | 153 | #change platform direction if it has moved fully or hit a wall 154 | if self.move_counter >= 100 or self.rect.left < 0 or self.rect.right > SCREEN_WIDTH: 155 | self.direction *= -1 156 | self.move_counter = 0 157 | 158 | #update platform's vertical position 159 | self.rect.y += scroll 160 | 161 | #check if platform has gone off the screen 162 | if self.rect.top > SCREEN_HEIGHT: 163 | self.kill() 164 | 165 | #player instance 166 | jumpy = Player(SCREEN_WIDTH // 2, SCREEN_HEIGHT - 150) 167 | 168 | #create sprite groups 169 | platform_group = pygame.sprite.Group() 170 | 171 | #create starting platform 172 | platform = Platform(SCREEN_WIDTH // 2 - 50, SCREEN_HEIGHT - 50, 100, False) 173 | platform_group.add(platform) 174 | 175 | #game loop 176 | run = True 177 | while run: 178 | 179 | clock.tick(FPS) 180 | 181 | if game_over == False: 182 | scroll = jumpy.move() 183 | 184 | #draw background 185 | bg_scroll += scroll 186 | if bg_scroll >= 600: 187 | bg_scroll = 0 188 | draw_bg(bg_scroll) 189 | 190 | #generate platforms 191 | if len(platform_group) < MAX_PLATFORMS: 192 | p_w = random.randint(40, 60) 193 | p_x = random.randint(0, SCREEN_WIDTH - p_w) 194 | p_y = platform.rect.y - random.randint(80, 120) 195 | p_type = random.randint(1, 2) 196 | if p_type == 1 and score > 500: 197 | p_moving = True 198 | else: 199 | p_moving = False 200 | platform = Platform(p_x, p_y, p_w, p_moving) 201 | platform_group.add(platform) 202 | 203 | #update platforms 204 | platform_group.update(scroll) 205 | 206 | #update score 207 | if scroll > 0: 208 | score += scroll 209 | 210 | #draw line at previous high score 211 | pygame.draw.line(screen, WHITE, (0, score - high_score + SCROLL_THRESH), (SCREEN_WIDTH, score - high_score + SCROLL_THRESH), 3) 212 | draw_text('HIGH SCORE', font_small, WHITE, SCREEN_WIDTH - 130, score - high_score + SCROLL_THRESH) 213 | 214 | #draw sprites 215 | platform_group.draw(screen) 216 | jumpy.draw() 217 | 218 | #draw panel 219 | draw_panel() 220 | 221 | #check game over 222 | if jumpy.rect.top > SCREEN_HEIGHT: 223 | game_over = True 224 | else: 225 | if fade_counter < SCREEN_WIDTH: 226 | fade_counter += 5 227 | for y in range(0, 6, 2): 228 | pygame.draw.rect(screen, BLACK, (0, y * 100, fade_counter, 100)) 229 | pygame.draw.rect(screen, BLACK, (SCREEN_WIDTH - fade_counter, (y + 1) * 100, SCREEN_WIDTH, 100)) 230 | else: 231 | draw_text('GAME OVER!', font_big, WHITE, 130, 200) 232 | draw_text('SCORE: ' + str(score), font_big, WHITE, 130, 250) 233 | draw_text('PRESS SPACE TO PLAY AGAIN', font_big, WHITE, 40, 300) 234 | #update high score 235 | if score > high_score: 236 | high_score = score 237 | with open('score.txt', 'w') as file: 238 | file.write(str(high_score)) 239 | key = pygame.key.get_pressed() 240 | if key[pygame.K_SPACE]: 241 | #reset variables 242 | game_over = False 243 | score = 0 244 | scroll = 0 245 | fade_counter = 0 246 | #reposition jumpy 247 | jumpy.rect.center = (SCREEN_WIDTH // 2, SCREEN_HEIGHT - 150) 248 | #reset platforms 249 | platform_group.empty() 250 | #create starting platform 251 | platform = Platform(SCREEN_WIDTH // 2 - 50, SCREEN_HEIGHT - 50, 100, False) 252 | platform_group.add(platform) 253 | 254 | 255 | #event handler 256 | for event in pygame.event.get(): 257 | if event.type == pygame.QUIT: 258 | #update high score 259 | if score > high_score: 260 | high_score = score 261 | with open('score.txt', 'w') as file: 262 | file.write(str(high_score)) 263 | run = False 264 | 265 | 266 | #update display window 267 | pygame.display.update() 268 | 269 | 270 | 271 | pygame.quit() 272 | 273 | -------------------------------------------------------------------------------- /jumpy_tut12.py: -------------------------------------------------------------------------------- 1 | #import libraries 2 | import pygame 3 | import random 4 | import os 5 | from spritesheet import SpriteSheet 6 | from enemy import Enemy 7 | 8 | #initialise pygame 9 | pygame.init() 10 | 11 | #game window dimensions 12 | SCREEN_WIDTH = 400 13 | SCREEN_HEIGHT = 600 14 | 15 | #create game window 16 | screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) 17 | pygame.display.set_caption('Jumpy') 18 | 19 | #set frame rate 20 | clock = pygame.time.Clock() 21 | FPS = 60 22 | 23 | #game variables 24 | SCROLL_THRESH = 200 25 | GRAVITY = 1 26 | MAX_PLATFORMS = 10 27 | scroll = 0 28 | bg_scroll = 0 29 | game_over = False 30 | score = 0 31 | fade_counter = 0 32 | 33 | if os.path.exists('score.txt'): 34 | with open('score.txt', 'r') as file: 35 | high_score = int(file.read()) 36 | else: 37 | high_score = 0 38 | 39 | #define colours 40 | WHITE = (255, 255, 255) 41 | BLACK = (0, 0, 0) 42 | PANEL = (153, 217, 234) 43 | 44 | #define font 45 | font_small = pygame.font.SysFont('Lucida Sans', 20) 46 | font_big = pygame.font.SysFont('Lucida Sans', 24) 47 | 48 | #load images 49 | jumpy_image = pygame.image.load('assets/jump.png').convert_alpha() 50 | bg_image = pygame.image.load('assets/bg.png').convert_alpha() 51 | platform_image = pygame.image.load('assets/wood.png').convert_alpha() 52 | #bird spritesheet 53 | bird_sheet_img = pygame.image.load('assets/bird.png').convert_alpha() 54 | bird_sheet = SpriteSheet(bird_sheet_img) 55 | 56 | 57 | #function for outputting text onto the screen 58 | def draw_text(text, font, text_col, x, y): 59 | img = font.render(text, True, text_col) 60 | screen.blit(img, (x, y)) 61 | 62 | #function for drawing info panel 63 | def draw_panel(): 64 | pygame.draw.rect(screen, PANEL, (0, 0, SCREEN_WIDTH, 30)) 65 | pygame.draw.line(screen, WHITE, (0, 30), (SCREEN_WIDTH, 30), 2) 66 | draw_text('SCORE: ' + str(score), font_small, WHITE, 0, 0) 67 | 68 | 69 | #function for drawing the background 70 | def draw_bg(bg_scroll): 71 | screen.blit(bg_image, (0, 0 + bg_scroll)) 72 | screen.blit(bg_image, (0, -600 + bg_scroll)) 73 | 74 | #player class 75 | class Player(): 76 | def __init__(self, x, y): 77 | self.image = pygame.transform.scale(jumpy_image, (45, 45)) 78 | self.width = 25 79 | self.height = 40 80 | self.rect = pygame.Rect(0, 0, self.width, self.height) 81 | self.rect.center = (x, y) 82 | self.vel_y = 0 83 | self.flip = False 84 | 85 | def move(self): 86 | #reset variables 87 | scroll = 0 88 | dx = 0 89 | dy = 0 90 | 91 | #process keypresses 92 | key = pygame.key.get_pressed() 93 | if key[pygame.K_a]: 94 | dx = -10 95 | self.flip = True 96 | if key[pygame.K_d]: 97 | dx = 10 98 | self.flip = False 99 | 100 | #gravity 101 | self.vel_y += GRAVITY 102 | dy += self.vel_y 103 | 104 | #ensure player doesn't go off the edge of the screen 105 | if self.rect.left + dx < 0: 106 | dx = -self.rect.left 107 | if self.rect.right + dx > SCREEN_WIDTH: 108 | dx = SCREEN_WIDTH - self.rect.right 109 | 110 | 111 | #check collision with platforms 112 | for platform in platform_group: 113 | #collision in the y direction 114 | if platform.rect.colliderect(self.rect.x, self.rect.y + dy, self.width, self.height): 115 | #check if above the platform 116 | if self.rect.bottom < platform.rect.centery: 117 | if self.vel_y > 0: 118 | self.rect.bottom = platform.rect.top 119 | dy = 0 120 | self.vel_y = -20 121 | 122 | #check if the player has bounced to the top of the screen 123 | if self.rect.top <= SCROLL_THRESH: 124 | #if player is jumping 125 | if self.vel_y < 0: 126 | scroll = -dy 127 | 128 | #update rectangle position 129 | self.rect.x += dx 130 | self.rect.y += dy + scroll 131 | 132 | return scroll 133 | 134 | def draw(self): 135 | screen.blit(pygame.transform.flip(self.image, self.flip, False), (self.rect.x - 12, self.rect.y - 5)) 136 | pygame.draw.rect(screen, WHITE, self.rect, 2) 137 | 138 | 139 | 140 | #platform class 141 | class Platform(pygame.sprite.Sprite): 142 | def __init__(self, x, y, width, moving): 143 | pygame.sprite.Sprite.__init__(self) 144 | self.image = pygame.transform.scale(platform_image, (width, 10)) 145 | self.moving = moving 146 | self.move_counter = random.randint(0, 50) 147 | self.direction = random.choice([-1, 1]) 148 | self.speed = random.randint(1, 2) 149 | self.rect = self.image.get_rect() 150 | self.rect.x = x 151 | self.rect.y = y 152 | 153 | def update(self, scroll): 154 | #moving platform side to side if it is a moving platform 155 | if self.moving == True: 156 | self.move_counter += 1 157 | self.rect.x += self.direction * self.speed 158 | 159 | #change platform direction if it has moved fully or hit a wall 160 | if self.move_counter >= 100 or self.rect.left < 0 or self.rect.right > SCREEN_WIDTH: 161 | self.direction *= -1 162 | self.move_counter = 0 163 | 164 | #update platform's vertical position 165 | self.rect.y += scroll 166 | 167 | #check if platform has gone off the screen 168 | if self.rect.top > SCREEN_HEIGHT: 169 | self.kill() 170 | 171 | #player instance 172 | jumpy = Player(SCREEN_WIDTH // 2, SCREEN_HEIGHT - 150) 173 | 174 | #create sprite groups 175 | platform_group = pygame.sprite.Group() 176 | enemy_group = pygame.sprite.Group() 177 | 178 | #create starting platform 179 | platform = Platform(SCREEN_WIDTH // 2 - 50, SCREEN_HEIGHT - 50, 100, False) 180 | platform_group.add(platform) 181 | 182 | #game loop 183 | run = True 184 | while run: 185 | 186 | clock.tick(FPS) 187 | 188 | if game_over == False: 189 | scroll = jumpy.move() 190 | 191 | #draw background 192 | bg_scroll += scroll 193 | if bg_scroll >= 600: 194 | bg_scroll = 0 195 | draw_bg(bg_scroll) 196 | 197 | #generate platforms 198 | if len(platform_group) < MAX_PLATFORMS: 199 | p_w = random.randint(40, 60) 200 | p_x = random.randint(0, SCREEN_WIDTH - p_w) 201 | p_y = platform.rect.y - random.randint(80, 120) 202 | p_type = random.randint(1, 2) 203 | if p_type == 1 and score > 500: 204 | p_moving = True 205 | else: 206 | p_moving = False 207 | platform = Platform(p_x, p_y, p_w, p_moving) 208 | platform_group.add(platform) 209 | 210 | #update platforms 211 | platform_group.update(scroll) 212 | 213 | #generate enemies 214 | if len(enemy_group) == 0 and score > 1500: 215 | enemy = Enemy(SCREEN_WIDTH, 100, bird_sheet, 1.5) 216 | enemy_group.add(enemy) 217 | 218 | #update enemies 219 | enemy_group.update(scroll, SCREEN_WIDTH) 220 | 221 | #update score 222 | if scroll > 0: 223 | score += scroll 224 | 225 | #draw line at previous high score 226 | pygame.draw.line(screen, WHITE, (0, score - high_score + SCROLL_THRESH), (SCREEN_WIDTH, score - high_score + SCROLL_THRESH), 3) 227 | draw_text('HIGH SCORE', font_small, WHITE, SCREEN_WIDTH - 130, score - high_score + SCROLL_THRESH) 228 | 229 | #draw sprites 230 | platform_group.draw(screen) 231 | enemy_group.draw(screen) 232 | jumpy.draw() 233 | 234 | #draw panel 235 | draw_panel() 236 | 237 | #check game over 238 | if jumpy.rect.top > SCREEN_HEIGHT: 239 | game_over = True 240 | else: 241 | if fade_counter < SCREEN_WIDTH: 242 | fade_counter += 5 243 | for y in range(0, 6, 2): 244 | pygame.draw.rect(screen, BLACK, (0, y * 100, fade_counter, 100)) 245 | pygame.draw.rect(screen, BLACK, (SCREEN_WIDTH - fade_counter, (y + 1) * 100, SCREEN_WIDTH, 100)) 246 | else: 247 | draw_text('GAME OVER!', font_big, WHITE, 130, 200) 248 | draw_text('SCORE: ' + str(score), font_big, WHITE, 130, 250) 249 | draw_text('PRESS SPACE TO PLAY AGAIN', font_big, WHITE, 40, 300) 250 | #update high score 251 | if score > high_score: 252 | high_score = score 253 | with open('score.txt', 'w') as file: 254 | file.write(str(high_score)) 255 | key = pygame.key.get_pressed() 256 | if key[pygame.K_SPACE]: 257 | #reset variables 258 | game_over = False 259 | score = 0 260 | scroll = 0 261 | fade_counter = 0 262 | #reposition jumpy 263 | jumpy.rect.center = (SCREEN_WIDTH // 2, SCREEN_HEIGHT - 150) 264 | #reset enemies 265 | enemy_group.empty() 266 | #reset platforms 267 | platform_group.empty() 268 | #create starting platform 269 | platform = Platform(SCREEN_WIDTH // 2 - 50, SCREEN_HEIGHT - 50, 100, False) 270 | platform_group.add(platform) 271 | 272 | 273 | #event handler 274 | for event in pygame.event.get(): 275 | if event.type == pygame.QUIT: 276 | #update high score 277 | if score > high_score: 278 | high_score = score 279 | with open('score.txt', 'w') as file: 280 | file.write(str(high_score)) 281 | run = False 282 | 283 | 284 | #update display window 285 | pygame.display.update() 286 | 287 | 288 | 289 | pygame.quit() 290 | 291 | -------------------------------------------------------------------------------- /jumpy_tut13.py: -------------------------------------------------------------------------------- 1 | #import libraries 2 | import pygame 3 | import random 4 | import os 5 | from spritesheet import SpriteSheet 6 | from enemy import Enemy 7 | 8 | #initialise pygame 9 | pygame.init() 10 | 11 | #game window dimensions 12 | SCREEN_WIDTH = 400 13 | SCREEN_HEIGHT = 600 14 | 15 | #create game window 16 | screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) 17 | pygame.display.set_caption('Jumpy') 18 | 19 | #set frame rate 20 | clock = pygame.time.Clock() 21 | FPS = 60 22 | 23 | #game variables 24 | SCROLL_THRESH = 200 25 | GRAVITY = 1 26 | MAX_PLATFORMS = 10 27 | scroll = 0 28 | bg_scroll = 0 29 | game_over = False 30 | score = 0 31 | fade_counter = 0 32 | 33 | if os.path.exists('score.txt'): 34 | with open('score.txt', 'r') as file: 35 | high_score = int(file.read()) 36 | else: 37 | high_score = 0 38 | 39 | #define colours 40 | WHITE = (255, 255, 255) 41 | BLACK = (0, 0, 0) 42 | PANEL = (153, 217, 234) 43 | 44 | #define font 45 | font_small = pygame.font.SysFont('Lucida Sans', 20) 46 | font_big = pygame.font.SysFont('Lucida Sans', 24) 47 | 48 | #load images 49 | jumpy_image = pygame.image.load('assets/jump.png').convert_alpha() 50 | bg_image = pygame.image.load('assets/bg.png').convert_alpha() 51 | platform_image = pygame.image.load('assets/wood.png').convert_alpha() 52 | #bird spritesheet 53 | bird_sheet_img = pygame.image.load('assets/bird.png').convert_alpha() 54 | bird_sheet = SpriteSheet(bird_sheet_img) 55 | 56 | 57 | #function for outputting text onto the screen 58 | def draw_text(text, font, text_col, x, y): 59 | img = font.render(text, True, text_col) 60 | screen.blit(img, (x, y)) 61 | 62 | #function for drawing info panel 63 | def draw_panel(): 64 | pygame.draw.rect(screen, PANEL, (0, 0, SCREEN_WIDTH, 30)) 65 | pygame.draw.line(screen, WHITE, (0, 30), (SCREEN_WIDTH, 30), 2) 66 | draw_text('SCORE: ' + str(score), font_small, WHITE, 0, 0) 67 | 68 | 69 | #function for drawing the background 70 | def draw_bg(bg_scroll): 71 | screen.blit(bg_image, (0, 0 + bg_scroll)) 72 | screen.blit(bg_image, (0, -600 + bg_scroll)) 73 | 74 | #player class 75 | class Player(): 76 | def __init__(self, x, y): 77 | self.image = pygame.transform.scale(jumpy_image, (45, 45)) 78 | self.width = 25 79 | self.height = 40 80 | self.rect = pygame.Rect(0, 0, self.width, self.height) 81 | self.rect.center = (x, y) 82 | self.vel_y = 0 83 | self.flip = False 84 | 85 | def move(self): 86 | #reset variables 87 | scroll = 0 88 | dx = 0 89 | dy = 0 90 | 91 | #process keypresses 92 | key = pygame.key.get_pressed() 93 | if key[pygame.K_a]: 94 | dx = -10 95 | self.flip = True 96 | if key[pygame.K_d]: 97 | dx = 10 98 | self.flip = False 99 | 100 | #gravity 101 | self.vel_y += GRAVITY 102 | dy += self.vel_y 103 | 104 | #ensure player doesn't go off the edge of the screen 105 | if self.rect.left + dx < 0: 106 | dx = -self.rect.left 107 | if self.rect.right + dx > SCREEN_WIDTH: 108 | dx = SCREEN_WIDTH - self.rect.right 109 | 110 | 111 | #check collision with platforms 112 | for platform in platform_group: 113 | #collision in the y direction 114 | if platform.rect.colliderect(self.rect.x, self.rect.y + dy, self.width, self.height): 115 | #check if above the platform 116 | if self.rect.bottom < platform.rect.centery: 117 | if self.vel_y > 0: 118 | self.rect.bottom = platform.rect.top 119 | dy = 0 120 | self.vel_y = -20 121 | 122 | #check if the player has bounced to the top of the screen 123 | if self.rect.top <= SCROLL_THRESH: 124 | #if player is jumping 125 | if self.vel_y < 0: 126 | scroll = -dy 127 | 128 | #update rectangle position 129 | self.rect.x += dx 130 | self.rect.y += dy + scroll 131 | 132 | #update mask 133 | self.mask = pygame.mask.from_surface(self.image) 134 | 135 | return scroll 136 | 137 | def draw(self): 138 | screen.blit(pygame.transform.flip(self.image, self.flip, False), (self.rect.x - 12, self.rect.y - 5)) 139 | 140 | #platform class 141 | class Platform(pygame.sprite.Sprite): 142 | def __init__(self, x, y, width, moving): 143 | pygame.sprite.Sprite.__init__(self) 144 | self.image = pygame.transform.scale(platform_image, (width, 10)) 145 | self.moving = moving 146 | self.move_counter = random.randint(0, 50) 147 | self.direction = random.choice([-1, 1]) 148 | self.speed = random.randint(1, 2) 149 | self.rect = self.image.get_rect() 150 | self.rect.x = x 151 | self.rect.y = y 152 | 153 | def update(self, scroll): 154 | #moving platform side to side if it is a moving platform 155 | if self.moving == True: 156 | self.move_counter += 1 157 | self.rect.x += self.direction * self.speed 158 | 159 | #change platform direction if it has moved fully or hit a wall 160 | if self.move_counter >= 100 or self.rect.left < 0 or self.rect.right > SCREEN_WIDTH: 161 | self.direction *= -1 162 | self.move_counter = 0 163 | 164 | #update platform's vertical position 165 | self.rect.y += scroll 166 | 167 | #check if platform has gone off the screen 168 | if self.rect.top > SCREEN_HEIGHT: 169 | self.kill() 170 | 171 | #player instance 172 | jumpy = Player(SCREEN_WIDTH // 2, SCREEN_HEIGHT - 150) 173 | 174 | #create sprite groups 175 | platform_group = pygame.sprite.Group() 176 | enemy_group = pygame.sprite.Group() 177 | 178 | #create starting platform 179 | platform = Platform(SCREEN_WIDTH // 2 - 50, SCREEN_HEIGHT - 50, 100, False) 180 | platform_group.add(platform) 181 | 182 | #game loop 183 | run = True 184 | while run: 185 | 186 | clock.tick(FPS) 187 | 188 | if game_over == False: 189 | scroll = jumpy.move() 190 | 191 | #draw background 192 | bg_scroll += scroll 193 | if bg_scroll >= 600: 194 | bg_scroll = 0 195 | draw_bg(bg_scroll) 196 | 197 | #generate platforms 198 | if len(platform_group) < MAX_PLATFORMS: 199 | p_w = random.randint(40, 60) 200 | p_x = random.randint(0, SCREEN_WIDTH - p_w) 201 | p_y = platform.rect.y - random.randint(80, 120) 202 | p_type = random.randint(1, 2) 203 | if p_type == 1 and score > 500: 204 | p_moving = True 205 | else: 206 | p_moving = False 207 | platform = Platform(p_x, p_y, p_w, p_moving) 208 | platform_group.add(platform) 209 | 210 | #update platforms 211 | platform_group.update(scroll) 212 | 213 | #generate enemies 214 | if len(enemy_group) == 0 and score > 1500: 215 | enemy = Enemy(SCREEN_WIDTH, 100, bird_sheet, 1.5) 216 | enemy_group.add(enemy) 217 | 218 | #update enemies 219 | enemy_group.update(scroll, SCREEN_WIDTH) 220 | 221 | #update score 222 | if scroll > 0: 223 | score += scroll 224 | 225 | #draw line at previous high score 226 | pygame.draw.line(screen, WHITE, (0, score - high_score + SCROLL_THRESH), (SCREEN_WIDTH, score - high_score + SCROLL_THRESH), 3) 227 | draw_text('HIGH SCORE', font_small, WHITE, SCREEN_WIDTH - 130, score - high_score + SCROLL_THRESH) 228 | 229 | #draw sprites 230 | platform_group.draw(screen) 231 | enemy_group.draw(screen) 232 | jumpy.draw() 233 | 234 | #draw panel 235 | draw_panel() 236 | 237 | #check game over 238 | if jumpy.rect.top > SCREEN_HEIGHT: 239 | game_over = True 240 | #check for collision with enemies 241 | if pygame.sprite.spritecollide(jumpy, enemy_group, False): 242 | if pygame.sprite.spritecollide(jumpy, enemy_group, False, pygame.sprite.collide_mask): 243 | game_over = True 244 | else: 245 | if fade_counter < SCREEN_WIDTH: 246 | fade_counter += 5 247 | for y in range(0, 6, 2): 248 | pygame.draw.rect(screen, BLACK, (0, y * 100, fade_counter, 100)) 249 | pygame.draw.rect(screen, BLACK, (SCREEN_WIDTH - fade_counter, (y + 1) * 100, SCREEN_WIDTH, 100)) 250 | else: 251 | draw_text('GAME OVER!', font_big, WHITE, 130, 200) 252 | draw_text('SCORE: ' + str(score), font_big, WHITE, 130, 250) 253 | draw_text('PRESS SPACE TO PLAY AGAIN', font_big, WHITE, 40, 300) 254 | #update high score 255 | if score > high_score: 256 | high_score = score 257 | with open('score.txt', 'w') as file: 258 | file.write(str(high_score)) 259 | key = pygame.key.get_pressed() 260 | if key[pygame.K_SPACE]: 261 | #reset variables 262 | game_over = False 263 | score = 0 264 | scroll = 0 265 | fade_counter = 0 266 | #reposition jumpy 267 | jumpy.rect.center = (SCREEN_WIDTH // 2, SCREEN_HEIGHT - 150) 268 | #reset enemies 269 | enemy_group.empty() 270 | #reset platforms 271 | platform_group.empty() 272 | #create starting platform 273 | platform = Platform(SCREEN_WIDTH // 2 - 50, SCREEN_HEIGHT - 50, 100, False) 274 | platform_group.add(platform) 275 | 276 | 277 | #event handler 278 | for event in pygame.event.get(): 279 | if event.type == pygame.QUIT: 280 | #update high score 281 | if score > high_score: 282 | high_score = score 283 | with open('score.txt', 'w') as file: 284 | file.write(str(high_score)) 285 | run = False 286 | 287 | 288 | #update display window 289 | pygame.display.update() 290 | 291 | 292 | 293 | pygame.quit() 294 | 295 | -------------------------------------------------------------------------------- /jumpy_tut14.py: -------------------------------------------------------------------------------- 1 | #import libraries 2 | import pygame 3 | import random 4 | import os 5 | from pygame import mixer 6 | from spritesheet import SpriteSheet 7 | from enemy import Enemy 8 | 9 | #initialise pygame 10 | mixer.init() 11 | pygame.init() 12 | 13 | #game window dimensions 14 | SCREEN_WIDTH = 400 15 | SCREEN_HEIGHT = 600 16 | 17 | #create game window 18 | screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) 19 | pygame.display.set_caption('Jumpy') 20 | 21 | #set frame rate 22 | clock = pygame.time.Clock() 23 | FPS = 60 24 | 25 | #load music and sounds 26 | pygame.mixer.music.load('assets/music.mp3') 27 | pygame.mixer.music.set_volume(0.6) 28 | pygame.mixer.music.play(-1, 0.0) 29 | jump_fx = pygame.mixer.Sound('assets/jump.mp3') 30 | jump_fx.set_volume(0.5) 31 | death_fx = pygame.mixer.Sound('assets/death.mp3') 32 | death_fx.set_volume(0.5) 33 | 34 | 35 | #game variables 36 | SCROLL_THRESH = 200 37 | GRAVITY = 1 38 | MAX_PLATFORMS = 10 39 | scroll = 0 40 | bg_scroll = 0 41 | game_over = False 42 | score = 0 43 | fade_counter = 0 44 | 45 | if os.path.exists('score.txt'): 46 | with open('score.txt', 'r') as file: 47 | high_score = int(file.read()) 48 | else: 49 | high_score = 0 50 | 51 | #define colours 52 | WHITE = (255, 255, 255) 53 | BLACK = (0, 0, 0) 54 | PANEL = (153, 217, 234) 55 | 56 | #define font 57 | font_small = pygame.font.SysFont('Lucida Sans', 20) 58 | font_big = pygame.font.SysFont('Lucida Sans', 24) 59 | 60 | #load images 61 | jumpy_image = pygame.image.load('assets/jump.png').convert_alpha() 62 | bg_image = pygame.image.load('assets/bg.png').convert_alpha() 63 | platform_image = pygame.image.load('assets/wood.png').convert_alpha() 64 | #bird spritesheet 65 | bird_sheet_img = pygame.image.load('assets/bird.png').convert_alpha() 66 | bird_sheet = SpriteSheet(bird_sheet_img) 67 | 68 | 69 | #function for outputting text onto the screen 70 | def draw_text(text, font, text_col, x, y): 71 | img = font.render(text, True, text_col) 72 | screen.blit(img, (x, y)) 73 | 74 | #function for drawing info panel 75 | def draw_panel(): 76 | pygame.draw.rect(screen, PANEL, (0, 0, SCREEN_WIDTH, 30)) 77 | pygame.draw.line(screen, WHITE, (0, 30), (SCREEN_WIDTH, 30), 2) 78 | draw_text('SCORE: ' + str(score), font_small, WHITE, 0, 0) 79 | 80 | 81 | #function for drawing the background 82 | def draw_bg(bg_scroll): 83 | screen.blit(bg_image, (0, 0 + bg_scroll)) 84 | screen.blit(bg_image, (0, -600 + bg_scroll)) 85 | 86 | #player class 87 | class Player(): 88 | def __init__(self, x, y): 89 | self.image = pygame.transform.scale(jumpy_image, (45, 45)) 90 | self.width = 25 91 | self.height = 40 92 | self.rect = pygame.Rect(0, 0, self.width, self.height) 93 | self.rect.center = (x, y) 94 | self.vel_y = 0 95 | self.flip = False 96 | 97 | def move(self): 98 | #reset variables 99 | scroll = 0 100 | dx = 0 101 | dy = 0 102 | 103 | #process keypresses 104 | key = pygame.key.get_pressed() 105 | if key[pygame.K_a]: 106 | dx = -10 107 | self.flip = True 108 | if key[pygame.K_d]: 109 | dx = 10 110 | self.flip = False 111 | 112 | #gravity 113 | self.vel_y += GRAVITY 114 | dy += self.vel_y 115 | 116 | #ensure player doesn't go off the edge of the screen 117 | if self.rect.left + dx < 0: 118 | dx = -self.rect.left 119 | if self.rect.right + dx > SCREEN_WIDTH: 120 | dx = SCREEN_WIDTH - self.rect.right 121 | 122 | 123 | #check collision with platforms 124 | for platform in platform_group: 125 | #collision in the y direction 126 | if platform.rect.colliderect(self.rect.x, self.rect.y + dy, self.width, self.height): 127 | #check if above the platform 128 | if self.rect.bottom < platform.rect.centery: 129 | if self.vel_y > 0: 130 | self.rect.bottom = platform.rect.top 131 | dy = 0 132 | self.vel_y = -20 133 | jump_fx.play() 134 | 135 | #check if the player has bounced to the top of the screen 136 | if self.rect.top <= SCROLL_THRESH: 137 | #if player is jumping 138 | if self.vel_y < 0: 139 | scroll = -dy 140 | 141 | #update rectangle position 142 | self.rect.x += dx 143 | self.rect.y += dy + scroll 144 | 145 | #update mask 146 | self.mask = pygame.mask.from_surface(self.image) 147 | 148 | return scroll 149 | 150 | def draw(self): 151 | screen.blit(pygame.transform.flip(self.image, self.flip, False), (self.rect.x - 12, self.rect.y - 5)) 152 | 153 | #platform class 154 | class Platform(pygame.sprite.Sprite): 155 | def __init__(self, x, y, width, moving): 156 | pygame.sprite.Sprite.__init__(self) 157 | self.image = pygame.transform.scale(platform_image, (width, 10)) 158 | self.moving = moving 159 | self.move_counter = random.randint(0, 50) 160 | self.direction = random.choice([-1, 1]) 161 | self.speed = random.randint(1, 2) 162 | self.rect = self.image.get_rect() 163 | self.rect.x = x 164 | self.rect.y = y 165 | 166 | def update(self, scroll): 167 | #moving platform side to side if it is a moving platform 168 | if self.moving == True: 169 | self.move_counter += 1 170 | self.rect.x += self.direction * self.speed 171 | 172 | #change platform direction if it has moved fully or hit a wall 173 | if self.move_counter >= 100 or self.rect.left < 0 or self.rect.right > SCREEN_WIDTH: 174 | self.direction *= -1 175 | self.move_counter = 0 176 | 177 | #update platform's vertical position 178 | self.rect.y += scroll 179 | 180 | #check if platform has gone off the screen 181 | if self.rect.top > SCREEN_HEIGHT: 182 | self.kill() 183 | 184 | #player instance 185 | jumpy = Player(SCREEN_WIDTH // 2, SCREEN_HEIGHT - 150) 186 | 187 | #create sprite groups 188 | platform_group = pygame.sprite.Group() 189 | enemy_group = pygame.sprite.Group() 190 | 191 | #create starting platform 192 | platform = Platform(SCREEN_WIDTH // 2 - 50, SCREEN_HEIGHT - 50, 100, False) 193 | platform_group.add(platform) 194 | 195 | #game loop 196 | run = True 197 | while run: 198 | 199 | clock.tick(FPS) 200 | 201 | if game_over == False: 202 | scroll = jumpy.move() 203 | 204 | #draw background 205 | bg_scroll += scroll 206 | if bg_scroll >= 600: 207 | bg_scroll = 0 208 | draw_bg(bg_scroll) 209 | 210 | #generate platforms 211 | if len(platform_group) < MAX_PLATFORMS: 212 | p_w = random.randint(40, 60) 213 | p_x = random.randint(0, SCREEN_WIDTH - p_w) 214 | p_y = platform.rect.y - random.randint(80, 120) 215 | p_type = random.randint(1, 2) 216 | if p_type == 1 and score > 500: 217 | p_moving = True 218 | else: 219 | p_moving = False 220 | platform = Platform(p_x, p_y, p_w, p_moving) 221 | platform_group.add(platform) 222 | 223 | #update platforms 224 | platform_group.update(scroll) 225 | 226 | #generate enemies 227 | if len(enemy_group) == 0 and score > 1500: 228 | enemy = Enemy(SCREEN_WIDTH, 100, bird_sheet, 1.5) 229 | enemy_group.add(enemy) 230 | 231 | #update enemies 232 | enemy_group.update(scroll, SCREEN_WIDTH) 233 | 234 | #update score 235 | if scroll > 0: 236 | score += scroll 237 | 238 | #draw line at previous high score 239 | pygame.draw.line(screen, WHITE, (0, score - high_score + SCROLL_THRESH), (SCREEN_WIDTH, score - high_score + SCROLL_THRESH), 3) 240 | draw_text('HIGH SCORE', font_small, WHITE, SCREEN_WIDTH - 130, score - high_score + SCROLL_THRESH) 241 | 242 | #draw sprites 243 | platform_group.draw(screen) 244 | enemy_group.draw(screen) 245 | jumpy.draw() 246 | 247 | #draw panel 248 | draw_panel() 249 | 250 | #check game over 251 | if jumpy.rect.top > SCREEN_HEIGHT: 252 | game_over = True 253 | death_fx.play() 254 | #check for collision with enemies 255 | if pygame.sprite.spritecollide(jumpy, enemy_group, False): 256 | if pygame.sprite.spritecollide(jumpy, enemy_group, False, pygame.sprite.collide_mask): 257 | game_over = True 258 | death_fx.play() 259 | else: 260 | if fade_counter < SCREEN_WIDTH: 261 | fade_counter += 5 262 | for y in range(0, 6, 2): 263 | pygame.draw.rect(screen, BLACK, (0, y * 100, fade_counter, 100)) 264 | pygame.draw.rect(screen, BLACK, (SCREEN_WIDTH - fade_counter, (y + 1) * 100, SCREEN_WIDTH, 100)) 265 | else: 266 | draw_text('GAME OVER!', font_big, WHITE, 130, 200) 267 | draw_text('SCORE: ' + str(score), font_big, WHITE, 130, 250) 268 | draw_text('PRESS SPACE TO PLAY AGAIN', font_big, WHITE, 40, 300) 269 | #update high score 270 | if score > high_score: 271 | high_score = score 272 | with open('score.txt', 'w') as file: 273 | file.write(str(high_score)) 274 | key = pygame.key.get_pressed() 275 | if key[pygame.K_SPACE]: 276 | #reset variables 277 | game_over = False 278 | score = 0 279 | scroll = 0 280 | fade_counter = 0 281 | #reposition jumpy 282 | jumpy.rect.center = (SCREEN_WIDTH // 2, SCREEN_HEIGHT - 150) 283 | #reset enemies 284 | enemy_group.empty() 285 | #reset platforms 286 | platform_group.empty() 287 | #create starting platform 288 | platform = Platform(SCREEN_WIDTH // 2 - 50, SCREEN_HEIGHT - 50, 100, False) 289 | platform_group.add(platform) 290 | 291 | 292 | #event handler 293 | for event in pygame.event.get(): 294 | if event.type == pygame.QUIT: 295 | #update high score 296 | if score > high_score: 297 | high_score = score 298 | with open('score.txt', 'w') as file: 299 | file.write(str(high_score)) 300 | run = False 301 | 302 | 303 | #update display window 304 | pygame.display.update() 305 | 306 | 307 | 308 | pygame.quit() 309 | 310 | -------------------------------------------------------------------------------- /jumpy_tut15.py: -------------------------------------------------------------------------------- 1 | #import libraries 2 | import pygame 3 | import random 4 | import os 5 | from pygame import mixer 6 | from spritesheet import SpriteSheet 7 | from enemy import Enemy 8 | 9 | #initialise pygame 10 | mixer.init() 11 | pygame.init() 12 | 13 | #game window dimensions 14 | SCREEN_WIDTH = 400 15 | SCREEN_HEIGHT = 600 16 | 17 | #create game window 18 | screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) 19 | pygame.display.set_caption('Jumpy') 20 | 21 | #set frame rate 22 | clock = pygame.time.Clock() 23 | FPS = 60 24 | 25 | #load music and sounds 26 | pygame.mixer.music.load('assets/music.mp3') 27 | pygame.mixer.music.set_volume(0.6) 28 | pygame.mixer.music.play(-1, 0.0) 29 | jump_fx = pygame.mixer.Sound('assets/jump.mp3') 30 | jump_fx.set_volume(0.5) 31 | death_fx = pygame.mixer.Sound('assets/death.mp3') 32 | death_fx.set_volume(0.5) 33 | 34 | 35 | #game variables 36 | SCROLL_THRESH = 200 37 | GRAVITY = 1 38 | MAX_PLATFORMS = 10 39 | scroll = 0 40 | bg_scroll = 0 41 | game_over = False 42 | score = 0 43 | fade_counter = 0 44 | 45 | if os.path.exists('score.txt'): 46 | with open('score.txt', 'r') as file: 47 | high_score = int(file.read()) 48 | else: 49 | high_score = 0 50 | 51 | #define colours 52 | WHITE = (255, 255, 255) 53 | BLACK = (0, 0, 0) 54 | PANEL = (153, 217, 234) 55 | 56 | #define font 57 | font_small = pygame.font.SysFont('Lucida Sans', 20) 58 | font_big = pygame.font.SysFont('Lucida Sans', 24) 59 | 60 | #load images 61 | jumpy_image = pygame.image.load('assets/jump.png').convert_alpha() 62 | bg_image = pygame.image.load('assets/bg.png').convert_alpha() 63 | platform_image = pygame.image.load('assets/wood.png').convert_alpha() 64 | #bird spritesheet 65 | bird_sheet_img = pygame.image.load('assets/bird.png').convert_alpha() 66 | bird_sheet = SpriteSheet(bird_sheet_img) 67 | 68 | 69 | #function for outputting text onto the screen 70 | def draw_text(text, font, text_col, x, y): 71 | img = font.render(text, True, text_col) 72 | screen.blit(img, (x, y)) 73 | 74 | #function for drawing info panel 75 | def draw_panel(): 76 | pygame.draw.rect(screen, PANEL, (0, 0, SCREEN_WIDTH, 30)) 77 | pygame.draw.line(screen, WHITE, (0, 30), (SCREEN_WIDTH, 30), 2) 78 | draw_text('SCORE: ' + str(score), font_small, WHITE, 0, 0) 79 | 80 | 81 | #function for drawing the background 82 | def draw_bg(bg_scroll): 83 | screen.blit(bg_image, (0, 0 + bg_scroll)) 84 | screen.blit(bg_image, (0, -600 + bg_scroll)) 85 | 86 | #player class 87 | class Player(): 88 | def __init__(self, x, y): 89 | self.image = pygame.transform.scale(jumpy_image, (45, 45)) 90 | self.width = 25 91 | self.height = 40 92 | self.rect = pygame.Rect(0, 0, self.width, self.height) 93 | self.rect.center = (x, y) 94 | self.vel_y = 0 95 | self.flip = False 96 | 97 | def move(self): 98 | #reset variables 99 | scroll = 0 100 | dx = 0 101 | dy = 0 102 | 103 | #process keypresses 104 | key = pygame.key.get_pressed() 105 | if key[pygame.K_a]: 106 | dx = -10 107 | self.flip = True 108 | if key[pygame.K_d]: 109 | dx = 10 110 | self.flip = False 111 | 112 | #gravity 113 | self.vel_y += GRAVITY 114 | dy += self.vel_y 115 | 116 | #ensure player doesn't go off the edge of the screen 117 | if self.rect.left + dx < 0: 118 | dx = -self.rect.left 119 | if self.rect.right + dx > SCREEN_WIDTH: 120 | dx = SCREEN_WIDTH - self.rect.right 121 | 122 | 123 | #check collision with platforms 124 | for platform in platform_group: 125 | #collision in the y direction 126 | if platform.rect.colliderect(self.rect.x, self.rect.y + dy, self.width, self.height): 127 | #check if above the platform 128 | if self.rect.bottom < platform.rect.centery: 129 | if self.vel_y > 0: 130 | self.rect.bottom = platform.rect.top 131 | dy = 0 132 | self.vel_y = -20 133 | jump_fx.play() 134 | 135 | #check if the player has bounced to the top of the screen 136 | if self.rect.top <= SCROLL_THRESH: 137 | #if player is jumping 138 | if self.vel_y < 0: 139 | scroll = -dy 140 | 141 | #update rectangle position 142 | self.rect.x += dx 143 | self.rect.y += dy + scroll 144 | 145 | #update mask 146 | self.mask = pygame.mask.from_surface(self.image) 147 | 148 | return scroll 149 | 150 | def draw(self): 151 | screen.blit(pygame.transform.flip(self.image, self.flip, False), (self.rect.x - 12, self.rect.y - 5)) 152 | 153 | #platform class 154 | class Platform(pygame.sprite.Sprite): 155 | def __init__(self, x, y, width, moving): 156 | pygame.sprite.Sprite.__init__(self) 157 | self.image = pygame.transform.scale(platform_image, (width, 10)) 158 | self.moving = moving 159 | self.move_counter = random.randint(0, 50) 160 | self.direction = random.choice([-1, 1]) 161 | self.speed = random.randint(1, 2) 162 | self.rect = self.image.get_rect() 163 | self.rect.x = x 164 | self.rect.y = y 165 | 166 | def update(self, scroll): 167 | #moving platform side to side if it is a moving platform 168 | if self.moving == True: 169 | self.move_counter += 1 170 | self.rect.x += self.direction * self.speed 171 | 172 | #change platform direction if it has moved fully or hit a wall 173 | if self.move_counter >= 100 or self.rect.left < 0 or self.rect.right > SCREEN_WIDTH: 174 | self.direction *= -1 175 | self.move_counter = 0 176 | 177 | #update platform's vertical position 178 | self.rect.y += scroll 179 | 180 | #check if platform has gone off the screen 181 | if self.rect.top > SCREEN_HEIGHT: 182 | self.kill() 183 | 184 | #player instance 185 | jumpy = Player(SCREEN_WIDTH // 2, SCREEN_HEIGHT - 150) 186 | 187 | #create sprite groups 188 | platform_group = pygame.sprite.Group() 189 | enemy_group = pygame.sprite.Group() 190 | 191 | #create starting platform 192 | platform = Platform(SCREEN_WIDTH // 2 - 50, SCREEN_HEIGHT - 50, 100, False) 193 | platform_group.add(platform) 194 | 195 | #game loop 196 | run = True 197 | while run: 198 | 199 | clock.tick(FPS) 200 | 201 | if game_over == False: 202 | scroll = jumpy.move() 203 | 204 | #draw background 205 | bg_scroll += scroll 206 | if bg_scroll >= 600: 207 | bg_scroll = 0 208 | draw_bg(bg_scroll) 209 | 210 | #generate platforms 211 | if len(platform_group) < MAX_PLATFORMS: 212 | p_w = random.randint(40, 60) 213 | p_x = random.randint(0, SCREEN_WIDTH - p_w) 214 | p_y = platform.rect.y - random.randint(80, 120) 215 | p_type = random.randint(1, 2) 216 | if p_type == 1 and score > 500: 217 | p_moving = True 218 | else: 219 | p_moving = False 220 | platform = Platform(p_x, p_y, p_w, p_moving) 221 | platform_group.add(platform) 222 | 223 | #update platforms 224 | platform_group.update(scroll) 225 | 226 | #generate enemies 227 | if len(enemy_group) == 0 and score > 1500: 228 | enemy = Enemy(SCREEN_WIDTH, 100, bird_sheet, 1.5) 229 | enemy_group.add(enemy) 230 | 231 | #update enemies 232 | enemy_group.update(scroll, SCREEN_WIDTH) 233 | 234 | #update score 235 | if scroll > 0: 236 | score += scroll 237 | 238 | #draw line at previous high score 239 | pygame.draw.line(screen, WHITE, (0, score - high_score + SCROLL_THRESH), (SCREEN_WIDTH, score - high_score + SCROLL_THRESH), 3) 240 | draw_text('HIGH SCORE', font_small, WHITE, SCREEN_WIDTH - 130, score - high_score + SCROLL_THRESH) 241 | 242 | #draw sprites 243 | platform_group.draw(screen) 244 | enemy_group.draw(screen) 245 | jumpy.draw() 246 | 247 | #draw panel 248 | draw_panel() 249 | 250 | #check game over 251 | if jumpy.rect.top > SCREEN_HEIGHT: 252 | game_over = True 253 | death_fx.play() 254 | #check for collision with enemies 255 | if pygame.sprite.spritecollide(jumpy, enemy_group, False): 256 | if pygame.sprite.spritecollide(jumpy, enemy_group, False, pygame.sprite.collide_mask): 257 | game_over = True 258 | death_fx.play() 259 | else: 260 | if fade_counter < SCREEN_WIDTH: 261 | fade_counter += 5 262 | for y in range(0, 6, 2): 263 | pygame.draw.rect(screen, BLACK, (0, y * 100, fade_counter, 100)) 264 | pygame.draw.rect(screen, BLACK, (SCREEN_WIDTH - fade_counter, (y + 1) * 100, SCREEN_WIDTH, 100)) 265 | else: 266 | draw_text('GAME OVER!', font_big, WHITE, 130, 200) 267 | draw_text('SCORE: ' + str(score), font_big, WHITE, 130, 250) 268 | draw_text('PRESS SPACE TO PLAY AGAIN', font_big, WHITE, 40, 300) 269 | #update high score 270 | if score > high_score: 271 | high_score = score 272 | with open('score.txt', 'w') as file: 273 | file.write(str(high_score)) 274 | key = pygame.key.get_pressed() 275 | if key[pygame.K_SPACE]: 276 | #reset variables 277 | game_over = False 278 | score = 0 279 | scroll = 0 280 | fade_counter = 0 281 | #reposition jumpy 282 | jumpy.rect.center = (SCREEN_WIDTH // 2, SCREEN_HEIGHT - 150) 283 | #reset enemies 284 | enemy_group.empty() 285 | #reset platforms 286 | platform_group.empty() 287 | #create starting platform 288 | platform = Platform(SCREEN_WIDTH // 2 - 50, SCREEN_HEIGHT - 50, 100, False) 289 | platform_group.add(platform) 290 | 291 | 292 | #event handler 293 | for event in pygame.event.get(): 294 | if event.type == pygame.QUIT: 295 | #update high score 296 | if score > high_score: 297 | high_score = score 298 | with open('score.txt', 'w') as file: 299 | file.write(str(high_score)) 300 | run = False 301 | 302 | 303 | #update display window 304 | pygame.display.update() 305 | 306 | 307 | 308 | pygame.quit() 309 | 310 | -------------------------------------------------------------------------------- /jumpy_tut2.py: -------------------------------------------------------------------------------- 1 | #import libraries 2 | import pygame 3 | 4 | #initialise pygame 5 | pygame.init() 6 | 7 | #game window dimensions 8 | SCREEN_WIDTH = 400 9 | SCREEN_HEIGHT = 600 10 | 11 | #create game window 12 | screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) 13 | pygame.display.set_caption('Jumpy') 14 | 15 | #define colours 16 | WHITE = (255, 255, 255) 17 | 18 | #load images 19 | jumpy_image = pygame.image.load('assets/jump.png').convert_alpha() 20 | bg_image = pygame.image.load('assets/bg.png').convert_alpha() 21 | 22 | 23 | #player class 24 | class Player(): 25 | def __init__(self, x, y): 26 | self.image = pygame.transform.scale(jumpy_image, (45, 45)) 27 | self.width = 25 28 | self.height = 40 29 | self.rect = pygame.Rect(0, 0, self.width, self.height) 30 | self.rect.center = (x, y) 31 | 32 | def draw(self): 33 | screen.blit(self.image, (self.rect.x - 12, self.rect.y - 5)) 34 | pygame.draw.rect(screen, WHITE, self.rect, 2) 35 | 36 | 37 | jumpy = Player(SCREEN_WIDTH // 2, SCREEN_HEIGHT - 150) 38 | 39 | #game loop 40 | run = True 41 | while run: 42 | 43 | #draw background 44 | screen.blit(bg_image, (0, 0)) 45 | 46 | #draw sprites 47 | jumpy.draw() 48 | 49 | 50 | #event handler 51 | for event in pygame.event.get(): 52 | if event.type == pygame.QUIT: 53 | run = False 54 | 55 | 56 | #update display window 57 | pygame.display.update() 58 | 59 | 60 | 61 | pygame.quit() 62 | 63 | -------------------------------------------------------------------------------- /jumpy_tut3.py: -------------------------------------------------------------------------------- 1 | #import libraries 2 | import pygame 3 | 4 | #initialise pygame 5 | pygame.init() 6 | 7 | #game window dimensions 8 | SCREEN_WIDTH = 400 9 | SCREEN_HEIGHT = 600 10 | 11 | #create game window 12 | screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) 13 | pygame.display.set_caption('Jumpy') 14 | 15 | #set frame rate 16 | clock = pygame.time.Clock() 17 | FPS = 60 18 | 19 | #define colours 20 | WHITE = (255, 255, 255) 21 | 22 | #load images 23 | jumpy_image = pygame.image.load('assets/jump.png').convert_alpha() 24 | bg_image = pygame.image.load('assets/bg.png').convert_alpha() 25 | 26 | 27 | #player class 28 | class Player(): 29 | def __init__(self, x, y): 30 | self.image = pygame.transform.scale(jumpy_image, (45, 45)) 31 | self.width = 25 32 | self.height = 40 33 | self.rect = pygame.Rect(0, 0, self.width, self.height) 34 | self.rect.center = (x, y) 35 | self.flip = False 36 | 37 | def move(self): 38 | #reset variables 39 | dx = 0 40 | dy = 0 41 | 42 | #process keypresses 43 | key = pygame.key.get_pressed() 44 | if key[pygame.K_a]: 45 | dx = -10 46 | self.flip = True 47 | if key[pygame.K_d]: 48 | dx = 10 49 | self.flip = False 50 | 51 | #ensure player doesn't go off the edge of the screen 52 | if self.rect.left + dx < 0: 53 | dx = -self.rect.left 54 | if self.rect.right + dx > SCREEN_WIDTH: 55 | dx = SCREEN_WIDTH - self.rect.right 56 | 57 | #update rectangle position 58 | self.rect.x += dx 59 | self.rect.y += dy 60 | 61 | def draw(self): 62 | screen.blit(pygame.transform.flip(self.image, self.flip, False), (self.rect.x - 12, self.rect.y - 5)) 63 | pygame.draw.rect(screen, WHITE, self.rect, 2) 64 | 65 | 66 | jumpy = Player(SCREEN_WIDTH // 2, SCREEN_HEIGHT - 150) 67 | 68 | #game loop 69 | run = True 70 | while run: 71 | 72 | clock.tick(FPS) 73 | 74 | jumpy.move() 75 | 76 | #draw background 77 | screen.blit(bg_image, (0, 0)) 78 | 79 | #draw sprites 80 | jumpy.draw() 81 | 82 | 83 | #event handler 84 | for event in pygame.event.get(): 85 | if event.type == pygame.QUIT: 86 | run = False 87 | 88 | 89 | #update display window 90 | pygame.display.update() 91 | 92 | 93 | 94 | pygame.quit() 95 | 96 | -------------------------------------------------------------------------------- /jumpy_tut4.py: -------------------------------------------------------------------------------- 1 | #import libraries 2 | import pygame 3 | 4 | #initialise pygame 5 | pygame.init() 6 | 7 | #game window dimensions 8 | SCREEN_WIDTH = 400 9 | SCREEN_HEIGHT = 600 10 | 11 | #create game window 12 | screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) 13 | pygame.display.set_caption('Jumpy') 14 | 15 | #set frame rate 16 | clock = pygame.time.Clock() 17 | FPS = 60 18 | 19 | #game variables 20 | GRAVITY = 1 21 | 22 | #define colours 23 | WHITE = (255, 255, 255) 24 | 25 | #load images 26 | jumpy_image = pygame.image.load('assets/jump.png').convert_alpha() 27 | bg_image = pygame.image.load('assets/bg.png').convert_alpha() 28 | 29 | 30 | #player class 31 | class Player(): 32 | def __init__(self, x, y): 33 | self.image = pygame.transform.scale(jumpy_image, (45, 45)) 34 | self.width = 25 35 | self.height = 40 36 | self.rect = pygame.Rect(0, 0, self.width, self.height) 37 | self.rect.center = (x, y) 38 | self.vel_y = 0 39 | self.flip = False 40 | 41 | def move(self): 42 | #reset variables 43 | dx = 0 44 | dy = 0 45 | 46 | #process keypresses 47 | key = pygame.key.get_pressed() 48 | if key[pygame.K_a]: 49 | dx = -10 50 | self.flip = True 51 | if key[pygame.K_d]: 52 | dx = 10 53 | self.flip = False 54 | 55 | #gravity 56 | self.vel_y += GRAVITY 57 | dy += self.vel_y 58 | 59 | #ensure player doesn't go off the edge of the screen 60 | if self.rect.left + dx < 0: 61 | dx = -self.rect.left 62 | if self.rect.right + dx > SCREEN_WIDTH: 63 | dx = SCREEN_WIDTH - self.rect.right 64 | 65 | 66 | #check collision with ground 67 | if self.rect.bottom + dy > SCREEN_HEIGHT: 68 | dy = 0 69 | self.vel_y = -20 70 | 71 | 72 | #update rectangle position 73 | self.rect.x += dx 74 | self.rect.y += dy 75 | 76 | def draw(self): 77 | screen.blit(pygame.transform.flip(self.image, self.flip, False), (self.rect.x - 12, self.rect.y - 5)) 78 | pygame.draw.rect(screen, WHITE, self.rect, 2) 79 | 80 | 81 | jumpy = Player(SCREEN_WIDTH // 2, SCREEN_HEIGHT - 150) 82 | 83 | #game loop 84 | run = True 85 | while run: 86 | 87 | clock.tick(FPS) 88 | 89 | jumpy.move() 90 | 91 | #draw background 92 | screen.blit(bg_image, (0, 0)) 93 | 94 | #draw sprites 95 | jumpy.draw() 96 | 97 | 98 | #event handler 99 | for event in pygame.event.get(): 100 | if event.type == pygame.QUIT: 101 | run = False 102 | 103 | 104 | #update display window 105 | pygame.display.update() 106 | 107 | 108 | 109 | pygame.quit() 110 | 111 | -------------------------------------------------------------------------------- /jumpy_tut5.py: -------------------------------------------------------------------------------- 1 | #import libraries 2 | import pygame 3 | import random 4 | 5 | #initialise pygame 6 | pygame.init() 7 | 8 | #game window dimensions 9 | SCREEN_WIDTH = 400 10 | SCREEN_HEIGHT = 600 11 | 12 | #create game window 13 | screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) 14 | pygame.display.set_caption('Jumpy') 15 | 16 | #set frame rate 17 | clock = pygame.time.Clock() 18 | FPS = 60 19 | 20 | #game variables 21 | GRAVITY = 1 22 | MAX_PLATFORMS = 10 23 | 24 | #define colours 25 | WHITE = (255, 255, 255) 26 | 27 | #load images 28 | jumpy_image = pygame.image.load('assets/jump.png').convert_alpha() 29 | bg_image = pygame.image.load('assets/bg.png').convert_alpha() 30 | platform_image = pygame.image.load('assets/wood.png').convert_alpha() 31 | 32 | 33 | #player class 34 | class Player(): 35 | def __init__(self, x, y): 36 | self.image = pygame.transform.scale(jumpy_image, (45, 45)) 37 | self.width = 25 38 | self.height = 40 39 | self.rect = pygame.Rect(0, 0, self.width, self.height) 40 | self.rect.center = (x, y) 41 | self.vel_y = 0 42 | self.flip = False 43 | 44 | def move(self): 45 | #reset variables 46 | dx = 0 47 | dy = 0 48 | 49 | #process keypresses 50 | key = pygame.key.get_pressed() 51 | if key[pygame.K_a]: 52 | dx = -10 53 | self.flip = True 54 | if key[pygame.K_d]: 55 | dx = 10 56 | self.flip = False 57 | 58 | #gravity 59 | self.vel_y += GRAVITY 60 | dy += self.vel_y 61 | 62 | #ensure player doesn't go off the edge of the screen 63 | if self.rect.left + dx < 0: 64 | dx = -self.rect.left 65 | if self.rect.right + dx > SCREEN_WIDTH: 66 | dx = SCREEN_WIDTH - self.rect.right 67 | 68 | 69 | #check collision with platforms 70 | for platform in platform_group: 71 | #collision in the y direction 72 | if platform.rect.colliderect(self.rect.x, self.rect.y + dy, self.width, self.height): 73 | #check if above the platform 74 | if self.rect.bottom < platform.rect.centery: 75 | if self.vel_y > 0: 76 | self.rect.bottom = platform.rect.top 77 | dy = 0 78 | self.vel_y = -20 79 | 80 | 81 | #check collision with ground 82 | if self.rect.bottom + dy > SCREEN_HEIGHT: 83 | dy = 0 84 | self.vel_y = -20 85 | 86 | 87 | #update rectangle position 88 | self.rect.x += dx 89 | self.rect.y += dy 90 | 91 | def draw(self): 92 | screen.blit(pygame.transform.flip(self.image, self.flip, False), (self.rect.x - 12, self.rect.y - 5)) 93 | pygame.draw.rect(screen, WHITE, self.rect, 2) 94 | 95 | 96 | 97 | #platform class 98 | class Platform(pygame.sprite.Sprite): 99 | def __init__(self, x, y, width): 100 | pygame.sprite.Sprite.__init__(self) 101 | self.image = pygame.transform.scale(platform_image, (width, 10)) 102 | self.rect = self.image.get_rect() 103 | self.rect.x = x 104 | self.rect.y = y 105 | 106 | 107 | 108 | #player instance 109 | jumpy = Player(SCREEN_WIDTH // 2, SCREEN_HEIGHT - 150) 110 | 111 | #create sprite groups 112 | platform_group = pygame.sprite.Group() 113 | 114 | #create temporary platforms 115 | for p in range(MAX_PLATFORMS): 116 | p_w = random.randint(40, 60) 117 | p_x = random.randint(0, SCREEN_WIDTH - p_w) 118 | p_y = p * random.randint(80, 120) 119 | platform = Platform(p_x, p_y, p_w) 120 | platform_group.add(platform) 121 | 122 | 123 | #game loop 124 | run = True 125 | while run: 126 | 127 | clock.tick(FPS) 128 | 129 | jumpy.move() 130 | 131 | #draw background 132 | screen.blit(bg_image, (0, 0)) 133 | 134 | #draw sprites 135 | platform_group.draw(screen) 136 | jumpy.draw() 137 | 138 | 139 | #event handler 140 | for event in pygame.event.get(): 141 | if event.type == pygame.QUIT: 142 | run = False 143 | 144 | 145 | #update display window 146 | pygame.display.update() 147 | 148 | 149 | 150 | pygame.quit() 151 | 152 | -------------------------------------------------------------------------------- /jumpy_tut6.py: -------------------------------------------------------------------------------- 1 | #import libraries 2 | import pygame 3 | import random 4 | 5 | #initialise pygame 6 | pygame.init() 7 | 8 | #game window dimensions 9 | SCREEN_WIDTH = 400 10 | SCREEN_HEIGHT = 600 11 | 12 | #create game window 13 | screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) 14 | pygame.display.set_caption('Jumpy') 15 | 16 | #set frame rate 17 | clock = pygame.time.Clock() 18 | FPS = 60 19 | 20 | #game variables 21 | SCROLL_THRESH = 200 22 | GRAVITY = 1 23 | MAX_PLATFORMS = 20 24 | scroll = 0 25 | bg_scroll = 0 26 | 27 | #define colours 28 | WHITE = (255, 255, 255) 29 | 30 | #load images 31 | jumpy_image = pygame.image.load('assets/jump.png').convert_alpha() 32 | bg_image = pygame.image.load('assets/bg.png').convert_alpha() 33 | platform_image = pygame.image.load('assets/wood.png').convert_alpha() 34 | 35 | #function for drawing the background 36 | def draw_bg(bg_scroll): 37 | screen.blit(bg_image, (0, 0 + bg_scroll)) 38 | screen.blit(bg_image, (0, -600 + bg_scroll)) 39 | 40 | #player class 41 | class Player(): 42 | def __init__(self, x, y): 43 | self.image = pygame.transform.scale(jumpy_image, (45, 45)) 44 | self.width = 25 45 | self.height = 40 46 | self.rect = pygame.Rect(0, 0, self.width, self.height) 47 | self.rect.center = (x, y) 48 | self.vel_y = 0 49 | self.flip = False 50 | 51 | def move(self): 52 | #reset variables 53 | scroll = 0 54 | dx = 0 55 | dy = 0 56 | 57 | #process keypresses 58 | key = pygame.key.get_pressed() 59 | if key[pygame.K_a]: 60 | dx = -10 61 | self.flip = True 62 | if key[pygame.K_d]: 63 | dx = 10 64 | self.flip = False 65 | 66 | #gravity 67 | self.vel_y += GRAVITY 68 | dy += self.vel_y 69 | 70 | #ensure player doesn't go off the edge of the screen 71 | if self.rect.left + dx < 0: 72 | dx = -self.rect.left 73 | if self.rect.right + dx > SCREEN_WIDTH: 74 | dx = SCREEN_WIDTH - self.rect.right 75 | 76 | 77 | #check collision with platforms 78 | for platform in platform_group: 79 | #collision in the y direction 80 | if platform.rect.colliderect(self.rect.x, self.rect.y + dy, self.width, self.height): 81 | #check if above the platform 82 | if self.rect.bottom < platform.rect.centery: 83 | if self.vel_y > 0: 84 | self.rect.bottom = platform.rect.top 85 | dy = 0 86 | self.vel_y = -20 87 | 88 | 89 | #check collision with ground 90 | if self.rect.bottom + dy > SCREEN_HEIGHT: 91 | dy = 0 92 | self.vel_y = -20 93 | 94 | 95 | #check if the player has bounced to the top of the screen 96 | if self.rect.top <= SCROLL_THRESH: 97 | #if player is jumping 98 | if self.vel_y < 0: 99 | scroll = -dy 100 | 101 | #update rectangle position 102 | self.rect.x += dx 103 | self.rect.y += dy + scroll 104 | 105 | return scroll 106 | 107 | def draw(self): 108 | screen.blit(pygame.transform.flip(self.image, self.flip, False), (self.rect.x - 12, self.rect.y - 5)) 109 | pygame.draw.rect(screen, WHITE, self.rect, 2) 110 | 111 | 112 | 113 | #platform class 114 | class Platform(pygame.sprite.Sprite): 115 | def __init__(self, x, y, width): 116 | pygame.sprite.Sprite.__init__(self) 117 | self.image = pygame.transform.scale(platform_image, (width, 10)) 118 | self.rect = self.image.get_rect() 119 | self.rect.x = x 120 | self.rect.y = y 121 | 122 | def update(self, scroll): 123 | 124 | #update platform's vertical position 125 | self.rect.y += scroll 126 | 127 | 128 | 129 | #player instance 130 | jumpy = Player(SCREEN_WIDTH // 2, SCREEN_HEIGHT - 150) 131 | 132 | #create sprite groups 133 | platform_group = pygame.sprite.Group() 134 | 135 | #create temporary platforms 136 | for p in range(MAX_PLATFORMS): 137 | p_w = random.randint(40, 60) 138 | p_x = random.randint(0, SCREEN_WIDTH - p_w) 139 | p_y = p * random.randint(80, 120) 140 | platform = Platform(p_x, p_y, p_w) 141 | platform_group.add(platform) 142 | 143 | 144 | #game loop 145 | run = True 146 | while run: 147 | 148 | clock.tick(FPS) 149 | 150 | scroll = jumpy.move() 151 | 152 | #draw background 153 | bg_scroll += scroll 154 | if bg_scroll >= 600: 155 | bg_scroll = 0 156 | draw_bg(bg_scroll) 157 | 158 | #draw temporary scroll threshold 159 | pygame.draw.line(screen, WHITE, (0, SCROLL_THRESH), (SCREEN_WIDTH, SCROLL_THRESH)) 160 | 161 | #update platforms 162 | platform_group.update(scroll) 163 | 164 | #draw sprites 165 | platform_group.draw(screen) 166 | jumpy.draw() 167 | 168 | 169 | #event handler 170 | for event in pygame.event.get(): 171 | if event.type == pygame.QUIT: 172 | run = False 173 | 174 | 175 | #update display window 176 | pygame.display.update() 177 | 178 | 179 | 180 | pygame.quit() 181 | 182 | -------------------------------------------------------------------------------- /jumpy_tut7.py: -------------------------------------------------------------------------------- 1 | #import libraries 2 | import pygame 3 | import random 4 | 5 | #initialise pygame 6 | pygame.init() 7 | 8 | #game window dimensions 9 | SCREEN_WIDTH = 400 10 | SCREEN_HEIGHT = 600 11 | 12 | #create game window 13 | screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) 14 | pygame.display.set_caption('Jumpy') 15 | 16 | #set frame rate 17 | clock = pygame.time.Clock() 18 | FPS = 60 19 | 20 | #game variables 21 | SCROLL_THRESH = 200 22 | GRAVITY = 1 23 | MAX_PLATFORMS = 10 24 | scroll = 0 25 | bg_scroll = 0 26 | 27 | #define colours 28 | WHITE = (255, 255, 255) 29 | 30 | #load images 31 | jumpy_image = pygame.image.load('assets/jump.png').convert_alpha() 32 | bg_image = pygame.image.load('assets/bg.png').convert_alpha() 33 | platform_image = pygame.image.load('assets/wood.png').convert_alpha() 34 | 35 | #function for drawing the background 36 | def draw_bg(bg_scroll): 37 | screen.blit(bg_image, (0, 0 + bg_scroll)) 38 | screen.blit(bg_image, (0, -600 + bg_scroll)) 39 | 40 | #player class 41 | class Player(): 42 | def __init__(self, x, y): 43 | self.image = pygame.transform.scale(jumpy_image, (45, 45)) 44 | self.width = 25 45 | self.height = 40 46 | self.rect = pygame.Rect(0, 0, self.width, self.height) 47 | self.rect.center = (x, y) 48 | self.vel_y = 0 49 | self.flip = False 50 | 51 | def move(self): 52 | #reset variables 53 | scroll = 0 54 | dx = 0 55 | dy = 0 56 | 57 | #process keypresses 58 | key = pygame.key.get_pressed() 59 | if key[pygame.K_a]: 60 | dx = -10 61 | self.flip = True 62 | if key[pygame.K_d]: 63 | dx = 10 64 | self.flip = False 65 | 66 | #gravity 67 | self.vel_y += GRAVITY 68 | dy += self.vel_y 69 | 70 | #ensure player doesn't go off the edge of the screen 71 | if self.rect.left + dx < 0: 72 | dx = -self.rect.left 73 | if self.rect.right + dx > SCREEN_WIDTH: 74 | dx = SCREEN_WIDTH - self.rect.right 75 | 76 | 77 | #check collision with platforms 78 | for platform in platform_group: 79 | #collision in the y direction 80 | if platform.rect.colliderect(self.rect.x, self.rect.y + dy, self.width, self.height): 81 | #check if above the platform 82 | if self.rect.bottom < platform.rect.centery: 83 | if self.vel_y > 0: 84 | self.rect.bottom = platform.rect.top 85 | dy = 0 86 | self.vel_y = -20 87 | 88 | 89 | #check collision with ground 90 | if self.rect.bottom + dy > SCREEN_HEIGHT: 91 | dy = 0 92 | self.vel_y = -20 93 | 94 | 95 | #check if the player has bounced to the top of the screen 96 | if self.rect.top <= SCROLL_THRESH: 97 | #if player is jumping 98 | if self.vel_y < 0: 99 | scroll = -dy 100 | 101 | #update rectangle position 102 | self.rect.x += dx 103 | self.rect.y += dy + scroll 104 | 105 | return scroll 106 | 107 | def draw(self): 108 | screen.blit(pygame.transform.flip(self.image, self.flip, False), (self.rect.x - 12, self.rect.y - 5)) 109 | pygame.draw.rect(screen, WHITE, self.rect, 2) 110 | 111 | 112 | 113 | #platform class 114 | class Platform(pygame.sprite.Sprite): 115 | def __init__(self, x, y, width): 116 | pygame.sprite.Sprite.__init__(self) 117 | self.image = pygame.transform.scale(platform_image, (width, 10)) 118 | self.rect = self.image.get_rect() 119 | self.rect.x = x 120 | self.rect.y = y 121 | 122 | def update(self, scroll): 123 | 124 | #update platform's vertical position 125 | self.rect.y += scroll 126 | 127 | #check if platform has gone off the screen 128 | if self.rect.top > SCREEN_HEIGHT: 129 | self.kill() 130 | 131 | #player instance 132 | jumpy = Player(SCREEN_WIDTH // 2, SCREEN_HEIGHT - 150) 133 | 134 | #create sprite groups 135 | platform_group = pygame.sprite.Group() 136 | 137 | #create starting platform 138 | platform = Platform(SCREEN_WIDTH // 2 - 50, SCREEN_HEIGHT - 50, 100) 139 | platform_group.add(platform) 140 | 141 | #game loop 142 | run = True 143 | while run: 144 | 145 | clock.tick(FPS) 146 | 147 | scroll = jumpy.move() 148 | 149 | #draw background 150 | bg_scroll += scroll 151 | if bg_scroll >= 600: 152 | bg_scroll = 0 153 | draw_bg(bg_scroll) 154 | 155 | #generate platforms 156 | if len(platform_group) < MAX_PLATFORMS: 157 | p_w = random.randint(40, 60) 158 | p_x = random.randint(0, SCREEN_WIDTH - p_w) 159 | p_y = platform.rect.y - random.randint(80, 120) 160 | platform = Platform(p_x, p_y, p_w) 161 | platform_group.add(platform) 162 | 163 | #update platforms 164 | platform_group.update(scroll) 165 | 166 | #draw sprites 167 | platform_group.draw(screen) 168 | jumpy.draw() 169 | 170 | 171 | #event handler 172 | for event in pygame.event.get(): 173 | if event.type == pygame.QUIT: 174 | run = False 175 | 176 | 177 | #update display window 178 | pygame.display.update() 179 | 180 | 181 | 182 | pygame.quit() 183 | 184 | -------------------------------------------------------------------------------- /jumpy_tut8.py: -------------------------------------------------------------------------------- 1 | #import libraries 2 | import pygame 3 | import random 4 | 5 | #initialise pygame 6 | pygame.init() 7 | 8 | #game window dimensions 9 | SCREEN_WIDTH = 400 10 | SCREEN_HEIGHT = 600 11 | 12 | #create game window 13 | screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) 14 | pygame.display.set_caption('Jumpy') 15 | 16 | #set frame rate 17 | clock = pygame.time.Clock() 18 | FPS = 60 19 | 20 | #game variables 21 | SCROLL_THRESH = 200 22 | GRAVITY = 1 23 | MAX_PLATFORMS = 10 24 | scroll = 0 25 | bg_scroll = 0 26 | game_over = False 27 | score = 0 28 | 29 | #define colours 30 | WHITE = (255, 255, 255) 31 | 32 | #define font 33 | font_small = pygame.font.SysFont('Lucida Sans', 20) 34 | font_big = pygame.font.SysFont('Lucida Sans', 24) 35 | 36 | #load images 37 | jumpy_image = pygame.image.load('assets/jump.png').convert_alpha() 38 | bg_image = pygame.image.load('assets/bg.png').convert_alpha() 39 | platform_image = pygame.image.load('assets/wood.png').convert_alpha() 40 | 41 | #function for outputting text onto the screen 42 | def draw_text(text, font, text_col, x, y): 43 | img = font.render(text, True, text_col) 44 | screen.blit(img, (x, y)) 45 | 46 | #function for drawing the background 47 | def draw_bg(bg_scroll): 48 | screen.blit(bg_image, (0, 0 + bg_scroll)) 49 | screen.blit(bg_image, (0, -600 + bg_scroll)) 50 | 51 | #player class 52 | class Player(): 53 | def __init__(self, x, y): 54 | self.image = pygame.transform.scale(jumpy_image, (45, 45)) 55 | self.width = 25 56 | self.height = 40 57 | self.rect = pygame.Rect(0, 0, self.width, self.height) 58 | self.rect.center = (x, y) 59 | self.vel_y = 0 60 | self.flip = False 61 | 62 | def move(self): 63 | #reset variables 64 | scroll = 0 65 | dx = 0 66 | dy = 0 67 | 68 | #process keypresses 69 | key = pygame.key.get_pressed() 70 | if key[pygame.K_a]: 71 | dx = -10 72 | self.flip = True 73 | if key[pygame.K_d]: 74 | dx = 10 75 | self.flip = False 76 | 77 | #gravity 78 | self.vel_y += GRAVITY 79 | dy += self.vel_y 80 | 81 | #ensure player doesn't go off the edge of the screen 82 | if self.rect.left + dx < 0: 83 | dx = -self.rect.left 84 | if self.rect.right + dx > SCREEN_WIDTH: 85 | dx = SCREEN_WIDTH - self.rect.right 86 | 87 | 88 | #check collision with platforms 89 | for platform in platform_group: 90 | #collision in the y direction 91 | if platform.rect.colliderect(self.rect.x, self.rect.y + dy, self.width, self.height): 92 | #check if above the platform 93 | if self.rect.bottom < platform.rect.centery: 94 | if self.vel_y > 0: 95 | self.rect.bottom = platform.rect.top 96 | dy = 0 97 | self.vel_y = -20 98 | 99 | #check if the player has bounced to the top of the screen 100 | if self.rect.top <= SCROLL_THRESH: 101 | #if player is jumping 102 | if self.vel_y < 0: 103 | scroll = -dy 104 | 105 | #update rectangle position 106 | self.rect.x += dx 107 | self.rect.y += dy + scroll 108 | 109 | return scroll 110 | 111 | def draw(self): 112 | screen.blit(pygame.transform.flip(self.image, self.flip, False), (self.rect.x - 12, self.rect.y - 5)) 113 | pygame.draw.rect(screen, WHITE, self.rect, 2) 114 | 115 | 116 | 117 | #platform class 118 | class Platform(pygame.sprite.Sprite): 119 | def __init__(self, x, y, width): 120 | pygame.sprite.Sprite.__init__(self) 121 | self.image = pygame.transform.scale(platform_image, (width, 10)) 122 | self.rect = self.image.get_rect() 123 | self.rect.x = x 124 | self.rect.y = y 125 | 126 | def update(self, scroll): 127 | 128 | #update platform's vertical position 129 | self.rect.y += scroll 130 | 131 | #check if platform has gone off the screen 132 | if self.rect.top > SCREEN_HEIGHT: 133 | self.kill() 134 | 135 | #player instance 136 | jumpy = Player(SCREEN_WIDTH // 2, SCREEN_HEIGHT - 150) 137 | 138 | #create sprite groups 139 | platform_group = pygame.sprite.Group() 140 | 141 | #create starting platform 142 | platform = Platform(SCREEN_WIDTH // 2 - 50, SCREEN_HEIGHT - 50, 100) 143 | platform_group.add(platform) 144 | 145 | #game loop 146 | run = True 147 | while run: 148 | 149 | clock.tick(FPS) 150 | 151 | if game_over == False: 152 | scroll = jumpy.move() 153 | 154 | #draw background 155 | bg_scroll += scroll 156 | if bg_scroll >= 600: 157 | bg_scroll = 0 158 | draw_bg(bg_scroll) 159 | 160 | #generate platforms 161 | if len(platform_group) < MAX_PLATFORMS: 162 | p_w = random.randint(40, 60) 163 | p_x = random.randint(0, SCREEN_WIDTH - p_w) 164 | p_y = platform.rect.y - random.randint(80, 120) 165 | platform = Platform(p_x, p_y, p_w) 166 | platform_group.add(platform) 167 | 168 | #update platforms 169 | platform_group.update(scroll) 170 | 171 | #draw sprites 172 | platform_group.draw(screen) 173 | jumpy.draw() 174 | 175 | #check game over 176 | if jumpy.rect.top > SCREEN_HEIGHT: 177 | game_over = True 178 | else: 179 | draw_text('GAME OVER!', font_big, WHITE, 130, 200) 180 | draw_text('SCORE: ' + str(score), font_big, WHITE, 130, 250) 181 | draw_text('PRESS SPACE TO PLAY AGAIN', font_big, WHITE, 40, 300) 182 | key = pygame.key.get_pressed() 183 | if key[pygame.K_SPACE]: 184 | #reset variables 185 | game_over = False 186 | score = 0 187 | scroll = 0 188 | #reposition jumpy 189 | jumpy.rect.center = (SCREEN_WIDTH // 2, SCREEN_HEIGHT - 150) 190 | #reset platforms 191 | platform_group.empty() 192 | #create starting platform 193 | platform = Platform(SCREEN_WIDTH // 2 - 50, SCREEN_HEIGHT - 50, 100) 194 | platform_group.add(platform) 195 | 196 | 197 | #event handler 198 | for event in pygame.event.get(): 199 | if event.type == pygame.QUIT: 200 | run = False 201 | 202 | 203 | #update display window 204 | pygame.display.update() 205 | 206 | 207 | 208 | pygame.quit() 209 | 210 | -------------------------------------------------------------------------------- /jumpy_tut9.py: -------------------------------------------------------------------------------- 1 | #import libraries 2 | import pygame 3 | import random 4 | 5 | #initialise pygame 6 | pygame.init() 7 | 8 | #game window dimensions 9 | SCREEN_WIDTH = 400 10 | SCREEN_HEIGHT = 600 11 | 12 | #create game window 13 | screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) 14 | pygame.display.set_caption('Jumpy') 15 | 16 | #set frame rate 17 | clock = pygame.time.Clock() 18 | FPS = 60 19 | 20 | #game variables 21 | SCROLL_THRESH = 200 22 | GRAVITY = 1 23 | MAX_PLATFORMS = 10 24 | scroll = 0 25 | bg_scroll = 0 26 | game_over = False 27 | score = 0 28 | fade_counter = 0 29 | 30 | #define colours 31 | WHITE = (255, 255, 255) 32 | BLACK = (0, 0, 0) 33 | 34 | #define font 35 | font_small = pygame.font.SysFont('Lucida Sans', 20) 36 | font_big = pygame.font.SysFont('Lucida Sans', 24) 37 | 38 | #load images 39 | jumpy_image = pygame.image.load('assets/jump.png').convert_alpha() 40 | bg_image = pygame.image.load('assets/bg.png').convert_alpha() 41 | platform_image = pygame.image.load('assets/wood.png').convert_alpha() 42 | 43 | #function for outputting text onto the screen 44 | def draw_text(text, font, text_col, x, y): 45 | img = font.render(text, True, text_col) 46 | screen.blit(img, (x, y)) 47 | 48 | #function for drawing the background 49 | def draw_bg(bg_scroll): 50 | screen.blit(bg_image, (0, 0 + bg_scroll)) 51 | screen.blit(bg_image, (0, -600 + bg_scroll)) 52 | 53 | #player class 54 | class Player(): 55 | def __init__(self, x, y): 56 | self.image = pygame.transform.scale(jumpy_image, (45, 45)) 57 | self.width = 25 58 | self.height = 40 59 | self.rect = pygame.Rect(0, 0, self.width, self.height) 60 | self.rect.center = (x, y) 61 | self.vel_y = 0 62 | self.flip = False 63 | 64 | def move(self): 65 | #reset variables 66 | scroll = 0 67 | dx = 0 68 | dy = 0 69 | 70 | #process keypresses 71 | key = pygame.key.get_pressed() 72 | if key[pygame.K_a]: 73 | dx = -10 74 | self.flip = True 75 | if key[pygame.K_d]: 76 | dx = 10 77 | self.flip = False 78 | 79 | #gravity 80 | self.vel_y += GRAVITY 81 | dy += self.vel_y 82 | 83 | #ensure player doesn't go off the edge of the screen 84 | if self.rect.left + dx < 0: 85 | dx = -self.rect.left 86 | if self.rect.right + dx > SCREEN_WIDTH: 87 | dx = SCREEN_WIDTH - self.rect.right 88 | 89 | 90 | #check collision with platforms 91 | for platform in platform_group: 92 | #collision in the y direction 93 | if platform.rect.colliderect(self.rect.x, self.rect.y + dy, self.width, self.height): 94 | #check if above the platform 95 | if self.rect.bottom < platform.rect.centery: 96 | if self.vel_y > 0: 97 | self.rect.bottom = platform.rect.top 98 | dy = 0 99 | self.vel_y = -20 100 | 101 | #check if the player has bounced to the top of the screen 102 | if self.rect.top <= SCROLL_THRESH: 103 | #if player is jumping 104 | if self.vel_y < 0: 105 | scroll = -dy 106 | 107 | #update rectangle position 108 | self.rect.x += dx 109 | self.rect.y += dy + scroll 110 | 111 | return scroll 112 | 113 | def draw(self): 114 | screen.blit(pygame.transform.flip(self.image, self.flip, False), (self.rect.x - 12, self.rect.y - 5)) 115 | pygame.draw.rect(screen, WHITE, self.rect, 2) 116 | 117 | 118 | 119 | #platform class 120 | class Platform(pygame.sprite.Sprite): 121 | def __init__(self, x, y, width): 122 | pygame.sprite.Sprite.__init__(self) 123 | self.image = pygame.transform.scale(platform_image, (width, 10)) 124 | self.rect = self.image.get_rect() 125 | self.rect.x = x 126 | self.rect.y = y 127 | 128 | def update(self, scroll): 129 | 130 | #update platform's vertical position 131 | self.rect.y += scroll 132 | 133 | #check if platform has gone off the screen 134 | if self.rect.top > SCREEN_HEIGHT: 135 | self.kill() 136 | 137 | #player instance 138 | jumpy = Player(SCREEN_WIDTH // 2, SCREEN_HEIGHT - 150) 139 | 140 | #create sprite groups 141 | platform_group = pygame.sprite.Group() 142 | 143 | #create starting platform 144 | platform = Platform(SCREEN_WIDTH // 2 - 50, SCREEN_HEIGHT - 50, 100) 145 | platform_group.add(platform) 146 | 147 | #game loop 148 | run = True 149 | while run: 150 | 151 | clock.tick(FPS) 152 | 153 | if game_over == False: 154 | scroll = jumpy.move() 155 | 156 | #draw background 157 | bg_scroll += scroll 158 | if bg_scroll >= 600: 159 | bg_scroll = 0 160 | draw_bg(bg_scroll) 161 | 162 | #generate platforms 163 | if len(platform_group) < MAX_PLATFORMS: 164 | p_w = random.randint(40, 60) 165 | p_x = random.randint(0, SCREEN_WIDTH - p_w) 166 | p_y = platform.rect.y - random.randint(80, 120) 167 | platform = Platform(p_x, p_y, p_w) 168 | platform_group.add(platform) 169 | 170 | #update platforms 171 | platform_group.update(scroll) 172 | 173 | #draw sprites 174 | platform_group.draw(screen) 175 | jumpy.draw() 176 | 177 | #check game over 178 | if jumpy.rect.top > SCREEN_HEIGHT: 179 | game_over = True 180 | else: 181 | if fade_counter < SCREEN_WIDTH: 182 | fade_counter += 5 183 | for y in range(0, 6, 2): 184 | pygame.draw.rect(screen, BLACK, (0, y * 100, fade_counter, 100)) 185 | pygame.draw.rect(screen, BLACK, (SCREEN_WIDTH - fade_counter, (y + 1) * 100, SCREEN_WIDTH, 100)) 186 | draw_text('GAME OVER!', font_big, WHITE, 130, 200) 187 | draw_text('SCORE: ' + str(score), font_big, WHITE, 130, 250) 188 | draw_text('PRESS SPACE TO PLAY AGAIN', font_big, WHITE, 40, 300) 189 | key = pygame.key.get_pressed() 190 | if key[pygame.K_SPACE]: 191 | #reset variables 192 | game_over = False 193 | score = 0 194 | scroll = 0 195 | fade_counter = 0 196 | #reposition jumpy 197 | jumpy.rect.center = (SCREEN_WIDTH // 2, SCREEN_HEIGHT - 150) 198 | #reset platforms 199 | platform_group.empty() 200 | #create starting platform 201 | platform = Platform(SCREEN_WIDTH // 2 - 50, SCREEN_HEIGHT - 50, 100) 202 | platform_group.add(platform) 203 | 204 | 205 | #event handler 206 | for event in pygame.event.get(): 207 | if event.type == pygame.QUIT: 208 | run = False 209 | 210 | 211 | #update display window 212 | pygame.display.update() 213 | 214 | 215 | 216 | pygame.quit() 217 | 218 | -------------------------------------------------------------------------------- /spritesheet.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | 3 | class SpriteSheet(): 4 | def __init__(self, image): 5 | self.sheet = image 6 | 7 | def get_image(self, frame, width, height, scale, colour): 8 | image = pygame.Surface((width, height)).convert_alpha() 9 | image.blit(self.sheet, (0, 0), ((frame * width), 0, width, height)) 10 | image = pygame.transform.scale(image, (int(width * scale), int(height * scale))) 11 | image.set_colorkey(colour) 12 | 13 | return image --------------------------------------------------------------------------------