Fertiges PONG-Spiel (Beta-Version zu finden unter "Tag2_EigenesSpiel.py")
This commit is contained in:
parent
95b84517e8
commit
fbffbfe332
159
Pong.py
Normal file
159
Pong.py
Normal file
@ -0,0 +1,159 @@
|
|||||||
|
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()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
91
Tag2_EigenesSpiel.py
Normal file
91
Tag2_EigenesSpiel.py
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
import pygame
|
||||||
|
|
||||||
|
def initScreen():
|
||||||
|
global width, height, size
|
||||||
|
width = 820
|
||||||
|
height = 640
|
||||||
|
size = (width, height)
|
||||||
|
|
||||||
|
global red, black
|
||||||
|
red = (255, 0, 0, 0)
|
||||||
|
black = (0, 0, 0, 0)
|
||||||
|
|
||||||
|
def initGame():
|
||||||
|
global Rectangle
|
||||||
|
Rectangle = initRectangle()
|
||||||
|
global running
|
||||||
|
running = False
|
||||||
|
|
||||||
|
def initSign():
|
||||||
|
global image, image_rect
|
||||||
|
image = pygame.image.load("seehofer.png")
|
||||||
|
image_rect = image.get_rect()
|
||||||
|
|
||||||
|
class Rectangle(pygame.Rect):
|
||||||
|
def __init__(self, x, y, width, height):
|
||||||
|
self.x = x
|
||||||
|
self.y = y
|
||||||
|
self.width = width
|
||||||
|
self.height = height
|
||||||
|
|
||||||
|
self.speed_l = 0
|
||||||
|
self.speed_r = 0
|
||||||
|
|
||||||
|
def initRectangle():
|
||||||
|
x = 0
|
||||||
|
y = 620
|
||||||
|
width = 100
|
||||||
|
height = 20
|
||||||
|
|
||||||
|
global My_Rect
|
||||||
|
My_Rect = Rectangle(x, y, width, height)
|
||||||
|
return My_Rect
|
||||||
|
|
||||||
|
def input():
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
return False
|
||||||
|
if event.type == pygame.KEYDOWN:
|
||||||
|
if event.key == pygame.K_ESCAPE:
|
||||||
|
return False
|
||||||
|
if event.key == pygame.K_LEFT:
|
||||||
|
My_Rect.speed_l = 1
|
||||||
|
if event.key == pygame.K_RIGHT:
|
||||||
|
My_Rect.speed_r = 1
|
||||||
|
if event.type == pygame.KEYUP:
|
||||||
|
Rectangle.speed_left = 0
|
||||||
|
Rectangle.speed_right = 0
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
def update():
|
||||||
|
if(0 <= Rectangle.x < width):
|
||||||
|
Rectangle.x += My_Rect.speed_r * 3
|
||||||
|
Rectangle.x += My_Rect.speed_l * 3
|
||||||
|
|
||||||
|
image_rect.x = 100
|
||||||
|
image_rect.y = 50
|
||||||
|
|
||||||
|
def draw():
|
||||||
|
screen.fill(black)
|
||||||
|
screen.blit(image, image_rect)
|
||||||
|
pygame.draw.rect(screen, red, Rectangle)
|
||||||
|
pygame.display.flip()
|
||||||
|
|
||||||
|
|
||||||
|
# --------- Start of Main-Code ------------
|
||||||
|
running = False
|
||||||
|
|
||||||
|
pygame.init()
|
||||||
|
initScreen()
|
||||||
|
initGame()
|
||||||
|
initSign()
|
||||||
|
screen = pygame.display.set_mode(size)
|
||||||
|
|
||||||
|
running = True
|
||||||
|
|
||||||
|
|
||||||
|
while running == True:
|
||||||
|
running = input()
|
||||||
|
update()
|
||||||
|
draw()
|
Loading…
x
Reference in New Issue
Block a user