class DoodleJumpGame: FPS = 30 def __init__(self): pygame.init() self.screen = Screen() self.clock = pygame.time.Clock() self.doodler = Doodler(Screen.WIDTH / 2, Screen.HEIGHT / 2) self.p1 = Platform(Screen.WIDTH / 2, Screen.HEIGHT / 2 + 130) # jogador aparece do lado oposto ao passar pela borda def check_border(self): if self.doodler.rect.centerx > self.screen.WIDTH: self.doodler.pos_x = 0 elif self.doodler.rect.centerx < 0: self.doodler.pos_x = self.screen.WIDTH def key_pressed(self, key): if key[pygame.K_RIGHT] or key[pygame.K_d]: self.doodler.right_movement() elif key[pygame.K_LEFT] or key[pygame.K_a]: self.doodler.left_movement() def check_events(self): for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() if event.type == pygame.KEYDOWN: # pressinou e soltou a tecla key = pygame.key.get_pressed() if key[pygame.K_UP] or key[pygame.K_SPACE] or key[pygame.K_w]: self.doodler.shoot() self.key_pressed( pygame.key.get_pressed()) # manteve a tecla pressionada def check_collide(self): hits = pygame.sprite.collide_mask(self.doodler, self.p1) if hits: time.sleep(1) self.doodler.pos_y -= self.doodler.SPEED * 20 def draw(self): self.screen.draw_background() self.screen.draw_platform(self.p1.image, self.p1.rect) self.screen.draw_doodler(self.doodler.image, self.doodler.rect) self.screen.update() def run(self): while True: self.clock.tick(DoodleJumpGame.FPS) self.check_events() self.doodler.update() self.draw() self.check_collide() self.check_border()
def run_game(): # Initialize the game and create screen obj. pygame.init() ai_settings = Settings() screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption("Doodler Fight -- Rubing") doodler = Doodler(ai_settings, screen) #f = open("rect_ship.txt", "w") # Make a group to store bullets in. bullets = Group() # Start the main loop for the game while True: check_events(ai_settings, screen, doodler, bullets) doodler.update() #bullets.update_bullet()#'Group' object has no attribute 'update_bullet' update_bullets(bullets) #f.write("%d\t%d\t%d\t%d\n" % (ship.ship_rect.centerx, ship.screen_rect.centerx, ship.screen_rect.top, ship.screen_rect.bottom)) update_screen(ai_settings, screen, doodler, bullets)
class DoodleJump: pygame.init() pygame.display.set_caption("Doodle Jump") platforms = [] # координаты платформ def __init__(self): self.window = pygame.display.set_mode((700, 700)) self.platforms = [[400, 700]] self.platform = pygame.image.load("static/platform_mini.png") self.doodler = Doodler() self.curr_level = 50 self.run = True def draw_platforms(self): global a print(len(self.platforms)) for platform in self.platforms: a = random.randint(0, 700) if platform[1] > self.doodler.cameray + 700: self.platforms.remove(platform) self.window.blit(self.platform, (platform[0], platform[1] - self.doodler.cameray)) if self.platforms[-1][1] >= self.doodler.cameray: self.platforms.append([a, self.platforms[-1][1] - 50]) self.platforms.append( [random.randint(0, 700), self.platforms[-1][1] - 100]) def update_platforms(self): player = pygame.Rect(self.doodler.x, self.doodler.y, self.doodler.image.get_width(), self.doodler.image.get_height() - 10) for platform in self.platforms: rect = pygame.Rect(platform[0], platform[1], self.platform.get_width(), self.platform.get_height()) if rect.colliderect( player ) and self.doodler.gravity and self.doodler.y < platform[ 1] - self.doodler.cameray: self.doodler.jump = 15 self.doodler.gravity = 0 def isDeath(self): if self.doodler.y - self.doodler.cameray > 1000: self.platforms = [[0, -100, 0]] self.run = False print("Умер") def main(self): # platform = Platform() while self.run: if self.doodler.y - self.doodler.cameray < 1000: self.update_platforms() self.draw_platforms() self.window.blit( self.doodler.image, [self.doodler.x, self.doodler.y - self.doodler.cameray]) self.doodler.update() pygame.display.update() pygame.time.delay(30) self.window.fill((0, 220, 255)) for event in pygame.event.get(): if event.type == pygame.QUIT: self.run = False self.isDeath() pygame.quit()
class DoodleJump: GAME_FIELD_EDGE = 700 UPDATE_PLATFORMS_CONSTANT = 1000 PLATFORM_POP_CONST = 900 VERY_BEATIFUL_COSTIL = -50000 def __init__(self): self.window = pygame.display.set_mode( (DoodleJump.GAME_FIELD_EDGE, DoodleJump.GAME_FIELD_EDGE)) self.platforms = [[400, DoodleJump.GAME_FIELD_EDGE, 0]] self.sprite = pygame.image.load("static/platform_mini.png") self.doodler = Doodler() self.curr_level = 50 self.run = True def draw_platforms(self): global a for platform in self.platforms: a = random.randint(0, DoodleJump.GAME_FIELD_EDGE) if platform[1] > DoodleJump.VERY_BEATIFUL_COSTIL: self.window.blit( self.sprite, (platform[0], platform[1] - self.doodler.cameray)) self.platforms.append([a, self.platforms[-1][1] - 50, 0]) self.platforms.append([ random.randint(0, DoodleJump.GAME_FIELD_EDGE), self.platforms[-1][1] - 100, 0 ]) if self.platforms[1][1] - self.doodler.cameray > \ DoodleJump.PLATFORM_POP_CONST: self.platforms.pop(0) def update_platforms(self): player = pygame.Rect(self.doodler.x, self.doodler.y, self.doodler.image.get_width(), self.doodler.image.get_height()) for platform in self.platforms: rect = pygame.Rect(platform[0], platform[1], self.sprite.get_width(), self.sprite.get_height()) if rect.colliderect( player ) and self.doodler.gravity and self.doodler.y < platform[ 1] - self.doodler.cameray: self.doodler.jump = 15 self.doodler.gravity = 0 def main(self): while self.run: if (self.doodler.y - self.doodler.cameray < DoodleJump.UPDATE_PLATFORMS_CONSTANT): self.update_platforms() self.draw_platforms() self.window.blit( self.doodler.image, [self.doodler.x, self.doodler.y - self.doodler.cameray]) self.doodler.update() pygame.display.update() pygame.time.delay(30) self.window.fill((0, 220, 255)) for event in pygame.event.get(): if event.type == pygame.QUIT: self.run = False pygame.quit()
class DoodleJump: pygame.display.set_caption("Doodle Jump") platforms = [] # координаты платформ def __init__(self): self.window = pygame.display.set_mode((700, 700)) self.platforms = [[ 400, 700, 0 ]] # третий параметр - стукнулся ли о платформу дудлер # self.sprite_group = pygame.sprite.Group() # не надо? self.platform = pygame.image.load("static/platform_mini.png") self.doodler = Doodler() self.curr_level = 50 self.run = True # self.font = pygame.font.SysFont('Arial', 25) # это в класс Дудлер # def jump(self): # self.doodler.isJump = True # if self.doodler.jumpCount >= -10: # if self.doodler.jumpCount < 0: # self.doodler.y += int(self.doodler.jumpCount ** 2 / 2) # # else: # self.doodler.y -= int(self.doodler.jumpCount ** 2 / 2) # self.doodler.jumpCount -= 1 # else: # self.doodler.isJump = False # self.doodler.jumpCount = 10 def draw_platforms(self): global a for platform in self.platforms: a = random.randint(0, 700) if platform[1] > -50000: self.window.blit( self.platform, (platform[0], platform[1] - self.doodler.cameray)) self.platforms.append([a, self.platforms[-1][1] - 50, 0]) self.platforms.append( [random.randint(0, 700), self.platforms[-1][1] - 100, 0]) if self.platforms[1][1] - self.doodler.cameray > 900: self.platforms.pop(0) def update_platforms(self): player = pygame.Rect(self.doodler.x, self.doodler.y, self.doodler.image.get_width(), self.doodler.image.get_height() - 10) for platform in self.platforms: rect = pygame.Rect(platform[0], platform[1], self.platform.get_width(), self.platform.get_height()) if rect.colliderect( player ) and self.doodler.gravity and self.doodler.y < platform[ 1] - self.doodler.cameray: self.doodler.jump = 15 self.doodler.gravity = 0 def isDeath(self): if self.doodler.y - self.doodler.cameray > 1000: self.platforms = [[0, -100, 0]] def main(self): platform = Platform() while self.run: # drawGrid() if (self.doodler.y - self.doodler.cameray < 1000): self.update_platforms() self.draw_platforms() self.window.blit( self.doodler.image, [self.doodler.x, self.doodler.y - self.doodler.cameray]) # self.sprite_group.draw(self.win) self.doodler.update() # self.playerX = self.doodler.x # не должно находиться в main # self.jump() pygame.display.update() pygame.time.delay(30) self.window.fill((0, 220, 255)) for event in pygame.event.get(): if event.type == pygame.QUIT: self.run = False self.isDeath() pygame.quit()