Compare commits

...

1 Commits

Author SHA1 Message Date
Ulad
302d178697 refactor: Code structure and variable nameing 2023-03-18 14:37:33 +01:00
2 changed files with 69 additions and 63 deletions

67
src/game.py Normal file
View File

@ -0,0 +1,67 @@
import pygame
def game_init():
pygame.init()
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
start_pos = [300, 300]
chnage_pos = [0, 0]
game_over = False
blocks = [[100, 20], [100, 500], [700, 20], [700, 500]]
clock = pygame.time.Clock()
dis = pygame.display.set_mode((800, 600))
pygame.display.set_caption('Intelegentny Pszczelarz')
game_env = dict(start_pos=start_pos, chnage_pos=chnage_pos, game_over=game_over, blocks=blocks, clock=clock,
white=white, red=red, black=black, dis=dis) # change it later
return game_env
def game_start(game_env):
pos = game_env["start_pos"]
chnage_pos = game_env["chnage_pos"]
game_over = game_env["game_over"]
blocks = game_env["blocks"]
clock = game_env["clock"]
white = game_env["white"]
red = game_env["red"]
black = game_env["black"]
dis = game_env["dis"]
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
chnage_pos = [-10, 0]
elif event.key == pygame.K_RIGHT:
chnage_pos = [10, 0]
elif event.key == pygame.K_UP:
chnage_pos = [0, -10]
elif event.key == pygame.K_DOWN:
chnage_pos = [0, 10]
pos = [pos[0] + chnage_pos[0], pos[1] + chnage_pos[1]]
dis.fill(white)
pygame.draw.rect(dis, black, [pos[0], pos[1], 10, 10])
if pos in blocks:
game_over = True
pygame.draw.rect(dis, red, [blocks[0][0], blocks[0][1], 20, 20])
pygame.draw.rect(dis, red, [blocks[1][0], blocks[1][1], 20, 20])
pygame.draw.rect(dis, red, [blocks[2][0], blocks[2][1], 20, 20])
pygame.draw.rect(dis, red, [blocks[3][0], blocks[3][1], 20, 20])
pygame.display.update()
clock.tick(30)
pygame.quit()

View File

@ -1,65 +1,4 @@
import pygame
import game
pygame.init()
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
dis = pygame.display.set_mode((800, 600))
pygame.display.set_caption('Intelegentny Pszczelarz')
game_over = False
x1 = 300
y1 = 300
x1_change = 0
y1_change = 0
block_xy = [[100, 20], [100, 500], [700, 20], [700, 500]]
block1 = block_xy[0]
block2 = block_xy[1]
block3 = block_xy[2]
block4 = block_xy[3]
clock = pygame.time.Clock()
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x1_change = -10
y1_change = 0
elif event.key == pygame.K_RIGHT:
x1_change = 10
y1_change = 0
elif event.key == pygame.K_UP:
y1_change = -10
x1_change = 0
elif event.key == pygame.K_DOWN:
y1_change = 10
x1_change = 0
x1 += x1_change
y1 += y1_change
dis.fill(white)
pygame.draw.rect(dis, black, [x1, y1, 10, 10])
if [x1,y1] in block_xy:
game_over = True
pygame.draw.rect(dis, red, [block1[0], block1[1], 20, 20])
pygame.draw.rect(dis, red, [block2[0], block2[1], 20, 20])
pygame.draw.rect(dis, red, [block3[0], block3[1], 20, 20])
pygame.draw.rect(dis, red, [block4[0], block4[1], 20, 20])
pygame.display.update()
clock.tick(30)
pygame.quit()
quit()
game.game_start(game.game_init())