|
|
@@ -0,0 +1,140 @@ |
|
|
|
import pygame |
|
|
|
import time |
|
|
|
|
|
|
|
|
|
|
|
# Initialisierung der Spieldaten |
|
|
|
def init(): |
|
|
|
global width, height, size, black, red |
|
|
|
global image, image_rect |
|
|
|
global speed_image |
|
|
|
global speed_balken |
|
|
|
global balken |
|
|
|
global text, text_rect |
|
|
|
speed_image=[1,1] |
|
|
|
speed_balken=[0,0] |
|
|
|
width=320 |
|
|
|
height=240 |
|
|
|
size= (width,height) |
|
|
|
|
|
|
|
black=(0,0,0) |
|
|
|
red=(255,0,0) |
|
|
|
white=(255,255,255) |
|
|
|
|
|
|
|
image=pygame.image.load("ohm.png") |
|
|
|
image_rect=image.get_rect() |
|
|
|
balken=initbalken() |
|
|
|
|
|
|
|
font=pygame.font.Font(None,28) |
|
|
|
text= font.render('Game Over', 1, white) |
|
|
|
text_rect =text.get_rect() |
|
|
|
textinit(text_rect) |
|
|
|
|
|
|
|
def textinit(text_rect): |
|
|
|
text_rect.centerx=width/2 |
|
|
|
text_rect.centery=height/2 |
|
|
|
|
|
|
|
def initbalken(): |
|
|
|
global x_balken |
|
|
|
global y_balken |
|
|
|
x_balken=25 |
|
|
|
y_balken=200 |
|
|
|
width = 50 |
|
|
|
height = 20 |
|
|
|
return pygame.Rect(x_balken,y_balken,width,height) |
|
|
|
|
|
|
|
|
|
|
|
def input(): |
|
|
|
pygame.display.set_caption('PONG!') |
|
|
|
for event in pygame.event.get(): |
|
|
|
if event.type ==pygame.KEYDOWN: |
|
|
|
if event.key==pygame.K_ESCAPE: |
|
|
|
return False |
|
|
|
|
|
|
|
return True |
|
|
|
|
|
|
|
def update(): |
|
|
|
image_rect.x=100 |
|
|
|
image_rect.y=50 |
|
|
|
|
|
|
|
def draw_game(): |
|
|
|
|
|
|
|
screen.fill(black) |
|
|
|
screen.blit(image, image_rect) |
|
|
|
pygame.draw.rect(screen, red, balken) |
|
|
|
pygame.display.flip() |
|
|
|
|
|
|
|
|
|
|
|
def draw_gameover(): |
|
|
|
image_rect.bottom=0 |
|
|
|
screen.fill(black) |
|
|
|
screen.blit(text, text_rect) |
|
|
|
pygame.display.flip() |
|
|
|
time.sleep(2) |
|
|
|
|
|
|
|
|
|
|
|
def movement_image(): |
|
|
|
|
|
|
|
|
|
|
|
#prüft ob Image links o. rechts am Rand kollidiert |
|
|
|
|
|
|
|
if image_rect.left < 0 or image_rect.right > width: |
|
|
|
speed_image[0] = -speed_image[0] |
|
|
|
|
|
|
|
#prüft ob Image oben am Rand kollidiert |
|
|
|
|
|
|
|
if image_rect.top <0: |
|
|
|
speed_image[1] = - speed_image[1] |
|
|
|
|
|
|
|
#prüft ob Kollidierung der Rechtecke stattfindet |
|
|
|
|
|
|
|
if pygame.Rect.colliderect(image_rect,balken): |
|
|
|
speed_image[1] = - speed_image[1] |
|
|
|
|
|
|
|
|
|
|
|
def movement_balken(): |
|
|
|
|
|
|
|
speed_balken[0]=0 |
|
|
|
|
|
|
|
keys=pygame.key.get_pressed() |
|
|
|
|
|
|
|
if keys[pygame.K_a]: |
|
|
|
speed_balken[0]= -1 |
|
|
|
|
|
|
|
if keys[pygame.K_d]: |
|
|
|
speed_balken[0]= 1 |
|
|
|
|
|
|
|
|
|
|
|
if balken.left == 0: |
|
|
|
balken.left=1 |
|
|
|
|
|
|
|
if balken.right == width: |
|
|
|
balken.right = width -1 |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pygame.init() |
|
|
|
init() |
|
|
|
screen=pygame.display.set_mode(size) |
|
|
|
|
|
|
|
running = True |
|
|
|
|
|
|
|
while running: |
|
|
|
running =input() |
|
|
|
image_rect = image_rect.move(speed_image) |
|
|
|
balken=balken.move(speed_balken) |
|
|
|
movement_image() |
|
|
|
movement_balken() |
|
|
|
|
|
|
|
|
|
|
|
if image_rect.bottom < (height-1): |
|
|
|
draw_game() |
|
|
|
|
|
|
|
else: |
|
|
|
running= False |
|
|
|
|
|
|
|
time.sleep(0.01) |
|
|
|
|
|
|
|
draw_gameover() |