2021-06-22 16:18:08 +02:00
|
|
|
import pygame, random, sys, time
|
2021-06-21 20:00:50 +02:00
|
|
|
|
|
|
|
# global variables
|
2021-07-14 17:42:00 +02:00
|
|
|
WIDTH = 800
|
|
|
|
HEIGHT = 800
|
|
|
|
FPS = 60
|
|
|
|
TEXT_COLOR = (255,255,255)
|
2021-06-21 20:00:50 +02:00
|
|
|
|
|
|
|
# initialize pygame and create a window
|
|
|
|
pygame.init()
|
|
|
|
clock = pygame.time.Clock()
|
2021-07-14 17:42:00 +02:00
|
|
|
window = pygame.display.set_mode((WIDTH, HEIGHT))
|
2021-06-21 20:00:50 +02:00
|
|
|
pygame.display.set_caption("Pygame Dodger")
|
|
|
|
pygame.mouse.set_visible(False)
|
|
|
|
|
2021-07-14 17:42:00 +02:00
|
|
|
class Game:
|
2021-06-22 16:18:08 +02:00
|
|
|
def __init__(self):
|
|
|
|
self.timer = 0
|
|
|
|
self.run = True
|
|
|
|
self.lost = False
|
|
|
|
|
2021-07-14 17:42:00 +02:00
|
|
|
def play(self, keys, baddies, baddies_spawn_rate, player):
|
|
|
|
if self.timer % baddies_spawn_rate == 0:
|
2021-06-22 16:18:08 +02:00
|
|
|
baddies.append(enemy())
|
|
|
|
|
|
|
|
for baddie in baddies:
|
|
|
|
baddie.move()
|
|
|
|
|
|
|
|
if player.hitbox.colliderect(baddie.hitbox):
|
|
|
|
self.lost = True
|
|
|
|
|
2021-07-14 17:42:00 +02:00
|
|
|
if baddie.y + baddie.height + 10 > HEIGHT:
|
2021-06-22 16:18:08 +02:00
|
|
|
baddies.pop(baddies.index(baddie))
|
|
|
|
|
|
|
|
player.move(keys)
|
|
|
|
|
|
|
|
self.timer += 1
|
2021-06-23 08:09:30 +02:00
|
|
|
|
|
|
|
self.draw(baddies, player)
|
2021-07-14 17:42:00 +02:00
|
|
|
|
2021-06-23 08:09:30 +02:00
|
|
|
def draw(self, baddies, player):
|
|
|
|
window.fill((0,0,0))
|
|
|
|
for baddie in baddies:
|
|
|
|
baddie.draw(window)
|
|
|
|
player.draw(window)
|
|
|
|
pygame.display.update()
|
2021-06-21 20:00:50 +02:00
|
|
|
|
|
|
|
# player class
|
2021-07-14 17:42:00 +02:00
|
|
|
class Player:
|
2021-06-21 20:00:50 +02:00
|
|
|
def __init__(self, x, y, width, height):
|
|
|
|
self.x = x
|
|
|
|
self.y = y
|
|
|
|
self.width = width
|
|
|
|
self.height = height
|
2021-07-14 17:42:00 +02:00
|
|
|
self.is_alive = True
|
|
|
|
self.move_speed = 5
|
2021-06-21 20:00:50 +02:00
|
|
|
self.hitbox = pygame.Rect(self.x, self.y, self.width, self.height)
|
|
|
|
|
|
|
|
def draw(self, window):
|
|
|
|
pygame.draw.rect(window, (0,255,0), (self.x, self.y, self.width, self.height))
|
|
|
|
|
|
|
|
def move(self, keys):
|
|
|
|
self.hitbox = pygame.Rect(self.x, self.y, self.width, self.height)
|
2021-07-14 17:42:00 +02:00
|
|
|
if keys[pygame.K_RIGHT] and self.x < WIDTH - self.width - 50:
|
|
|
|
self.x += self.move_speed
|
2021-06-21 20:00:50 +02:00
|
|
|
if keys[pygame.K_LEFT] and self.x > 0 + 50:
|
2021-07-14 17:42:00 +02:00
|
|
|
self.x -= self.move_speed
|
|
|
|
if keys[pygame.K_UP] and self.y > 0 + WIDTH / 2:
|
|
|
|
self.y -= self.move_speed
|
|
|
|
if keys[pygame.K_DOWN] and self.y < HEIGHT - self.height - 50:
|
|
|
|
self.y += self.move_speed
|
|
|
|
|
|
|
|
# enemy class
|
|
|
|
class enemy:
|
|
|
|
def __init__(self):
|
|
|
|
self.start_line = 100
|
|
|
|
self.end_line = 700
|
|
|
|
self.min_size = 10
|
|
|
|
self.max_size = 40
|
|
|
|
self.min_speed = 1
|
|
|
|
self.max_speed = 8
|
|
|
|
self.y = 10
|
|
|
|
self.x = random.randint(self.start_line, self.end_line)
|
|
|
|
self.width = self.height = random.randint(self.min_size, self.max_size)
|
|
|
|
self.vel = random.randint(self.min_speed, self.max_speed)
|
|
|
|
self.hitbox = pygame.Rect(self.x, self.y, self.width, self.height)
|
|
|
|
|
|
|
|
def draw(self, window):
|
|
|
|
pygame.draw.rect(window, (255,0,0), (self.x, self.y, self.width, self.height))
|
|
|
|
|
|
|
|
def move(self):
|
|
|
|
self.hitbox = pygame.Rect(self.x, self.y, self.width, self.height)
|
|
|
|
self.y += self.vel
|
|
|
|
|
|
|
|
# create a quit function
|
|
|
|
def terminate():
|
|
|
|
pygame.quit()
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
def draw_text(text, font, window, x,y):
|
|
|
|
textObj = font.render(text, 1, TEXT_COLOR)
|
|
|
|
textRect = textObj.get_rect()
|
|
|
|
textRect.topleft = (x - textObj.get_width() // 2, y - textObj.get_height() // 2)
|
|
|
|
window.blit(textObj, textRect)
|
|
|
|
|
|
|
|
def wait_for_key_press():
|
|
|
|
while True:
|
|
|
|
for event in pygame.event.get():
|
|
|
|
if event.type == pygame.QUIT:
|
|
|
|
terminate()
|
|
|
|
if event.type == pygame.KEYDOWN:
|
|
|
|
if event.key == pygame.K_ESCAPE:
|
|
|
|
dodger.run = False
|
|
|
|
dodger.retry = True
|
|
|
|
return
|
|
|
|
|
|
|
|
def reset_game(player, baddies, game_logic):
|
|
|
|
player.x = 100
|
|
|
|
player.y = 700
|
|
|
|
|
|
|
|
baddies.clear()
|
|
|
|
|
|
|
|
game_logic.timer = 0
|
|
|
|
|
|
|
|
def ask_for_retry(keys, game_logic):
|
|
|
|
window.fill((0,0,0))
|
|
|
|
draw_text("Wanna try again? y/n", font, window, HEIGHT / 2, WIDTH / 2)
|
|
|
|
pygame.display.update()
|
|
|
|
|
|
|
|
if keys[pygame.K_y]:
|
|
|
|
game_logic.lost = False
|
|
|
|
if keys[pygame.K_n]:
|
|
|
|
game_logic.run = False
|
2021-06-21 20:00:50 +02:00
|
|
|
|
2021-07-14 17:42:00 +02:00
|
|
|
man = Player(100, 700, 30, 30)
|
2021-06-21 20:00:50 +02:00
|
|
|
baddies = []
|
2021-07-14 17:42:00 +02:00
|
|
|
BADDIE_SPAWN_RATE = 40
|
2021-06-22 16:18:08 +02:00
|
|
|
font = pygame.font.SysFont(None, 48)
|
2021-07-14 17:42:00 +02:00
|
|
|
dodger = Game()
|
2021-06-21 20:00:50 +02:00
|
|
|
|
2021-07-14 17:42:00 +02:00
|
|
|
draw_text('Dodger', font, window, (WIDTH / 2), (HEIGHT / 2 - 30))
|
|
|
|
draw_text('Press a key to start.', font, window, (WIDTH / 2), (HEIGHT / 2 + 30))
|
2021-06-21 20:00:50 +02:00
|
|
|
|
2021-06-22 16:18:08 +02:00
|
|
|
pygame.display.update()
|
2021-06-21 20:00:50 +02:00
|
|
|
|
2021-07-14 17:42:00 +02:00
|
|
|
wait_for_key_press()
|
2021-06-22 16:18:08 +02:00
|
|
|
|
|
|
|
# main loop
|
|
|
|
while dodger.run:
|
2021-07-14 17:42:00 +02:00
|
|
|
clock.tick(FPS)
|
2021-06-21 20:00:50 +02:00
|
|
|
|
2021-06-22 16:18:08 +02:00
|
|
|
keys = pygame.key.get_pressed()
|
2021-06-21 20:00:50 +02:00
|
|
|
|
2021-06-22 16:18:08 +02:00
|
|
|
if dodger.lost:
|
2021-06-23 08:09:30 +02:00
|
|
|
if dodger.timer > 0:
|
2021-07-14 17:42:00 +02:00
|
|
|
reset_game(man, baddies, dodger)
|
|
|
|
ask_for_retry(keys, dodger)
|
2021-06-22 16:18:08 +02:00
|
|
|
else :
|
2021-07-14 17:42:00 +02:00
|
|
|
dodger.play(keys, baddies, BADDIE_SPAWN_RATE, man)
|
2021-06-21 20:00:50 +02:00
|
|
|
for event in pygame.event.get():
|
|
|
|
if event.type == pygame.QUIT:
|
2021-06-22 16:18:08 +02:00
|
|
|
terminate()
|
|
|
|
|
2021-06-21 20:00:50 +02:00
|
|
|
if keys[pygame.K_ESCAPE]:
|
2021-06-22 16:18:08 +02:00
|
|
|
dodger.run = False
|
2021-06-21 20:00:50 +02:00
|
|
|
|
2021-06-22 16:18:08 +02:00
|
|
|
window.fill((0,0,0))
|
2021-07-14 17:42:00 +02:00
|
|
|
draw_text('Goodbye', font, window, (WIDTH / 2 ), (HEIGHT / 2))
|
2021-06-22 16:18:08 +02:00
|
|
|
pygame.display.update()
|
|
|
|
time.sleep(1)
|
2021-06-21 20:00:50 +02:00
|
|
|
terminate()
|