main.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import pygame
  2. import random
  3. from sys import exit
  4. # Initialize pygame
  5. pygame.init()
  6. # Logical screen dimensions
  7. LOGICAL_WIDTH = 113
  8. LOGICAL_HEIGHT = 80
  9. SCALE_FACTOR = 4 # Scale by 4 times
  10. # Create the window with the scaled size
  11. WINDOW_WIDTH = LOGICAL_WIDTH * SCALE_FACTOR
  12. WINDOW_HEIGHT = LOGICAL_HEIGHT * SCALE_FACTOR
  13. screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT), pygame.RESIZABLE)
  14. pygame.display.set_caption('Stick Catch Game')
  15. clock = pygame.time.Clock()
  16. roundringimage = pygame.image.load('roundring.png').convert()
  17. test_font = pygame.font.Font('3-by-5-pixel-font.ttf', 8)
  18. logical_surface = pygame.Surface((LOGICAL_WIDTH, LOGICAL_HEIGHT))
  19. FINISHED = False
  20. # Stick class
  21. class Stick:
  22. def __init__(self, xpos, ypos):
  23. self.x = xpos
  24. self.y = ypos
  25. self.velocity_y = 0 # Vertical velocity
  26. self.gravity = 0.01
  27. self.stickimage = pygame.image.load('stick.png').convert_alpha()
  28. self.stickrect = self.stickimage.get_rect()
  29. self.stickrect.topleft = (self.x, self.y)
  30. def get_surf(self):
  31. return self.stickimage
  32. def change_xy(self, xpos, ypos):
  33. self.x = xpos
  34. self.y = ypos
  35. def update(self):
  36. self.velocity_y += self.gravity
  37. self.y += self.velocity_y
  38. self.stickrect.topleft = (self.x, self.y)
  39. # Create sticks
  40. sticks = []
  41. stickHeight = 8
  42. heightList = [0, 1, 2, 2, 3]
  43. heightList = heightList + heightList[::-1]
  44. for num, i in enumerate(range(10)):
  45. sticks.append(Stick(18 + (8 * i), stickHeight + heightList[num])) # Adjusted for logical scale
  46. # Game variables
  47. dirt_y_pos = 5
  48. speed = 1 # Adjust for logical scale
  49. move_down = True
  50. bottom = LOGICAL_HEIGHT - 24 # Adjust for logical scale
  51. pause = False
  52. index = random.randint(0, 9)
  53. timer_active = False
  54. timer_start = 0
  55. timer_duration = 2000
  56. counter = 0
  57. # Game loop
  58. while True:
  59. for event in pygame.event.get():
  60. if event.type == pygame.QUIT:
  61. pygame.quit()
  62. exit()
  63. if event.type == pygame.MOUSEBUTTONDOWN:
  64. print(event.pos)
  65. if sticks[index].stickrect.collidepoint((event.pos[0]/SCALE_FACTOR, event.pos[1]/SCALE_FACTOR)):
  66. print("AAAA")
  67. counter+=1
  68. # Fill the logical surface
  69. logical_surface.fill((0, 0, 0)) # Black background
  70. logical_surface.blit(roundringimage, (LOGICAL_WIDTH/2-roundringimage.get_width()/2, 3))
  71. text_surface = test_font.render(str(counter).zfill(2), False, (245, 27, 27))
  72. logical_surface.blit(text_surface, (53, 44))
  73. # Draw sticks
  74. for dirt in sticks:
  75. logical_surface.blit(dirt.get_surf(), (dirt.x, dirt.y))
  76. # Move the stick
  77. if not FINISHED:
  78. if sticks[index].y >= LOGICAL_HEIGHT:
  79. sticks.pop(index)
  80. timer_active = True
  81. timer_start = pygame.time.get_ticks()
  82. if len(sticks) > 0:
  83. index = random.randint(0, len(sticks) - 1)
  84. else:
  85. FINISHED = True
  86. if timer_active:
  87. elapsed_time = pygame.time.get_ticks() - timer_start
  88. if elapsed_time >= timer_duration:
  89. timer_active = False
  90. if not timer_active and not FINISHED:
  91. sticks[index].update()
  92. logical_surface.blit(sticks[index].get_surf(), (sticks[index].x, sticks[index].y))
  93. # Scale and blit the logical surface onto the screen
  94. scaled_surface = pygame.transform.scale(logical_surface, (WINDOW_WIDTH, WINDOW_HEIGHT))
  95. screen.blit(scaled_surface, (0, 0))
  96. # Update the display
  97. pygame.display.update()
  98. clock.tick(60)