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.

main.py 2.5KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import pygame
  2. import time
  3. # Initialisierung der Spieldaten
  4. def init():
  5. global width, height, size, black, red
  6. global image, image_rect
  7. global speed_image
  8. global speed_balken
  9. global balken
  10. global text, text_rect
  11. speed_image=[1,1]
  12. speed_balken=[0,0]
  13. width=320
  14. height=240
  15. size= (width,height)
  16. black=(0,0,0)
  17. red=(255,0,0)
  18. white=(255,255,255)
  19. image=pygame.image.load("ohm.png")
  20. image_rect=image.get_rect()
  21. balken=initbalken()
  22. font=pygame.font.Font(None,28)
  23. text= font.render('Game Over', 1, white)
  24. text_rect =text.get_rect()
  25. textinit(text_rect)
  26. def textinit(text_rect):
  27. text_rect.centerx=width/2
  28. text_rect.centery=height/2
  29. def initbalken():
  30. global x_balken
  31. global y_balken
  32. x_balken=25
  33. y_balken=200
  34. width = 50
  35. height = 20
  36. return pygame.Rect(x_balken,y_balken,width,height)
  37. def input():
  38. pygame.display.set_caption('PONG!')
  39. for event in pygame.event.get():
  40. if event.type ==pygame.KEYDOWN:
  41. if event.key==pygame.K_ESCAPE:
  42. return False
  43. return True
  44. def update():
  45. image_rect.x=100
  46. image_rect.y=50
  47. def draw_game():
  48. screen.fill(black)
  49. screen.blit(image, image_rect)
  50. pygame.draw.rect(screen, red, balken)
  51. pygame.display.flip()
  52. def draw_gameover():
  53. image_rect.bottom=0
  54. screen.fill(black)
  55. screen.blit(text, text_rect)
  56. pygame.display.flip()
  57. time.sleep(2)
  58. def movement_image():
  59. #prüft ob Image links o. rechts am Rand kollidiert
  60. if image_rect.left < 0 or image_rect.right > width:
  61. speed_image[0] = -speed_image[0]
  62. #prüft ob Image oben am Rand kollidiert
  63. if image_rect.top <0:
  64. speed_image[1] = - speed_image[1]
  65. #prüft ob Kollidierung der Rechtecke stattfindet
  66. if pygame.Rect.colliderect(image_rect,balken):
  67. speed_image[1] = - speed_image[1]
  68. def movement_balken():
  69. speed_balken[0]=0
  70. keys=pygame.key.get_pressed()
  71. if keys[pygame.K_a]:
  72. speed_balken[0]= -1
  73. if keys[pygame.K_d]:
  74. speed_balken[0]= 1
  75. if balken.left == 0:
  76. balken.left=1
  77. if balken.right == width:
  78. balken.right = width -1
  79. return
  80. pygame.init()
  81. init()
  82. screen=pygame.display.set_mode(size)
  83. running = True
  84. while running:
  85. running =input()
  86. image_rect = image_rect.move(speed_image)
  87. balken=balken.move(speed_balken)
  88. movement_image()
  89. movement_balken()
  90. if image_rect.bottom < (height-1):
  91. draw_game()
  92. else:
  93. running= False
  94. time.sleep(0.01)
  95. draw_gameover()