def welcomscreen(): """ show welcome screen """ playerx=int(SCREENWIDTH/5) playery=int(SCREENHEIGHT-GAME_SPRITES['player'].get_height())/2 messagex=int(SCREENWIDTH-GAME_SPRITES['message'].get_width())/2 messagey=int(SCREENHEIGHT*0.13) basex==0 while True: for event in pygame.get(): if event.type==QUIT or (event.type==KEYDOWN and event.key==K_ESCAPE): pygame.quit() sys.exit() elif event.type==KEYDOWN and (event.type==K_SPACE or event.type==K_UP): return else: screen.blit(GAME_SPRITES['BACKGROUND'],(0,0)) screen.blit(GAME_SPRITES['PLAYER'],(playerx,playery)) screen.blit(GAME_SPRITES['MESSAGE'],(messagex,messagey)) screen.blit(GAME_SPRITES['BASE'],(basex,GROUNDY)) pygam.dislay.update() FPSCLOCK.tick(FPS)
def run_game(): game_over = False game_close = False x = width / 2 y = height / 2 x_speed = 0 y_speed = 0 snake_pixels = [] snake_length = 1 target_x = round(random.randrange(0,width-snake_size/10.0)*10) target_y = round(random.randrange(0,height-snake_size/10.0)*10) while not game_over: while game_close: game_display.fill(black) game_over_message = message_font.render("Game Over! ", True, red) game_display.blit(game_over_message, [width/3, height/3]) printscore(snake_length -1) pygame.update() for event in pygame.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_1: game_over = True game_close = False if event.key == pygame.K_2: run_game() if event.type == pygame.quit: game_over = True game_close = False for event in pygame.event.get(): if event.type == pygame.QUIT: game_over == True if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: x_speed = -snake_size y_speed = 0 if event.key == pygame.K_RIGHT: x_speed = snake_size y_speed = 0 if event.key == pygame.K_UP: x_speed = 0 y_speed = -snake_size if event.key == pygame.K_DOWN: x_speed = 0 y_speed = snake_size if x >= width or x < 0 or y >= height or y < 0: game_close = True x += x_speed y += y_speed game_display.fill(black) pygame.draw.rect(game_display, orange, [target_x, target_y, snake_size, snake_size]) snake_pixels.append([x,y]) if len(snake_pixels) > snake_length: del snake_pixels[0] for pixel in snake_pixels[:-1]: if pixel == [x,y]: game_close = True draw_snake(snake_size, snake_pixels) printscore(snake_length -1) pygame.display.update() if x == target_x and y == target_y: target_x = round(random.randrange(0,width-snake_size/10.0)*10) target_y = round(random.randrange(0,height-snake_size/10.0)*10) snake_length += 1 clock.tick(snake_speed) pygame.quit() quit
pygame.display.set_caption("John's First Video Game") # Sets up the Game Exit Value gameExit = False # Starting value for movments lead_x = 300 lead_y = 300 lead_x_change = 0 # Sets up the FPS clock = pygame.time.Clock() # Game Event Zone while not gameExit: for event in pygame.get(): if event.type == pygame.QUIT: gameExit = True if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: lead_x_change = -10 if event.key == pygame.K_RIGHT: lead_x_change = 10 if event.type == pygame.KEYUP: if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT: lead_x_change = 0 lead_x += lead_x_change # updates the movment gameDisplay.fill(white) # Color the whole screen white pygame.draw.rect(gameDisplay,black, [lead_x,lead_y, 10, 10]) # Object on the screen
class display_game_objs(): def __init__(self): print("PROGRAM: Displaying game objects") def game_obj(self): # Def will draw player, enemy, and ping pong sprites on screen pygame.draw.rect(screen, red, player_sprite) pygame.draw.rect(screen, red, enemy_sprite) pygame.draw.rect(screen, red, ping_pong_sprite) """ Class Variable """ # Stores class object_x variable object_game_objs = display_game_objs() ############################ ##################### ##### GAME LOOP ##### ##################### running = True while running: screen.fill(green) for event in pygame.get(): if event.type == QUIT: pygame.quit() sys.exit() ############################ ################### ##### EXECUTE ##### ################### object_game_objs.game_objs()