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 2.5KB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 game_over():
  55. global image_g_o
  56. screen.blit(image_g_o, (0, 0))
  57. def update():
  58. global x, y, width_player, height_player, speed, width, zaehler, height
  59. if (speed == -1 and rect.x <= 0) or (speed == 1 and rect.x >= (width - width_player)):
  60. pass
  61. else :
  62. rect.x = rect.x + speed
  63. if (zaehler%2 == 0) and (image_rect.x <= 0 or image_rect.x >= width) :
  64. reflect_x()
  65. elif (zaehler%2 == 0) and image_rect.y <= 0 :
  66. reflect_y()
  67. 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)) :
  68. reflect_y()
  69. zaehler = zaehler + 1
  70. if (zaehler%2 == 0):
  71. image_rect.x += speed_x
  72. image_rect.y += speed_y
  73. else:
  74. pass
  75. if image_rect.y == height and (zaehler%2 == 0):
  76. game_over()
  77. def draw():
  78. screen.fill(black)
  79. screen.blit(image, image_rect)
  80. pygame.draw.rect(screen, red, rect)
  81. pygame.display.flip()
  82. pygame.init()
  83. init()
  84. initRectangle()
  85. screen = pygame.display.set_mode(size)
  86. running = True
  87. while running:
  88. running = input()
  89. update()
  90. draw()