2021-06-22 16:18:08 +02:00
|
|
|
import pygame, random, sys, time
|
2021-06-21 20:00:50 +02:00
|
|
|
|
|
|
|
# global variables
|
|
|
|
windowWidth = 800
|
|
|
|
windowHeight = 800
|
|
|
|
fps = 60
|
2021-06-22 16:18:08 +02:00
|
|
|
textColor = (255,255,255)
|
2021-06-21 20:00:50 +02:00
|
|
|
|
|
|
|
# initialize pygame and create a window
|
|
|
|
pygame.init()
|
|
|
|
clock = pygame.time.Clock()
|
|
|
|
window = pygame.display.set_mode((windowWidth, windowHeight))
|
|
|
|
pygame.display.set_caption("Pygame Dodger")
|
|
|
|
pygame.mouse.set_visible(False)
|
|
|
|
|
|
|
|
# create a quit function
|
|
|
|
def terminate():
|
|
|
|
pygame.quit()
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
def redrawGameWindow():
|
|
|
|
window.fill((0,0,0))
|
|
|
|
|
|
|
|
for baddie in baddies:
|
|
|
|
baddie.draw(window)
|
|
|
|
man.draw(window)
|
|
|
|
|
|
|
|
pygame.display.update()
|
2021-06-22 16:18:08 +02:00
|
|
|
|
|
|
|
def drawText(text, font, window, x,y):
|
|
|
|
textObj = font.render(text, 1, textColor)
|
|
|
|
textRect = textObj.get_rect()
|
|
|
|
textRect.topleft = (x - textObj.get_width() // 2,y - textObj.get_height() // 2)
|
|
|
|
window.blit(textObj, textRect)
|
|
|
|
|
|
|
|
def waitForKeyPress():
|
|
|
|
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 resetGame(player, baddies, gameLogic):
|
|
|
|
window.fill((0,0,0))
|
|
|
|
player.x = 100
|
|
|
|
player.y = 700
|
|
|
|
|
|
|
|
baddies.clear()
|
|
|
|
|
|
|
|
gameLogic.retry = False
|
|
|
|
gameLogic.timer = 0
|
|
|
|
pygame.display.update()
|
|
|
|
|
|
|
|
def askForRetry(keys, gameLogic):
|
|
|
|
window.fill((0,0,0))
|
|
|
|
drawText("Wanna try again? y/n", font, window, windowHeight / 2, windowWidth / 2)
|
|
|
|
pygame.display.update()
|
|
|
|
|
|
|
|
if keys[pygame.K_y]:
|
|
|
|
gameLogic.lost = False
|
|
|
|
gameLogic.retry = True
|
|
|
|
if keys[pygame.K_n]:
|
|
|
|
gameLogic.run = False
|
|
|
|
|
|
|
|
class game(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.timer = 0
|
|
|
|
self.run = True
|
|
|
|
self.lost = False
|
|
|
|
self.retry = False
|
|
|
|
pass
|
|
|
|
|
|
|
|
def play(self, keys, baddies, baddiesSpawnRate, player):
|
|
|
|
if self.timer % baddiesSpawnRate == 0:
|
|
|
|
baddies.append(enemy())
|
|
|
|
|
|
|
|
for baddie in baddies:
|
|
|
|
baddie.move()
|
|
|
|
|
|
|
|
if player.hitbox.colliderect(baddie.hitbox):
|
|
|
|
self.lost = True
|
|
|
|
|
|
|
|
if baddie.y + baddie.height + 10 > windowHeight:
|
|
|
|
baddies.pop(baddies.index(baddie))
|
|
|
|
|
|
|
|
player.move(keys)
|
|
|
|
|
|
|
|
self.timer += 1
|
2021-06-21 20:00:50 +02:00
|
|
|
|
|
|
|
# enemy class
|
|
|
|
class enemy(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.startLine = 100
|
|
|
|
self.endLine = 700
|
|
|
|
self.minSize = 10
|
|
|
|
self.maxSize = 40
|
|
|
|
self.minSpeed = 1
|
|
|
|
self.maxSpeed = 8
|
|
|
|
self.y = 10
|
|
|
|
self.x = random.randint(self.startLine, self.endLine)
|
|
|
|
self.width = self.height = random.randint(self.minSize, self.maxSize)
|
|
|
|
self.vel = random.randint(self.minSpeed, self.maxSpeed)
|
|
|
|
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
|
|
|
|
|
|
|
|
# player class
|
|
|
|
class player(object):
|
|
|
|
def __init__(self, x, y, width, height):
|
|
|
|
self.x = x
|
|
|
|
self.y = y
|
|
|
|
self.width = width
|
|
|
|
self.height = height
|
|
|
|
self.isAlive = True
|
|
|
|
self.moveSpeed = 5
|
|
|
|
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)
|
|
|
|
if keys[pygame.K_RIGHT] and self.x < windowWidth - self.width - 50:
|
|
|
|
self.x += self.moveSpeed
|
|
|
|
if keys[pygame.K_LEFT] and self.x > 0 + 50:
|
|
|
|
self.x -= self.moveSpeed
|
|
|
|
if keys[pygame.K_UP] and self.y > 0 + windowWidth / 2:
|
|
|
|
self.y -= self.moveSpeed
|
|
|
|
if keys[pygame.K_DOWN] and self.y < windowHeight - self.height - 50:
|
|
|
|
self.y += self.moveSpeed
|
|
|
|
|
|
|
|
man = player(100, 700, 30, 30)
|
|
|
|
baddies = []
|
|
|
|
baddieSpawnRate = 40
|
2021-06-22 16:18:08 +02:00
|
|
|
font = pygame.font.SysFont(None, 48)
|
|
|
|
dodger = game()
|
2021-06-21 20:00:50 +02:00
|
|
|
|
2021-06-22 16:18:08 +02:00
|
|
|
drawText('Dodger', font, window, (windowWidth / 2), (windowHeight / 2 - 30))
|
|
|
|
drawText('Press a key to start.', font, window, (windowWidth / 2), (windowHeight / 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-06-22 16:18:08 +02:00
|
|
|
waitForKeyPress()
|
|
|
|
|
|
|
|
# main loop
|
|
|
|
while dodger.run:
|
2021-06-21 20:00:50 +02:00
|
|
|
clock.tick(fps)
|
|
|
|
|
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:
|
|
|
|
askForRetry(keys, dodger)
|
|
|
|
else :
|
|
|
|
if dodger.retry:
|
|
|
|
resetGame(man, baddies, dodger)
|
|
|
|
dodger.play(keys, baddies, baddieSpawnRate, man)
|
|
|
|
redrawGameWindow()
|
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))
|
|
|
|
drawText('Goodbye', font, window, (windowWidth / 2 ), (windowHeight / 2))
|
|
|
|
pygame.display.update()
|
|
|
|
time.sleep(1)
|
2021-06-21 20:00:50 +02:00
|
|
|
terminate()
|