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.6KB

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