staikx


SUBMITTED BY: staikx

DATE: Feb. 22, 2023, 10:32 p.m.

FORMAT: Text only

SIZE: 2.1 kB

HITS: 390

  1. import pygame
  2. import random
  3. # Initialize pygame
  4. pygame.init()
  5. # Set up the screen
  6. screen_width = 800
  7. screen_height = 600
  8. screen = pygame.display.set_mode((screen_width, screen_height))
  9. pygame.display.set_caption("Skateboard Game")
  10. # Set up colors
  11. white = (255, 255, 255)
  12. black = (0, 0, 0)
  13. # Set up player
  14. player_width = 50
  15. player_height = 50
  16. player_x = (screen_width - player_width) / 2
  17. player_y = screen_height - player_height
  18. player_speed = 5
  19. # Set up obstacles
  20. obstacle_width = 50
  21. obstacle_height = 50
  22. obstacle_x = random.randint(0, screen_width - obstacle_width)
  23. obstacle_y = 0
  24. obstacle_speed = 5
  25. # Set up score
  26. score = 0
  27. font = pygame.font.SysFont(None, 30)
  28. # Game loop
  29. game_over = False
  30. clock = pygame.time.Clock()
  31. while not game_over:
  32. # Handle events
  33. for event in pygame.event.get():
  34. if event.type == pygame.QUIT:
  35. game_over = True
  36. # Move player
  37. keys = pygame.key.get_pressed()
  38. if keys[pygame.K_LEFT] and player_x > 0:
  39. player_x -= player_speed
  40. if keys[pygame.K_RIGHT] and player_x < screen_width - player_width:
  41. player_x += player_speed
  42. # Move obstacle
  43. obstacle_y += obstacle_speed
  44. if obstacle_y > screen_height:
  45. obstacle_x = random.randint(0, screen_width - obstacle_width)
  46. obstacle_y = 0
  47. score += 1
  48. # Check for collision
  49. if player_x + player_width > obstacle_x and player_x < obstacle_x + obstacle_width and player_y + player_height > obstacle_y and player_y < obstacle_y + obstacle_height:
  50. game_over = True
  51. # Draw objects
  52. screen.fill(white)
  53. pygame.draw.rect(screen, black, (player_x, player_y, player_width, player_height))
  54. pygame.draw.rect(screen, black, (obstacle_x, obstacle_y, obstacle_width, obstacle_height))
  55. score_text = font.render("Score: " + str(score), True, black)
  56. screen.blit(score_text, (10, 10))
  57. pygame.display.update()
  58. # Set frame rate
  59. clock.tick(60)
  60. # Clean up
  61. pygame.quit()

comments powered by Disqus