Untitled
unknown
plain_text
2 years ago
2.0 kB
1
Indexable
import pygame import random pygame.init() # set up the display width = 800 height = 600 screen = pygame.display.set_mode((width, height)) pygame.display.set_caption("Dodger") # set up the colors white = (255, 255, 255) black = (0, 0, 0) red = (255, 0, 0) # set up the player player_width = 50 player_height = 50 player_x = width // 2 - player_width // 2 player_y = height - player_height - 10 player_speed = 5 # set up the blocks block_width = 50 block_height = 50 block_x = random.randint(0, width - block_width) block_y = -block_height block_speed = 5 # set up the score score = 0 font = pygame.font.SysFont(None, 30) # set up the game loop game_over = False clock = pygame.time.Clock() while not game_over: # event handling for event in pygame.event.get(): if event.type == pygame.QUIT: game_over = True # player movement keys = pygame.key.get_pressed() if keys[pygame.K_LEFT] and player_x > 0: player_x -= player_speed if keys[pygame.K_RIGHT] and player_x < width - player_width: player_x += player_speed # update the block position block_y += block_speed if block_y > height: block_x = random.randint(0, width - block_width) block_y = -block_height score += 1 block_speed += 1 # collision detection if player_y < block_y + block_height: if player_x < block_x + block_width and player_x + player_width > block_x: game_over = True # draw the screen screen.fill(white) pygame.draw.rect(screen, black, [player_x, player_y, player_width, player_height]) pygame.draw.rect(screen, red, [block_x, block_y, block_width, block_height]) score_text = font.render("Score: " + str(score), True, black) screen.blit(score_text, [10, 10]) pygame.display.update() # set the frame rate clock.tick(60) # end of game pygame.quit()
Editor is loading...