You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pong.py 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import pygame
  2. # init Spieldaten
  3. speed = 0
  4. def init():
  5. global width, height, size, zaehler
  6. global speed_x, speed_y, image_g_o
  7. zaehler = 0
  8. speed_x = -1
  9. speed_y = -1
  10. global black, red
  11. global image, image_rect
  12. width = 1080
  13. height = 720
  14. size = (width, height)
  15. black = (0, 0, 0)
  16. red = (255, 0, 0)
  17. image = pygame.image.load("ohm.png")
  18. image_g_o = pygame.image.load("color-me-happy-game-over.jpg")
  19. image_rect = image.get_rect()
  20. image_rect.x = 500
  21. image_rect.y = 350
  22. def initRectangle():
  23. global rect, width_player, height_player, x, y
  24. x = 0
  25. y = 650
  26. width_player = 300
  27. height_player = 40
  28. rect = pygame.Rect(x, y, width_player, height_player)
  29. def input():
  30. global speed
  31. for event in pygame.event.get():
  32. if event.type == pygame.QUIT:
  33. return False
  34. if event.type == pygame.KEYDOWN:
  35. if event.key == pygame.K_LEFT:
  36. speed = -1
  37. if event.key == pygame.K_RIGHT:
  38. speed = 1
  39. if event.type == pygame.KEYUP:
  40. if event.key == pygame.K_LEFT:
  41. speed = 0
  42. if event.key == pygame.K_RIGHT:
  43. speed = 0
  44. if event.type == pygame.KEYDOWN:
  45. if event.key == pygame.K_ESCAPE:
  46. return False
  47. return True
  48. def reflect_x():
  49. global speed_x
  50. speed_x *= -1
  51. def reflect_y():
  52. global speed_y
  53. speed_y *= -1
  54. def show_game_over():
  55. global image_g_o
  56. screen.blit(image_g_o, (0, 0))
  57. pygame.display.flip()
  58. waiting = True
  59. while waiting:
  60. for event in pygame.event.get():
  61. if (event.type == pygame.QUIT or event.key == pygame.K_ESCAPE):
  62. print('quit')
  63. waiting = False
  64. pygame.quit()
  65. #self.running = False
  66. if event.type == pygame.KEYDOWN:
  67. if event.key == pygame.K_SPACE:
  68. print('space')
  69. waiting = False
  70. init_game()
  71. def update():
  72. global x, y, width_player, height_player, speed, width, zaehler, height
  73. if (speed == -1 and rect.x <= 0) or (speed == 1 and rect.x >= (width - width_player)):
  74. pass
  75. else :
  76. rect.x = rect.x + speed
  77. if (zaehler%2 == 0) and (image_rect.x <= 0 or image_rect.x >= width) :
  78. reflect_x()
  79. elif (zaehler%2 == 0) and image_rect.y <= 0 :
  80. reflect_y()
  81. if (zaehler%2 == 0) and (image_rect.y == (y - height_player) and image_rect.x >= rect.x and image_rect.x <= (rect.x + width_player)) :
  82. reflect_y()
  83. zaehler = zaehler + 1
  84. if (zaehler%2 == 0):
  85. image_rect.x += speed_x
  86. image_rect.y += speed_y
  87. else:
  88. pass
  89. if image_rect.y >= height and (zaehler%2 == 0):
  90. show_game_over()
  91. def draw():
  92. screen.fill(black)
  93. screen.blit(image, image_rect)
  94. pygame.draw.rect(screen, red, rect)
  95. pygame.display.flip()
  96. def init_game():
  97. global screen
  98. pygame.init()
  99. init()
  100. initRectangle()
  101. screen = pygame.display.set_mode(size)
  102. init_game()
  103. running = True
  104. game_over = False
  105. while running:
  106. if game_over:
  107. show_game_over()
  108. running = input()
  109. update()
  110. draw()