1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
| import random from sprites import *
class Game: def __init__(self): pg.init() self.screen = pg.display.set_mode([WIDTH, HEIGHT]) pg.display.set_caption(TITLE) self.clock = pg.time.Clock() self.running = True
def new(self): self.score = 0 self.all_sprites = pg.sprite.Group() self.platforms = pg.sprite.Group() self.player = Player(self) self.all_sprites.add(self.player) self.ground = Platform(0, HEIGHT - 20, WIDTH, 20) self.all_sprites.add(self.ground) self.platforms.add(self.ground) for plat in PLATFORM_LIST: p = Platform(*plat) self.all_sprites.add(p) self.platforms.add(p) self.run()
def run(self): self.playing = True while self.playing: self.clock.tick(FPS) self.events() self.update() self.draw()
def update(self): self.all_sprites.update() if self.player.vel.y > 0: hits = pg.sprite.spritecollide(self.player, self.platforms, False) if hits: lowest = hits[0] for hit in hits: if hit.rect.bottom > lowest.rect.centery: lowest = hit if self.player.pos.y < lowest.rect.bottom: self.player.pos.y = lowest.rect.top self.player.vel.y = 0 self.player.jumping = False
if self.player.rect.top <= HEIGHT * 0.4: self.player.pos.y += abs(int(self.player.vel.y)) for plat in self.platforms: plat.rect.y += abs(int(self.player.vel.y)) if plat.rect.top > HEIGHT: plat.kill() self.score += 10
if self.player.rect.bottom > HEIGHT: for sprite in self.all_sprites: sprite.rect.y -= int(max(self.player.vel.y, 10)) if sprite.rect.bottom < 0: sprite.kill() if len(self.platforms) == 0: self.playing = False
while len(self.platforms) < 6: width = random.randrange(50, 100) p = Platform(random.randrange(0, WIDTH - width), random.randrange(-75, -30), width, 20) self.platforms.add(p) self.all_sprites.add(p)
def events(self): for event in pg.event.get(): if event.type == pg.QUIT: if self.playing: self.playing = False self.running = False if event.type == pg.KEYDOWN: if event.key == pg.K_SPACE: self.player.jump() if event.type == pg.KEYUP: if event.key == pg.K_SPACE: self.player.jump_cut()
def draw(self): self.screen.fill(BACKGROUND_COLOR) self.all_sprites.draw(self.screen) self.draw_text(str(self.score)+" score", 22, WHITE, WIDTH / 2, 25) pg.display.flip()
def show_start_screen(self): self.screen.fill(BACKGROUND_COLOR) self.draw_text(TITLE, 48, WHITE, WIDTH*0.5, HEIGHT*0.25) self.draw_text("Arrows to move, Space to jump", 22, WHITE, WIDTH*0.5, HEIGHT*0.5) self.draw_text("Press a key to play", 22, WHITE, WIDTH * 0.5, HEIGHT * 0.75) pg.display.flip() self.wait_for_key()
def show_go_screen(self): self.screen.fill(BACKGROUND_COLOR) self.draw_text("Congratulations !", 48, WHITE, WIDTH * 0.5, HEIGHT * 0.25) self.draw_text("Score: " + str(self.score), 22, WHITE, WIDTH * 0.5, HEIGHT * 0.5) self.draw_text("Press a key to play again", 22, WHITE, WIDTH * 0.5, HEIGHT * 0.75) pg.display.flip() self.wait_for_key()
def wait_for_key(self): self.waiting = True while self.waiting: self.clock.tick(FPS) for event in pg.event.get(): if event.type == pg.QUIT: self.waiting = False self.running = False if event.type == pg.KEYDOWN: self.waiting = False
def draw_text(self, text, size, color, x, y): font = pg.font.SysFont("arial", size) text_surface = font.render(text, True, color) text_rect = text_surface.get_rect() text_rect.midtop = (int(x), int(y)) self.screen.blit(text_surface, text_rect)
g = Game() g.show_start_screen() while g.running: g.new() g.show_go_screen()
|