123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- import pygame
- import random
- import time
-
- running = False
- gameover = False
-
-
- def initScreen():
- global black, red, white, screen, width, height
- width = 640
- height = 400
- size = (width, height)
-
- screen = pygame.display.set_mode(size)
-
- white = (255, 255, 255)
- black = (0,0,0)
- red = (255,0,0)
-
- pygame.display.set_caption("PONG!")
-
-
-
- def initImage():
- global image, image_rect
- image = pygame.image.load("seehofer.png")
- image_rect = image.get_rect()
- image_rect.centerx = width/2
- image_rect.centery = height/2
- global size_image
- size_image = image.get_rect().size
-
-
- def initImageMovement():
- random.seed(a=None, version=2)
- rand_x = random.randint(1, 4)
- random.seed(a=None, version=2)
- rand_y = random.randint(1, 4)
- global move_vector
- move_vector = [ rand_x, rand_y ]
-
- class Rectangle(pygame.Rect):
- def __init__(self, rect_width, rect_height):
- self.width = rect_width
- self.height = rect_height
- self.centerx = width/2
- self.centery = height - (rect_height/2)
- self.speed_x = 0
-
-
- def initRedRectangle():
- global red_rect
- red_rect = Rectangle(100, 12)
-
-
- def doMovement():
- time.sleep(0.01)
- image_rect.centerx += move_vector[0]
- image_rect.centery += move_vector[1]
-
- if size_image[0]/2 <= image_rect.centerx <= width-(size_image[0]/2) and size_image[1]/2 <= image_rect.centery <= height-(size_image[1]/2):
-
- pass
- else:
- if image_rect.centerx >= width-size_image[0]/2 or image_rect.centerx <= size_image[0]/2:
- # CASE 1: image bounces on the right/left wall
- move_vector[0] = -(move_vector[0])
- elif image_rect.centery <= size_image[1]/2:
- # CASE 2: image bounces on the upper wall
- move_vector[1] = -(move_vector[1])
- elif image_rect.centery >= height-size_image[1]/2:
- # CASE 3: image is about to pass the lower border of screen
- if image_rect.colliderect(red_rect):
- # CASE 3.1: image touches the red balk
- move_vector[1] = -(move_vector[1])
- if not image_rect.colliderect(red_rect):
- # CASE 3.2: image does not touch the red balk
- showGameOver()
- global gameover
- gameover = True
-
- else:
- # CASE 4: image bounces in one of the upper corners (right or left)
- move_vector[0] = -(move_vector[0])
- move_vector[1] = -(move_vector[1])
-
- def moveRedBalk():
- if (red_rect.width/2) <= red_rect.centerx <= width-(red_rect.width/2):
- red_rect.centerx += red_rect.speed_x * 3
- elif red_rect.centerx <= (red_rect.width/2) or red_rect.centerx >= width-(red_rect.width/2):
- red_rect.speed_x = -(red_rect.speed_x)
- red_rect.centerx += red_rect.speed_x * 3
-
- def showGameOver():
- global text, gameover_rect
- font = pygame.font.Font(None, 38)
- text = font.render("Game Over!", 1, white)
- gameover_rect = text.get_rect()
-
- gameover_rect.centerx = width/2
- gameover_rect.centery = height/2
-
-
-
- def input():
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- return
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_ESCAPE:
- return False
- if event.key == pygame.K_RIGHT:
- red_rect.speed_x = 1
- if event.key == pygame.K_LEFT:
- red_rect.speed_x = -1
- if event.type == pygame.KEYUP:
- red_rect.speed_x = 0
- return True
-
-
-
- def update():
- doMovement()
- moveRedBalk()
-
-
-
- def draw():
- screen.fill(black)
- if gameover:
- screen.blit(text, gameover_rect)
- else:
- screen.blit(image, image_rect)
- pygame.draw.rect(screen, red, red_rect)
- pygame.display.flip()
-
-
-
- #------------- MAIN PROGRAMM ----------------
-
- pygame.init()
- initScreen()
- initImage()
- initImageMovement()
- initRedRectangle()
- running = True
-
-
- #---------------- GAME LOOP ------------------
-
- while running:
- running = input()
- update()
- draw()
-
-
-
|