import pygame # init Spieldaten pygame.init() myfont = pygame.font.SysFont('Comic Sans MS', 40) global running speed = 0 def init(): global width, height, size, zaehler global speed_x, speed_y, image_g_o zaehler = 0 speed_x = -1 speed_y = -1 global black, red global image, image_rect width = 1080 height = 720 size = (width, height) black = (0, 0, 0) red = (255, 0, 0) image = pygame.image.load("ohm.png") image_g_o = pygame.image.load("color-me-happy-game-over.jpg") image_rect = image.get_rect() image_rect.x = 500 image_rect.y = 350 def initRectangle(): global rect, width_player, height_player, x, y x = 0 y = 650 width_player = 300 height_player = 40 rect = pygame.Rect(x, y, width_player, height_player) def input(): global speed for event in pygame.event.get(): if event.type == pygame.QUIT: return False if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: speed = -1 if event.key == pygame.K_RIGHT: speed = 1 if event.type == pygame.KEYUP: if event.key == pygame.K_LEFT: speed = 0 if event.key == pygame.K_RIGHT: speed = 0 if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: return False return True def reflect_x(): global speed_x speed_x *= -1 def reflect_y(): global speed_y speed_y *= -1 def show_game_over(): global image_g_o, screen, running screen.blit(image_g_o, (300, 100)) textsurface = myfont.render('PONG', True, (0, 50, 200)) screen.blit(textsurface, (500,80)) textsurface = myfont.render('Press Space to restart - Escape to exit', True, (0, 50, 200)) screen.blit(textsurface, (200, 500)) pygame.display.flip() waiting = True while waiting: for event in pygame.event.get(): if (event.type == pygame.QUIT): print('quit') waiting = False pygame.quit() running = False if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: print('space') waiting = False init_game() if event.key == pygame.K_ESCAPE: running = False pygame.quit() def update(): global x, y, width_player, height_player, speed, width, zaehler, height if (speed == -1 and rect.x <= 0) or (speed == 1 and rect.x >= (width - width_player)): pass else : rect.x = rect.x + speed if (zaehler%2 == 0) and (image_rect.x <= 0 or image_rect.x >= width) : reflect_x() elif (zaehler%2 == 0) and image_rect.y <= 0 : reflect_y() 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)) : reflect_y() zaehler = zaehler + 1 if (zaehler%2 == 0): image_rect.x += speed_x image_rect.y += speed_y else: pass if image_rect.y >= height and (zaehler%2 == 0): show_game_over() def draw(): screen.fill(black) screen.blit(image, image_rect) pygame.draw.rect(screen, red, rect) pygame.display.flip() def init_game(): global screen init() initRectangle() screen = pygame.display.set_mode(size) init_game() running = True game_over = False while running: if game_over: show_game_over() running = input() update() draw()