Dieses Repository enthält Python-Dateien die im Rahmen des Wahlpflichtmoduls "Informationssysteme in der Medizintechnik" (Dozent: Prof. Dr. Oliver Hofmann) erstellt wurden und verwaltet deren Versionskontrolle.
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 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import pygame
  2. import random
  3. import time
  4. running = False
  5. gameover = False
  6. def initScreen():
  7. global black, red, white, screen, width, height
  8. width = 640
  9. height = 400
  10. size = (width, height)
  11. screen = pygame.display.set_mode(size)
  12. white = (255, 255, 255)
  13. black = (0,0,0)
  14. red = (255,0,0)
  15. pygame.display.set_caption("PONG!")
  16. def initImage():
  17. global image, image_rect
  18. image = pygame.image.load("seehofer.png")
  19. image_rect = image.get_rect()
  20. image_rect.centerx = width/2
  21. image_rect.centery = height/2
  22. global size_image
  23. size_image = image.get_rect().size
  24. def initImageMovement():
  25. random.seed(a=None, version=2)
  26. rand_x = random.randint(1, 4)
  27. random.seed(a=None, version=2)
  28. rand_y = random.randint(1, 4)
  29. global move_vector
  30. move_vector = [ rand_x, rand_y ]
  31. class Rectangle(pygame.Rect):
  32. def __init__(self, rect_width, rect_height):
  33. self.width = rect_width
  34. self.height = rect_height
  35. self.centerx = width/2
  36. self.centery = height - (rect_height/2)
  37. self.speed_x = 0
  38. def initRedRectangle():
  39. global red_rect
  40. red_rect = Rectangle(100, 12)
  41. def doMovement():
  42. time.sleep(0.01)
  43. image_rect.centerx += move_vector[0]
  44. image_rect.centery += move_vector[1]
  45. if size_image[0]/2 <= image_rect.centerx <= width-(size_image[0]/2) and size_image[1]/2 <= image_rect.centery <= height-(size_image[1]/2):
  46. pass
  47. else:
  48. if image_rect.centerx >= width-size_image[0]/2 or image_rect.centerx <= size_image[0]/2:
  49. # CASE 1: image bounces on the right/left wall
  50. move_vector[0] = -(move_vector[0])
  51. elif image_rect.centery <= size_image[1]/2:
  52. # CASE 2: image bounces on the upper wall
  53. move_vector[1] = -(move_vector[1])
  54. elif image_rect.centery >= height-size_image[1]/2:
  55. # CASE 3: image is about to pass the lower border of screen
  56. if image_rect.colliderect(red_rect):
  57. # CASE 3.1: image touches the red balk
  58. move_vector[1] = -(move_vector[1])
  59. if not image_rect.colliderect(red_rect):
  60. # CASE 3.2: image does not touch the red balk
  61. showGameOver()
  62. global gameover
  63. gameover = True
  64. else:
  65. # CASE 4: image bounces in one of the upper corners (right or left)
  66. move_vector[0] = -(move_vector[0])
  67. move_vector[1] = -(move_vector[1])
  68. def moveRedBalk():
  69. if (red_rect.width/2) <= red_rect.centerx <= width-(red_rect.width/2):
  70. red_rect.centerx += red_rect.speed_x * 3
  71. elif red_rect.centerx <= (red_rect.width/2) or red_rect.centerx >= width-(red_rect.width/2):
  72. red_rect.speed_x = -(red_rect.speed_x)
  73. red_rect.centerx += red_rect.speed_x * 3
  74. def showGameOver():
  75. global text, gameover_rect
  76. font = pygame.font.Font(None, 38)
  77. text = font.render("Game Over!", 1, white)
  78. gameover_rect = text.get_rect()
  79. gameover_rect.centerx = width/2
  80. gameover_rect.centery = height/2
  81. def input():
  82. for event in pygame.event.get():
  83. if event.type == pygame.QUIT:
  84. return
  85. if event.type == pygame.KEYDOWN:
  86. if event.key == pygame.K_ESCAPE:
  87. return False
  88. if event.key == pygame.K_RIGHT:
  89. red_rect.speed_x = 1
  90. if event.key == pygame.K_LEFT:
  91. red_rect.speed_x = -1
  92. if event.type == pygame.KEYUP:
  93. red_rect.speed_x = 0
  94. return True
  95. def update():
  96. doMovement()
  97. moveRedBalk()
  98. def draw():
  99. screen.fill(black)
  100. if gameover:
  101. screen.blit(text, gameover_rect)
  102. else:
  103. screen.blit(image, image_rect)
  104. pygame.draw.rect(screen, red, red_rect)
  105. pygame.display.flip()
  106. #------------- MAIN PROGRAMM ----------------
  107. pygame.init()
  108. initScreen()
  109. initImage()
  110. initImageMovement()
  111. initRedRectangle()
  112. running = True
  113. #---------------- GAME LOOP ------------------
  114. while running:
  115. running = input()
  116. update()
  117. draw()