CODING/Python/rect race.py
2026-01-19 17:54:17 -05:00

143 lines
4.1 KiB
Python

import pygame, random, sys, time
from pygame.locals import *
windowWidth = 600
windowHeight = 600
FPS = 6000
Green = (0, 255, 0)
Blue = (0, 0, 255)
Red = (255, 0, 0)
Grey = (100, 100, 100)
Black = (0, 0, 0)
White = (255, 255, 255)
moveLeft,moveRight=0,0
moveUp,moveDown=0,0
pygame.init()
Clock = pygame.time.Clock()
windowSurface = pygame.display.set_mode((windowWidth,windowHeight))
pygame.display.set_caption('Rect Race')
pygame.mouse.set_visible(True)
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:
terminate()
if event.key == K_RETURN:
return
if event.key == ord ('e'):
return
class Sprite:
def __init__(self,x,y):
self.x=x
self.y=y
self.width=50
self.height=50
def render(self):
pygame.draw.rect(windowSurface,White,(self.x,self.y,self.width,self.height))
def playerHasHitSquare(playerRect, Square):
for s in square:
if Player.coliderect(s['rect']):
return True
return False
def drawTextGrey(text, font, surface, x, y):
textobj = font.render(text, 1, Grey)
textrect = textobj.get_rect()
textrect.topleft = (x, y)
surface.blit(textobj, textrect)
def drawTextRed(text, font, surface, x, y):
textobj = font.render(text, 1, Red)
textrect = textobj.get_rect()
textrect.topleft = (x, y)
surface.blit(textobj, textrect)
def drawTextBlue(text, font, surface, x, y):
textobj = font.render(text, 1, Blue)
textrect = textobj.get_rect()
textrect.topleft = (x, y)
surface.blit(textobj, textrect)
def drawTextGreen(text, font, surface, x, y):
textobj = font.render(text, 1, Green)
textrect = textobj.get_rect()
textrect.topleft = (x, y)
surface.blit(textobj, textrect)
def drawTextBlack(text, font, surface, x, y):
textobj = font.render(text, 1, Black)
textrect = textobj.get_rect()
textrect.topleft = (x, y)
surface.blit(textobj, textrect)
Player = Sprite (100,150)
font = pygame.font.SysFont(None, 48)
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
if event.key == K_RIGHT or event.key == ord('d'):
moveLeft = False
moveRight = True
if event.key == K_UP or event.key == ord('w'):
moveUp = True
moveDown = False
if event.key == K_DOWN or event.key == ord('s'):
moveUp = False
moveDown = True
if event.type == KEYUP:
if event.key == K_ESCAPE:
terminate()
if event.key == K_LEFT or event.key == ord('a'):
moveLeft = False
if event.key == K_RIGHT or event.key == ord('d'):
moveRight = False
if event.key == K_DOWN or event.key == ord('s'):
moveDown = False
if event.key == K_UP or event.key == ord('w'):
moveUp = False
windowSurface.fill(Black)
# Move the player around.
if moveLeft and Player.x > 0:
Player.x += -5
if moveRight and Player.x < windowWidth:
Player.x += 5
if moveUp and Player.y > 0:
Player.y += -5
if moveDown and Player.y < windowHeight:
Player.y += 5
Player.render()
Clock.tick(60)
pygame.display.flip()