MEIM/pong.py
2018-10-13 08:26:48 +02:00

113 lines
2.5 KiB
Python

import pygame
# init Spieldaten
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 game_over():
global image_g_o
screen.blit(image_g_o, (0, 0))
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):
game_over()
def draw():
screen.fill(black)
screen.blit(image, image_rect)
pygame.draw.rect(screen, red, rect)
pygame.display.flip()
pygame.init()
init()
initRectangle()
screen = pygame.display.set_mode(size)
running = True
while running:
running = input()
update()
draw()