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

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import pygame
  2. # init Spieldaten
  3. speed = 0
  4. def init():
  5. global width, height, size
  6. global speed_x, speed_y
  7. speed_x = 1
  8. speed_y = 1
  9. global black, red
  10. global image, image_rect
  11. width = 1080
  12. height = 720
  13. size = (width, height)
  14. black = (0, 0, 0)
  15. red = (255, 0, 0)
  16. image = pygame.image.load("ohm.png")
  17. image_rect = image.get_rect()
  18. image_rect.x = 100
  19. image_rect.y = 50
  20. def initRectangle():
  21. global rect
  22. x = 400
  23. y = 650
  24. width = 300
  25. height = 40
  26. rect = pygame.Rect(x, y, width, height)
  27. def input():
  28. global speed
  29. for event in pygame.event.get():
  30. if event.type == pygame.QUIT:
  31. return False
  32. if event.type == pygame.KEYDOWN:
  33. if event.key == pygame.K_LEFT:
  34. speed = -1
  35. if event.key == pygame.K_RIGHT:
  36. speed = 1
  37. if event.type == pygame.KEYUP:
  38. if event.key == pygame.K_LEFT:
  39. speed = 0
  40. if event.key == pygame.K_RIGHT:
  41. speed = 0
  42. return True
  43. def update():
  44. rect.x = rect.x + speed
  45. #if image_rect.x == 0
  46. image_rect.x += speed_x
  47. image_rect.y += speed_y
  48. def draw():
  49. screen.fill(black)
  50. screen.blit(image, image_rect)
  51. pygame.draw.rect(screen, red, rect)
  52. pygame.display.flip()
  53. pygame.init()
  54. init()
  55. initRectangle()
  56. screen = pygame.display.set_mode(size)
  57. running = True
  58. while running:
  59. running = input()
  60. update()
  61. draw()