initial commit
This commit is contained in:
commit
39f56a3b10
83 files changed
+3583
No files matched your search
@@ -0,0 +1,45 @@
|
||||
import pygame
|
||||
|
||||
from Prog_1.Pygame_games.first_games import draw_game, update_game
|
||||
|
||||
width = 800
|
||||
height = 600
|
||||
SIZE = (width, height)
|
||||
|
||||
def main():
|
||||
screen = init_game()
|
||||
game_loop(screen)
|
||||
exit_game()
|
||||
|
||||
def init_game():
|
||||
pygame.init()
|
||||
screen = pygame.display.set_mode(SIZE)
|
||||
return screen
|
||||
|
||||
def game_loop(screen):
|
||||
while True:
|
||||
if event_handling() == False:
|
||||
break
|
||||
if update_game() == False:
|
||||
break
|
||||
event = event_handling()
|
||||
update_game()
|
||||
draw_game(screen)
|
||||
|
||||
def event_handling():
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
return False
|
||||
return True
|
||||
|
||||
def update_game():
|
||||
return True
|
||||
|
||||
def draw_screen(screen):
|
||||
screen.fill("BLACK")
|
||||
pygame.display.flip()
|
||||
|
||||
def exit_game():
|
||||
pygame.quit()
|
||||
|
||||
main()
|
||||
@@ -0,0 +1,58 @@
|
||||
import pygame
|
||||
|
||||
WIDTH = 800
|
||||
HEIGHT= 600
|
||||
SIZE = (WIDTH, HEIGHT)
|
||||
RED = (255, 0, 0)
|
||||
CIRCLE_RADIUS = 10
|
||||
BLACK = (0, 0, 0)
|
||||
FPS = 60
|
||||
|
||||
def main():
|
||||
screen = init_game()
|
||||
game_loop(screen)
|
||||
exit_game()
|
||||
|
||||
def init_game():
|
||||
global position
|
||||
global clock
|
||||
clock = pygame.time.Clock()
|
||||
position = (WIDTH // 2, HEIGHT // 2)
|
||||
pygame.init()
|
||||
return pygame.display.set_mode(SIZE)
|
||||
|
||||
def game_loop(screen):
|
||||
while True:
|
||||
if not event_handling():
|
||||
break
|
||||
if not update_game():
|
||||
break
|
||||
event = event_handling()
|
||||
draw_game(screen)
|
||||
clock.tick(FPS)
|
||||
|
||||
def event_handling():
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
return False
|
||||
return True
|
||||
|
||||
def update_game():
|
||||
global new_pos
|
||||
mouse_pos = pygame.mouse.get_pos()
|
||||
delta_x = mouse_pos[0] - position[0]
|
||||
delta_y = mouse_pos[1] - position[1]
|
||||
acceleration = delta_x / WIDTH, delta_y / HEIGHT
|
||||
new_pos = (mouse_pos[0] + acceleration[0], mouse_pos[1] + acceleration[1])
|
||||
clock.tick(FPS)
|
||||
return True
|
||||
|
||||
def draw_game(screen):
|
||||
screen.fill(BLACK)
|
||||
pygame.draw.circle(screen, RED, new_pos, CIRCLE_RADIUS)
|
||||
pygame.display.flip()
|
||||
|
||||
def exit_game():
|
||||
pygame.quit()
|
||||
|
||||
main()
|
||||
@@ -0,0 +1,61 @@
|
||||
import pygame
|
||||
|
||||
WIDTH = 800
|
||||
HEIGHT= 600
|
||||
SIZE = (WIDTH, HEIGHT)
|
||||
RED = (255, 0, 0)
|
||||
CIRCLE_RADIUS = 10
|
||||
BLACK = (0, 0, 0)
|
||||
FPS = 120
|
||||
|
||||
def main():
|
||||
screen = init_game()
|
||||
game_loop(screen)
|
||||
exit_game()
|
||||
|
||||
def init_game():
|
||||
global position
|
||||
global clock
|
||||
clock = pygame.time.Clock()
|
||||
position = (WIDTH // 2, HEIGHT // 2)
|
||||
pygame.init()
|
||||
return pygame.display.set_mode(SIZE)
|
||||
|
||||
def game_loop(screen):
|
||||
while True:
|
||||
if not event_handling():
|
||||
break
|
||||
if not update_game():
|
||||
break
|
||||
event = event_handling()
|
||||
draw_game(screen)
|
||||
clock.tick(FPS)
|
||||
|
||||
def event_handling():
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
return False
|
||||
return True
|
||||
|
||||
def update_game():
|
||||
global position
|
||||
keys_pressed = pygame.key.get_pressed()
|
||||
if keys_pressed[pygame.K_UP]:
|
||||
position = (position[0], position[1] - 1)
|
||||
if keys_pressed[pygame.K_DOWN]:
|
||||
position = (position[0], position[1] + 1)
|
||||
if keys_pressed[pygame.K_LEFT]:
|
||||
position = (position[0] - 1, position[1])
|
||||
if keys_pressed[pygame.K_RIGHT]:
|
||||
position = (position[0] + 1, position[1])
|
||||
return True
|
||||
|
||||
def draw_game(screen):
|
||||
screen.fill(BLACK)
|
||||
pygame.draw.circle(screen, RED, position, CIRCLE_RADIUS)
|
||||
pygame.display.flip()
|
||||
|
||||
def exit_game():
|
||||
pygame.quit()
|
||||
|
||||
main()
|
||||
@@ -0,0 +1,121 @@
|
||||
import random
|
||||
|
||||
import pygame
|
||||
|
||||
|
||||
class Cell:
|
||||
def __init__(self,pos,size):
|
||||
self.pos = pos
|
||||
self.size = size
|
||||
|
||||
def grow(self):
|
||||
self.size = self.size + 1
|
||||
|
||||
def movement(self):
|
||||
keys_pressed = pygame.key.get_pressed()
|
||||
if keys_pressed[pygame.K_UP]:
|
||||
pos = (cell.pos[0], cell.pos[1] - 1)
|
||||
self.pos = pos
|
||||
if keys_pressed[pygame.K_DOWN]:
|
||||
pos = (cell.pos[0], cell.pos[1] + 1)
|
||||
self.pos = pos
|
||||
if keys_pressed[pygame.K_LEFT]:
|
||||
pos = (cell.pos[0] - 1, cell.pos[1])
|
||||
self.pos = pos
|
||||
if keys_pressed[pygame.K_RIGHT]:
|
||||
pos = (cell.pos[0] + 1, cell.pos[1])
|
||||
self.pos = pos
|
||||
|
||||
def appearance(self,screen):
|
||||
pygame.draw.circle(screen, RED, self.pos, self.size)
|
||||
|
||||
|
||||
class Nutrient:
|
||||
def __init__(self,visible,pos,size):
|
||||
self.visible = visible
|
||||
self.pos = pos
|
||||
self.size = size
|
||||
|
||||
def appear(self,screen):
|
||||
pygame.draw.circle(screen, BLUE, self.pos, CIRCLE_RADIUS)
|
||||
|
||||
def consumed(self,pos):
|
||||
del self
|
||||
del nutrients[pos]
|
||||
cell.grow()
|
||||
|
||||
def check_collision(self):
|
||||
if ((cell.pos[0] - self.pos[0]) ** 2) + ((cell.pos[1] - self.pos[1]) ** 2) <= (cell.size + self.size) ** 2:
|
||||
self.consumed(self.pos)
|
||||
|
||||
nutrients = {}
|
||||
width = 640
|
||||
height = 600
|
||||
SIZE = (width, height)
|
||||
WHITE = (255, 255, 255)
|
||||
RED = (255, 0, 0)
|
||||
BLUE = (0, 0, 255)
|
||||
CIRCLE_RADIUS = 2
|
||||
BLACK = (0, 0, 0)
|
||||
|
||||
def main():
|
||||
screen = init_game()
|
||||
game_loop(screen)
|
||||
exit_game()
|
||||
|
||||
def init_game():
|
||||
global clock
|
||||
clock = pygame.time.Clock()
|
||||
pygame.init()
|
||||
screen = pygame.display.set_mode(SIZE)
|
||||
return screen
|
||||
|
||||
def game_loop(screen):
|
||||
global cell
|
||||
cell = Cell((300,300),5)
|
||||
spawn_nutrition()
|
||||
while True:
|
||||
if not event_handling():
|
||||
break
|
||||
if not update_game():
|
||||
break
|
||||
event = event_handling()
|
||||
update_game()
|
||||
draw_screen(screen)
|
||||
|
||||
|
||||
|
||||
def event_handling():
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
return False
|
||||
return True
|
||||
|
||||
def update_game():
|
||||
n_copy = nutrients.copy()
|
||||
for x,y in n_copy:
|
||||
nutrients[(x,y)].check_collision()
|
||||
del n_copy
|
||||
cell.movement()
|
||||
clock.tick(60)
|
||||
return True
|
||||
|
||||
def draw_screen(screen):
|
||||
screen.fill(WHITE)
|
||||
x = 0
|
||||
cell.appearance(screen)
|
||||
for x,y in nutrients:
|
||||
if nutrients[x,y].visible:
|
||||
nutrients[x,y].appear(screen)
|
||||
x+=1
|
||||
pygame.display.flip()
|
||||
|
||||
def spawn_nutrition():
|
||||
for _ in range(100):
|
||||
pos = (random.randint(0, SIZE[0]), random.randint(0, SIZE[1]))
|
||||
nutrients[pos]=(Nutrient(True, pos,2))
|
||||
|
||||
def exit_game():
|
||||
pygame.quit()
|
||||
|
||||
main()
|
||||
@@ -0,0 +1,87 @@
|
||||
import pygame
|
||||
|
||||
BLOCKSIZE = 20
|
||||
width = 800
|
||||
height = 600
|
||||
SIZE = (width, height)
|
||||
BLACK = (0, 0, 0)
|
||||
YELLOW = (255, 255, 0)
|
||||
RED = (255, 0, 0)
|
||||
|
||||
class Block:
|
||||
def __init__(self, grid_pos, visibility):
|
||||
self.grid_pos = grid_pos
|
||||
self.visibility = visibility
|
||||
def set_visible(self):
|
||||
self.visibility = True
|
||||
def unset_visible(self):
|
||||
self.visibility = False
|
||||
def draw(self,screen):
|
||||
block_shape = (self.grid_pos[0], self.grid_pos[1], BLOCKSIZE, BLOCKSIZE)
|
||||
pygame.draw.rect(screen, YELLOW, block_shape)
|
||||
pygame.draw.rect(screen, RED, block_shape, 1)
|
||||
|
||||
def main():
|
||||
global blocks
|
||||
blocks = {}
|
||||
screen = init_game()
|
||||
create_grid(screen)
|
||||
game_loop(screen)
|
||||
exit_game()
|
||||
|
||||
def init_game():
|
||||
pygame.init()
|
||||
screen = pygame.display.set_mode(SIZE)
|
||||
return screen
|
||||
|
||||
def game_loop(screen):
|
||||
while True:
|
||||
if not event_handling():
|
||||
break
|
||||
if not update_game():
|
||||
break
|
||||
event = event_handling()
|
||||
update_game()
|
||||
draw_game(screen)
|
||||
|
||||
def event_handling():
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
return False
|
||||
if event.type == pygame.MOUSEBUTTONUP:
|
||||
pos = pygame.mouse.get_pos()
|
||||
x = pos[0] // BLOCKSIZE * BLOCKSIZE
|
||||
y = pos[1] // BLOCKSIZE * BLOCKSIZE
|
||||
blocks[(x,y)].set_visible()
|
||||
if event.type == pygame.KEYDOWN:
|
||||
if event.key == pygame.K_LSHIFT:
|
||||
pos = pygame.mouse.get_pos()
|
||||
x = pos[0] // BLOCKSIZE * BLOCKSIZE
|
||||
y = pos[1] // BLOCKSIZE * BLOCKSIZE
|
||||
blocks[(x,y)].unset_visible()
|
||||
return True
|
||||
|
||||
|
||||
def update_game():
|
||||
return True
|
||||
|
||||
def draw_game(screen):
|
||||
screen.fill(BLACK)
|
||||
for x,y in blocks:
|
||||
if blocks[(x,y)].visibility:
|
||||
blocks[(x,y)].draw(screen)
|
||||
pygame.display.flip()
|
||||
|
||||
def create_grid(screen):
|
||||
y = 0
|
||||
for _ in range(SIZE[1]//BLOCKSIZE*BLOCKSIZE):
|
||||
x = 0
|
||||
for _ in range(SIZE[0]//BLOCKSIZE*BLOCKSIZE):
|
||||
blocks[(x,y)]= (Block ((x, y), False))
|
||||
x += BLOCKSIZE
|
||||
y += BLOCKSIZE
|
||||
|
||||
def exit_game():
|
||||
pygame.quit()
|
||||
|
||||
main()
|
||||
@@ -0,0 +1,33 @@
|
||||
|
||||
import pygame
|
||||
|
||||
|
||||
def init_game():
|
||||
pygame.init()
|
||||
screen = pygame.display.set_mode()
|
||||
pygame.display.set_caption("Innit Game")
|
||||
return screen
|
||||
|
||||
def game_loop(screen):
|
||||
while True:
|
||||
if not event_handling():
|
||||
break
|
||||
if not update_game():
|
||||
break
|
||||
draw_game(screen)
|
||||
|
||||
def exit_game():
|
||||
pygame.quit()
|
||||
|
||||
def event_handling():
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
return False
|
||||
return True
|
||||
|
||||
def update_game():
|
||||
return True
|
||||
|
||||
def draw_game(screen):
|
||||
screen.fill("BLACK")
|
||||
pygame.display.flip()
|
||||
@@ -0,0 +1,99 @@
|
||||
import pygame
|
||||
|
||||
|
||||
class Particle:
|
||||
def __init__(self,x,y):
|
||||
self.pos = (x,y)
|
||||
self.velocity = (0,0)
|
||||
self.accel = (0,0)
|
||||
self.surface = None
|
||||
|
||||
def set_surface(self,surface):
|
||||
self.surface = surface
|
||||
|
||||
def appear(self,screen):
|
||||
if self.surface is not None:
|
||||
pygame.draw.circle(screen, BLUE, self.pos, CIRCLE_RADIUS)
|
||||
blit_pos = (self.pos[0]-self.surface.get_width()/2, self.pos[1]-self.surface.get_height()/2)
|
||||
screen.blit(self.surface, blit_pos)
|
||||
|
||||
def update(self):
|
||||
self.velocity = (self.velocity[0]+self.accel[0], self.velocity[1]+self.accel[1])
|
||||
self.pos = (self.pos[0] + self.velocity[0], self.pos[1] + self.velocity[1])
|
||||
self.accel = (0,0)
|
||||
|
||||
def apply_force(self,force):
|
||||
self.accel = (self.accel[0]+force[0],self.accel[1]+force[1])
|
||||
|
||||
projectiles = {}
|
||||
width = 640
|
||||
height = 600
|
||||
SIZE = (width, height)
|
||||
FRICTION = 0.3
|
||||
WHITE = (255, 255, 255)
|
||||
RED = (255, 0, 0)
|
||||
BLUE = (0, 0, 255)
|
||||
CIRCLE_RADIUS = 2
|
||||
BLACK = (0, 0, 0)
|
||||
|
||||
def init_circle():
|
||||
global particle
|
||||
particle = Particle(width // 2, height // 2)
|
||||
surface = pygame.Surface((CIRCLE_RADIUS * 2, CIRCLE_RADIUS * 2))
|
||||
pygame.draw.circle(surface, RED, (CIRCLE_RADIUS, CIRCLE_RADIUS), CIRCLE_RADIUS)
|
||||
particle.set_surface(surface.convert_alpha())
|
||||
|
||||
def main():
|
||||
screen = init_game()
|
||||
game_loop(screen)
|
||||
exit_game()
|
||||
|
||||
def init_game():
|
||||
global clock
|
||||
clock = pygame.time.Clock()
|
||||
pygame.init()
|
||||
screen = pygame.display.set_mode(SIZE)
|
||||
init_circle()
|
||||
return screen
|
||||
|
||||
def game_loop(screen):
|
||||
while True:
|
||||
if not event_handling():
|
||||
break
|
||||
if not update_game():
|
||||
break
|
||||
event_handling()
|
||||
update_game()
|
||||
draw_screen(screen)
|
||||
|
||||
|
||||
def event_handling():
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
return False
|
||||
keys_pressed = pygame.key.get_pressed()
|
||||
if keys_pressed[pygame.K_UP]:
|
||||
particle.apply_force((0,-1))
|
||||
if keys_pressed[pygame.K_DOWN]:
|
||||
particle.apply_force((0,1))
|
||||
if keys_pressed[pygame.K_LEFT]:
|
||||
particle.apply_force((-1,0))
|
||||
if keys_pressed[pygame.K_RIGHT]:
|
||||
particle.apply_force((1,0))
|
||||
particle.apply_force((particle.velocity[0]*-FRICTION,particle.velocity[1]*-FRICTION))
|
||||
particle.update()
|
||||
return True
|
||||
|
||||
def update_game():
|
||||
clock.tick(60)
|
||||
return True
|
||||
|
||||
def draw_screen(screen):
|
||||
screen.fill(BLACK)
|
||||
particle.appear(screen)
|
||||
pygame.display.flip()
|
||||
|
||||
def exit_game():
|
||||
pygame.quit()
|
||||
|
||||
main()
|
||||
@@ -0,0 +1,130 @@
|
||||
import pygame
|
||||
|
||||
|
||||
class Walker:
|
||||
def __init__(self,pos,size):
|
||||
self.pos = pos
|
||||
self.size = size
|
||||
self.walk_direction = (0,0)
|
||||
|
||||
def grow(self):
|
||||
self.size = self.size + 1
|
||||
|
||||
def controls(self):
|
||||
keys_pressed = pygame.key.get_pressed()
|
||||
if keys_pressed[pygame.K_UP]:
|
||||
pos = (walker.pos[0], walker.pos[1] - 1)
|
||||
self.pos = pos
|
||||
self.walk_direction = (self.walk_direction[0],self.walk_direction[1]-1)
|
||||
if keys_pressed[pygame.K_DOWN]:
|
||||
pos = (walker.pos[0], walker.pos[1] + 1)
|
||||
self.pos = pos
|
||||
self.walk_direction = (self.walk_direction[0], self.walk_direction[1] - 1)
|
||||
if keys_pressed[pygame.K_LEFT]:
|
||||
pos = (walker.pos[0] - 1, walker.pos[1])
|
||||
self.pos = pos
|
||||
self.walk_direction = (self.walk_direction[0], self.walk_direction[1] - 1)
|
||||
if keys_pressed[pygame.K_RIGHT]:
|
||||
pos = (walker.pos[0] + 1, walker.pos[1])
|
||||
self.pos = pos
|
||||
self.walk_direction = (self.walk_direction[0], self.walk_direction[1] - 1)
|
||||
if keys_pressed[pygame.K_SPACE]:
|
||||
x=0
|
||||
projectiles[x] = Particle(walker.pos[0],walker.pos[1])
|
||||
projectiles[x].apply_force(self.walk_direction)
|
||||
x+=1
|
||||
|
||||
def appearance(self,screen):
|
||||
pygame.draw.circle(screen, RED, self.pos, self.size)
|
||||
|
||||
class Particle:
|
||||
def __init__(self,x,y):
|
||||
self.pos = (x,y)
|
||||
self.velocity = (0,0)
|
||||
self.accel = (0,0)
|
||||
self.surface = None
|
||||
|
||||
def set_surface(self,surface):
|
||||
self.surface = surface
|
||||
|
||||
def appear(self,screen):
|
||||
if self.surface is not None:
|
||||
pygame.draw.circle(screen, BLUE, self.pos, CIRCLE_RADIUS)
|
||||
blit_pos = (self.pos[0]-self.surface.get_width()/2, self.pos[1]-self.surface.get_height()/2)
|
||||
screen.blit(self.surface, blit_pos)
|
||||
|
||||
def update(self):
|
||||
self.velocity = (self.velocity[0]+self.accel[0], self.velocity[1]+self.accel[1])
|
||||
self.pos = (self.pos[0] + self.velocity[0], self.pos[1] + self.velocity[1])
|
||||
self.accel = (0,0)
|
||||
|
||||
def apply_force(self,force):
|
||||
self.accel = (self.accel[0]+force[0],self.accel[1]+force[1])
|
||||
|
||||
projectiles = {}
|
||||
width = 640
|
||||
height = 600
|
||||
SIZE = (width, height)
|
||||
FRICTION = 0.3
|
||||
WHITE = (255, 255, 255)
|
||||
RED = (255, 0, 0)
|
||||
BLUE = (0, 0, 255)
|
||||
CIRCLE_RADIUS = 2
|
||||
BLACK = (0, 0, 0)
|
||||
|
||||
def init_circle():
|
||||
global particle
|
||||
particle = Particle(width // 2, height // 2)
|
||||
surface = pygame.Surface((CIRCLE_RADIUS * 2, CIRCLE_RADIUS * 2))
|
||||
pygame.draw.circle(surface, RED, (CIRCLE_RADIUS, CIRCLE_RADIUS), CIRCLE_RADIUS)
|
||||
particle.set_surface(surface.convert_alpha())
|
||||
|
||||
def main():
|
||||
screen = init_game()
|
||||
game_loop(screen)
|
||||
exit_game()
|
||||
|
||||
def init_game():
|
||||
global clock
|
||||
global walker
|
||||
walker = Walker((300,300),10)
|
||||
clock = pygame.time.Clock()
|
||||
pygame.init()
|
||||
screen = pygame.display.set_mode(SIZE)
|
||||
init_circle()
|
||||
return screen
|
||||
|
||||
def game_loop(screen):
|
||||
while True:
|
||||
if not event_handling():
|
||||
break
|
||||
if not update_game():
|
||||
break
|
||||
event_handling()
|
||||
update_game()
|
||||
draw_screen(screen)
|
||||
|
||||
def event_handling():
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
return False
|
||||
return True
|
||||
|
||||
def update_game():
|
||||
walker.controls()
|
||||
for x in projectiles:
|
||||
projectiles[x].update()
|
||||
clock.tick(60)
|
||||
return True
|
||||
|
||||
def draw_screen(screen):
|
||||
screen.fill(BLACK)
|
||||
walker.appearance(screen)
|
||||
for x in projectiles:
|
||||
projectiles[x].appear(screen)
|
||||
pygame.display.flip()
|
||||
|
||||
def exit_game():
|
||||
pygame.quit()
|
||||
|
||||
main()
|
||||
@@ -0,0 +1,108 @@
|
||||
import pygame
|
||||
|
||||
BLOCKSIZE = 200
|
||||
width = 600
|
||||
height = 600
|
||||
SIZE = (width, height)
|
||||
BLACK = (0, 0, 0)
|
||||
WHITE = (255, 255, 255)
|
||||
RED = (255, 0, 0)
|
||||
|
||||
class Block:
|
||||
def __init__(self, grid_pos, visibility):
|
||||
self.grid_pos = grid_pos
|
||||
self.visibility = visibility
|
||||
def draw(self,screen):
|
||||
block_shape = (self.grid_pos[0], self.grid_pos[1], BLOCKSIZE, BLOCKSIZE)
|
||||
pygame.draw.rect(screen, BLACK, block_shape)
|
||||
pygame.draw.rect(screen, RED, block_shape, 1)
|
||||
|
||||
class Circle:
|
||||
def __init__(self, grid_pos, visibility):
|
||||
self.grid_pos = grid_pos
|
||||
self.visibility = visibility
|
||||
def set_visible(self):
|
||||
self.visibility = True
|
||||
def draw(self,screen):
|
||||
radius = BLOCKSIZE // 2
|
||||
center = (self.grid_pos[0]-radius, self.grid_pos[1]-radius)
|
||||
pygame.draw.circle(screen, WHITE, center, radius)
|
||||
pygame.draw.circle(screen, BLACK, center, radius-10)
|
||||
|
||||
class Cross:
|
||||
def __init__(self, grid_pos, visibility):
|
||||
self.grid_pos = grid_pos
|
||||
self.visibility = visibility
|
||||
def set_visible(self):
|
||||
self.visibility = True
|
||||
def draw(self,screen):
|
||||
pass
|
||||
|
||||
|
||||
def main():
|
||||
screen = init_game()
|
||||
create_grid(screen)
|
||||
game_loop(screen)
|
||||
exit_game()
|
||||
|
||||
def init_game():
|
||||
global circles
|
||||
global blocks
|
||||
global crosses
|
||||
crosses ={}
|
||||
circles = {}
|
||||
blocks = {}
|
||||
pygame.init()
|
||||
screen = pygame.display.set_mode(SIZE)
|
||||
return screen
|
||||
|
||||
def game_loop(screen):
|
||||
while True:
|
||||
if not event_handling():
|
||||
break
|
||||
if not update_game():
|
||||
break
|
||||
event = event_handling()
|
||||
update_game()
|
||||
draw_game(screen)
|
||||
|
||||
def event_handling():
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
return False
|
||||
if event.type == pygame.MOUSEBUTTONDOWN:
|
||||
pos = pygame.mouse.get_pos()
|
||||
x = pos[0] // BLOCKSIZE * BLOCKSIZE
|
||||
y = pos[1] // BLOCKSIZE * BLOCKSIZE
|
||||
circles[(x, y)].set_visible()
|
||||
return True
|
||||
|
||||
|
||||
def update_game():
|
||||
return True
|
||||
|
||||
def draw_game(screen):
|
||||
screen.fill(BLACK)
|
||||
|
||||
for x,y in blocks:
|
||||
if blocks[(x,y)].visibility:
|
||||
blocks[(x,y)].draw(screen)
|
||||
if circles[(x,y)].visibility:
|
||||
circles[(x,y)].draw(screen)
|
||||
|
||||
pygame.display.flip()
|
||||
|
||||
def create_grid(screen):
|
||||
y = 0
|
||||
for _ in range(3):
|
||||
x = 0
|
||||
for _ in range(3):
|
||||
blocks[(x,y)]= (Block ((x, y), True))
|
||||
circles[(x, y)] = (Circle((x, y), False))
|
||||
x += BLOCKSIZE
|
||||
y += BLOCKSIZE
|
||||
|
||||
def exit_game():
|
||||
pygame.quit()
|
||||
|
||||
main()
|
||||
Reference in new issue
Block a user