def play(): global game_over #initialize pygame & window screen = pygame.display.get_surface() #fill background bg = pygame.Surface((screen.get_size())) bg.fill(BG) bg = bg.convert() screen.blit(bg, (0, 0)) #initialize scoreboard score_board = Score(bg) score_board.game_over = game_over_screen score_board.win = win_screen #initialize the wall (bricks) wall, wall_g = init_wall(score_board) #initialize the bat bat = Bat(10) batsprite = pygame.sprite.RenderPlain(bat) #initialize the ball ball = Ball((255, 255, 255), 5, bat, wall, score_board) ballsprite = pygame.sprite.RenderPlain(ball) clock = pygame.time.Clock() pause = False game.init() t1 = threading.Thread(target=tutorial, args=()) t1.start() out_count = 0 while True: while pause == True: clock.tick(60) pygame.mouse.set_visible(True) pygame.display.flip() for event in pygame.event.get(): if event.type == KEYDOWN: if event.key == K_ESCAPE: bat.movepos = [0,0] pause = False game.resume() game.notify("Game Resumed") elif event.type == QUIT: sys.exit(0) else: game.handle_pause_event(event) while game_over == True: clock.tick(60) pygame.mouse.set_visible(True) game.game_over_screen() pygame.display.flip() for event in pygame.event.get(): if event.type == QUIT: sys.exit(0) else: game.handle_event(event) while win == True: clock.tick(60) pygame.mouse.set_visible(True) game.win_screen() pygame.display.flip() for event in pygame.event.get(): if event.type == QUIT: sys.exit(0) else: game.handle_win_event(event) #handle events while pause == False and game_over == False and win == False: clock.tick(60) #max frame rate for event in pygame.event.get(): if event.type == QUIT: sys.exit(0) if event.type == KEYDOWN: if event.key == K_LEFT: bat.moveleft() elif event.key == K_RIGHT: bat.moveright() elif event.key == K_SPACE: screen.blit(bg, ball.rect, ball.rect) ball.shoot() elif event.key == K_ESCAPE: pause = True game.pause() elif event.type == KEYUP: if event.key == K_LEFT or event.key == K_RIGHT: bat.movepos = [0, 0] elif event.type == MOUSEMOTION: pygame.mouse.set_visible(False) x, y = pygame.mouse.get_pos() bx, by = bat.rect.midtop if x > screen.get_width()-(bat.width/2): x = screen.get_width()-(bat.width/2) elif x < bat.width/2: x = bat.width/2 screen.blit(bg, bat.rect, bat.rect) bat.rect.midtop = (x,by) if not screen.get_rect().contains(ball.rect): out_count += 1 if out_count >= 10: score_board.die() else: out_count = 0 cols, side = break_wall(ballsprite, wall_g) if cols: if side is 'left' or side is 'right': ball.deflect(1.5) if side is 'top' or side is 'bottom': ball.deflect(2.5) elif side == 'topright' or side == 'topleft': ball.deflect(1) elif side == 'bottomright' or side == 'bottomleft': ball.deflect(1) else: ball.deflect(1) for i in range(len(cols)): cols[i].destroy() side = coll_side(ball.area.get_rect(), ball.rect, True, True) if side == 'top': score_board.die() if score_board.updated_level != 0: print 'speed increased' ball.increment_speed = 3 if pause == False: for y in range(5): for i in range(30): if wall[y][i].destroyed == False: screen.blit(bg, wall[y][i].rect, wall[y][i].rect) #wall[y][i].destroy() score_board.update() screen.blit(bg, bat.rect, bat.rect) screen.blit(bg, ball.rect, ball.rect) ballsprite.update() batsprite.update() ballsprite.draw(screen) batsprite.draw(screen) wall_g.draw(screen) pygame.display.update()
def main(): pygame.init() pygame.display.set_caption('Interference') pygame.key.set_repeat(10, 10) # keypresses to auto-repeat every 10msec rects = [] # create a list screen = pygame.display.set_mode((screenwidth, screenheight)) background = Background("assets/level1-stage.gif", [0, 0]) bgsurface = pygame.Surface(screen.get_size()) bgsurface = bgsurface.convert() # speeds up blitting bgsurface.fill([0, 0, 255]) # set background to blue # if we only use a portion of the background image, change the first two values here: bgsurface.blit( background.image.subsurface(0, 0, screenwidth, screenheight), (0, 0)) screen.blit(bgsurface, (0, 0)) # get the bgsurface onto the screen surface pygame.display.update() # update the entire display # instantiate the sprites ball_sprite = Ball() bat_sprite = Bat() # store all sprite rects we create into this list rects.append(ball_sprite.getrect()) rects.append(bat_sprite.getrect()) balls_group = pygame.sprite.Group(ball_sprite) characters_group = pygame.sprite.Group(bat_sprite) while True: # this is a forever loop ev = pygame.event.poll() if ev.type == pygame.QUIT: pygame.quit() sys.exit(0) batmoved = 0 if ev.type == KEYDOWN: # button pressed? if (ev.key == btn_left): batloc = [-1, 0] bat_sprite.movepos(batloc) ball_sprite.accel(-1) # influence ball speed if needed batmoved = -1 elif (ev.key == btn_right): batloc = [1, 0] bat_sprite.movepos(batloc) ball_sprite.accel(1) # influence ball speed if needed batmoved = 1 # check for collisions collide = pygame.sprite.collide_rect(bat_sprite, ball_sprite) if collide: ball_sprite.collision(bat_sprite.getrect(), batmoved) # clear the sprite(s) and then update and redraw them characters_group.clear(screen, bgsurface) balls_group.clear(screen, bgsurface) characters_group.update() balls_group.update() characters_group.draw(screen) balls_group.draw(screen) pygame.display.update(rects)