91 lines
1.7 KiB
Python
91 lines
1.7 KiB
Python
from pygame import *
|
|
import random
|
|
|
|
init()
|
|
|
|
width, height = 1920, 1080
|
|
screen = display.set_mode((width, height))
|
|
display.set_caption("Game")
|
|
|
|
clock = time.Clock()
|
|
game_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)
|
|
grey = (180, 180, 180)
|
|
|
|
border = Rect(0, 0 , width, height)
|
|
|
|
p_size = 100
|
|
|
|
speed = 10
|
|
|
|
|
|
|
|
c_r = 25
|
|
c_x = random.randint(c_r, width - c_r)
|
|
c_x = random.randint(c_r, height - c_r)
|
|
c_visible = True
|
|
time = 0
|
|
|
|
p1 = Rect(100, 200, p_size, p_size)
|
|
p2 = Rect(600, 250, p_size, p_size)
|
|
|
|
p1.x = 100
|
|
p1.y = 200
|
|
|
|
p2.x = 600
|
|
p2.y = 250
|
|
|
|
running = True
|
|
|
|
while running:
|
|
for e in event.get():
|
|
if e.type == QUIT:
|
|
running = False
|
|
print("Programm closed by user input")
|
|
# if e.type == MOUSEBUTTONDOWN:
|
|
# m_x, m_y = mouse.get_pos()
|
|
# dx = m_x - c_x
|
|
# dy = m_y - c_y
|
|
# if dx * dx + dy*dy <= c_r * c_r:
|
|
# c_color = green if c_color == red else red
|
|
|
|
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))
|
|
|
|
|
|
screen.fill(white)
|
|
draw.rect(screen, blue, p1)
|
|
draw.rect(screen, green, p2)
|
|
display.flip()
|
|
clock.tick(60)
|
|
|
|
|
|
quit() |