import pygame
from pygame.locals import *
import info
from pymsgbox import *
sizeConfirm = confirm(text='Select window size:', title='Easy? No!', buttons=['800, 400', '1200, 700', '800, 600'])
#variables
if sizeConfirm == '800, 400':
windowSize = (800, 400)
elif sizeConfirm == '1200, 700':
windowSize = (1200, 700)
elif sizeConfirm == '800, 600':
windowSize = (800, 600)
moveRight = False
moveLeft = False
jumpCount = 0
airTimer = 0
trueScroll = [0,0]
fails = 0
demoMode = "Yes"
fullscreen = False
pygame.init()
#window
pygame.display.set_caption('Danger!')
screen = pygame.display.set_mode(windowSize, pygame.RESIZABLE)
display = pygame.Surface((400, 300))
clock = pygame.time.Clock()
#loading images
grassImg = pygame.image.load("grass.png")
dirtImg = pygame.image.load("dirt.png")
playerImg = pygame.image.load('player.png')
portalImg = pygame.image.load("portal.png")
#loading audio
soundChannel = pygame.mixer.Channel(0)
jumpSound = pygame.mixer.Sound('jump.wav')
failSound = pygame.mixer.Sound('fail.wav')
playerRect = pygame.Rect(100,48,8,16)
def loadMap(path):
f = open(path + '.txt','r')
data = f.read()
f.close()
data = data.split('\n')
gameMap = []
for row in data:
gameMap.append(list(row))
return gameMap
gameMap = loadMap('map')
def collision_test(rect,tiles):
hitList = []
for tile in tiles:
if rect.colliderect(tile):
hitList.append(tile)
return hitList
def move(rect,movement,tiles):
collision_types = {'top':False,'bottom':False,'right':False,'left':False}
rect.x += movement[0]
hitList = collision_test(rect,tiles)
for tile in hitList:
if movement[0] > 0:
rect.right = tile.left
collision_types['right'] = True
elif movement[0] < 0:
rect.left = tile.right
collision_types['left'] = True
rect.y += movement[1]
hitList = collision_test(rect,tiles)
for tile in hitList:
if movement[1] > 0:
rect.bottom = tile.top
collision_types['bottom'] = True
elif movement[1] < 0:
rect.top = tile.bottom
collision_types['top'] = True
return rect, collision_types
def level1(playerRect, moveRight, moveLeft, jumpCount, airTimer, fails, display):
run = True
while run:
display.fill((146,244,255))
trueScroll[0] += (playerRect.x-trueScroll[0]-152)/20
trueScroll[1] += (playerRect.y-trueScroll[1]-106)/20
scroll = trueScroll.copy()
scroll[0] = int(scroll[0])
scroll[1] = int(scroll[1])
tileRects = []
y = 0
for layer in gameMap:
x = 0
for tile in layer:
if tile == 'd':
display.blit(dirtImg,(x*16-scroll[0],y*16-scroll[1]))
if tile == 'g':
display.blit(grassImg,(x*16-scroll[0],y*16-scroll[1]))
if tile == 't':
display.blit(portalImg,(x*16-scroll[0],y*16-scroll[1]))
if tile != ' ':
tileRects.append(pygame.Rect(x*16,y*16,16,16))
x += 1
y += 1
playerMovement = [0, 0]
if moveRight == True:
playerMovement[0] += 2
if moveLeft == True:
playerMovement[0] -= 2
playerMovement[1] += jumpCount
jumpCount += 0.3
if jumpCount > 3:
jumpCount = 3
playerRect,collisions = move(playerRect,playerMovement,tileRects)
if collisions['bottom'] == True:
airTimer = 0
jumpCount = 0
else:
airTimer += 1
display.blit(playerImg,(playerRect.x-scroll[0],playerRect.y-scroll[1]))
for event in pygame.event.get(): # event loop
if event.type == QUIT:
run = False
if event.type == VIDEORESIZE:
if not fullscreen:
display = pygame.display.set_mode((event.w/2, event.h/2), pygame.RESIZABLE)
if event.type == KEYDOWN:
if event.key == K_RIGHT:
moveRight = True
if event.key == K_LEFT:
moveLeft = True
if event.key == K_UP:
if airTimer < 6:
soundChannel.play(jumpSound)
jumpCount = -5
if event.type == KEYUP:
if event.key == K_RIGHT:
moveRight = False
if event.key == K_LEFT:
moveLeft = False
if playerRect.y > 800:
soundChannel.play(failSound)
fails += 1
pygame.display.set_caption('Danger! Fails: '+str(fails))
playerRect.x = 100
playerRect.y = 48
screen.blit(pygame.transform.scale(display,windowSize),(0,0))
pygame.display.update()
clock.tick(info.framerate)
#Demo-board
if demoMode == "Yes":
print("# Demo-board #")
print("FPS:", clock.get_fps())
print("Player position: " + str(playerRect.x), str(playerRect.y))
print("Max-Channels: 8")
print('Playing: ' + str(soundChannel.get_sound()))
level1(playerRect, moveRight, moveLeft, jumpCount, airTimer, fails, display)