CODING/Python/Escape.py DATA/Escape log/Escape - Copy.py
2026-01-19 17:54:17 -05:00

142 lines
4.3 KiB
Python

import pygame, random, sys, time
from pygame.locals import *
#set up some variables
WINDOWWIDTH = 600
WINDOWHEIGHT = 400
window = pygame.display.set_mode((600,400))
TEXTCOLOR = (255, 0, 0)
BLACK = (0,0,0)
BLUE = (0, 0, 255)
RED = (255, 0, 0)
playerImage = pygame.image.load('Player.png')
playerRect = playerImage.get_rect()
moveX,moveY=0,0
mainClock = pygame.time.Clock()
class Sprite:
def __init__(self,x,y):
self.x=x
self.y=y
self.image=playerImage
self.rect=playerRect
surface=pygame.get_rect()
def render(self):
pygame.draw.rect(window,white,(self.x,self.y,self.width,self.height))
def terminate():
pygame.quit()
sys.exit()
def waitForPlayerToPressKey():
while True:
for event in pygame.event.get():
if event.type == QUIT:
terminate()
if event.type == KEYDOWN:
if event.key == K_ESCAPE: # pressing escape quits
terminate()
if event.key == K_RETURN:
return
if event.key == ord('q'):
return
def drawText(text, font, surface, x, y):
textobj = font.render(text, 1, TEXTCOLOR)
textrect = textobj.get_rect()
textrect.topleft = (x, y)
surface.blit(textobj, textrect)
player = Sprite
# set up pygame, the window, and the mouse cursor
pygame.init()
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
pygame.display.set_caption('ESCAPE')
pygame.mouse.set_visible(True)
# set up fonts
font = pygame.font.SysFont(None, 48)
# set up images
backgroundImage = pygame.image.load('background1.png')
rescaledBackground = pygame.transform.scale(backgroundImage, (WINDOWWIDTH, WINDOWHEIGHT))
# show the "Start" screen
windowSurface.blit(rescaledBackground, (0, 0))
drawText(' Welcome to ESCAPE', font, windowSurface, (WINDOWWIDTH / 2) - 105, (WINDOWHEIGHT / 3))
drawText('Press Enter to start', font, windowSurface, (WINDOWWIDTH / 2) - 135, (WINDOWHEIGHT / 3) + 50)
pygame.display.update()
waitForPlayerToPressKey()
while True:
# set up start of game
moveLeft = moveRight =False
moveUp = moveDown = False
while True: # the game loop runs while the game part is playing
for event in pygame.event.get():
if event.type == QUIT:
terminate()
if event.type == KEYDOWN:
if event.key == K_LEFT or event.key == ord('a'):
moveRight = False
moveLeft = True
moveX=-5
if event.key == K_RIGHT or event.key == ord('d'):
moveLeft = False
moveRight = True
moveX=5
if event.key == K_UP or event.key == ord('w'):
moveUp = True
moveDown = False
moveY=-5
if event.key == K_DOWN or event.key == ord('s'):
moveUp = False
moveDown = True
moveY=5
if event.type == KEYUP:
if event.key == K_ESCAPE:
terminate()
if event.key == K_LEFT or event.key == ord('a'):
moveLeft = False
moveX=0
if event.key == K_RIGHT or event.key == ord('d'):
moveRight = False
moveX=0
if event.key == K_DOWN or event.key == ord('s'):
moveDown = False
moveY=0
if event.key == K_UP or event.key == ord('w'):
moveUp = False
moveY=0
# Move the player around.
player.x += moveX
player.y += moveY
# Draw the game world on the window.
windowSurface.blit(rescaledBackground, (0, 0))
# Draw the game world on the window.
windowSurface.blit(rescaledBackground, (0, 0))
# Draw the player's rectangle, rails
windowSurface.blit(playerImage,playerRect, (x,y,50,50))
# update the display
pygame.display.update()
pygame.display.flip()
mainClock.tick(50)