Timer added
This commit is contained in:
@@ -0,0 +1,154 @@
|
||||
from pygame import *
|
||||
import random
|
||||
|
||||
init()
|
||||
|
||||
running = False
|
||||
|
||||
width, height = 1920, 1000
|
||||
screen = display.set_mode((width, height))
|
||||
display.set_caption("Game")
|
||||
|
||||
clock = time.Clock()
|
||||
font = font.SysFont(None, 40)
|
||||
|
||||
white = (255,255,255)
|
||||
blue = (0,0,255)
|
||||
red = (255,0,0)
|
||||
green = (0,255,0)
|
||||
black = (0, 0, 0)
|
||||
|
||||
border = Rect(0, 0 , width, height)
|
||||
|
||||
p_size = 100
|
||||
speed = 10
|
||||
|
||||
points_p1 = 0
|
||||
points_p2 = 0
|
||||
|
||||
|
||||
c_r = 25
|
||||
c_d = c_r * 2
|
||||
c_x = random.randint(0, width - c_d)
|
||||
c_y = random.randint(0, height - c_d)
|
||||
|
||||
c = Rect(c_x, c_y, c_d, c_d)
|
||||
|
||||
|
||||
p1 = Rect(100, 200, p_size, p_size)
|
||||
p2 = Rect(600, 250, p_size, p_size)
|
||||
|
||||
|
||||
timer_sec = 120
|
||||
timer_bool = True
|
||||
|
||||
def show_score(score_p1, score_p2):
|
||||
score_text = font.render(
|
||||
f"Punkte P1: {score_p1} Punkte P2: {score_p2}",
|
||||
True,
|
||||
black
|
||||
)
|
||||
screen.blit(score_text, (10, 10))
|
||||
|
||||
def show_timer():
|
||||
timer_text = font.render(f"Zeit: {int(timer_sec)}", True, black)
|
||||
screen.blit(timer_text, (10, 40))
|
||||
|
||||
def game():
|
||||
global running, points_p1, points_p2, c_x, c_y, c, timer_sec
|
||||
|
||||
while running:
|
||||
for e in event.get():
|
||||
if e.type == QUIT:
|
||||
running = False
|
||||
print("Programm closed by user input")
|
||||
|
||||
keys = key.get_pressed()
|
||||
|
||||
|
||||
if keys[K_LEFT]:
|
||||
p1.x -= speed
|
||||
if keys[K_RIGHT]:
|
||||
p1.x += speed
|
||||
if keys[K_DOWN]:
|
||||
p1.y += speed
|
||||
if keys[K_UP]:
|
||||
p1.y -= speed
|
||||
|
||||
|
||||
if keys[K_a]:
|
||||
p2.x -= speed
|
||||
if keys[K_d]:
|
||||
p2.x += speed
|
||||
if keys[K_s]:
|
||||
p2.y += speed
|
||||
if keys[K_w]:
|
||||
p2.y -= speed
|
||||
|
||||
p1.clamp_ip(border)
|
||||
p2.clamp_ip(border)
|
||||
|
||||
|
||||
if p1.colliderect(p2):
|
||||
p1.topleft = (
|
||||
random.randint(0, width - p_size),
|
||||
random.randint(0, height - p_size)
|
||||
)
|
||||
p2.topleft = (
|
||||
random.randint(0, width - p_size),
|
||||
random.randint(0, height - p_size)
|
||||
)
|
||||
|
||||
|
||||
if p1.colliderect(c):
|
||||
c_x = random.randint(0, width - c_d)
|
||||
c_y = random.randint(0, height - c_d)
|
||||
c = Rect(c_x, c_y, c_d, c_d)
|
||||
points_p1 += 1
|
||||
|
||||
if p2.colliderect(c):
|
||||
c_x = random.randint(0, width - c_d)
|
||||
c_y = random.randint(0, height - c_d)
|
||||
c = Rect(c_x, c_y, c_d, c_d)
|
||||
points_p2 += 1
|
||||
|
||||
|
||||
timer_sec -= clock.get_time() / 1000
|
||||
if timer_sec <= 0:
|
||||
running = False
|
||||
|
||||
# Zeichnen
|
||||
screen.fill(white)
|
||||
draw.rect(screen, blue, p1)
|
||||
draw.rect(screen, green, p2)
|
||||
|
||||
|
||||
draw.circle(screen, red, (c_x + c_r, c_y + c_r), c_r)
|
||||
|
||||
show_score(points_p1, points_p2)
|
||||
|
||||
if timer_bool:
|
||||
show_timer()
|
||||
|
||||
display.flip()
|
||||
clock.tick(60)
|
||||
|
||||
screen.fill(white)
|
||||
end_text = font.render(
|
||||
f"Ende! P1: {points_p1} | P2: {points_p2}",
|
||||
True,
|
||||
black
|
||||
)
|
||||
screen.blit(end_text, (width // 2 - 200, height // 2))
|
||||
display.flip()
|
||||
time.wait(7000)
|
||||
|
||||
def start():
|
||||
global running
|
||||
print("Starting Game")
|
||||
running = True
|
||||
game()
|
||||
print("Game ended")
|
||||
|
||||
start()
|
||||
quit()
|
||||
Reference in New Issue
Block a user