Spaces:
Build error
Build error
import streamlit as st | |
import pygame | |
from pygame.locals import * | |
import sys | |
#Initialize Pygame | |
pygame.init() | |
#Set the window size | |
window_width=640 | |
window_height=480 | |
screen=pygame.display.set_mode((window_width,window_height)) | |
#Set window title | |
pygame.display.set_caption('Pacman') | |
#Set the background color | |
bgcolor = (0,0,0) | |
screen.fill(bgcolor) | |
#Pacman variables | |
x_pos = 50 | |
y_pos = 50 | |
pacman_width = 20 | |
#Ghost variables | |
ghost_xpos = 300 | |
ghost_ypos = 300 | |
ghost_width = 20 | |
ghost_height = 20 | |
#Draw Pacman | |
def drawPacman(x,y): | |
pygame.draw.circle(screen,(255,255,0),(x,y),pacman_width) | |
#Draw Ghost | |
def drawGhost(x,y): | |
pygame.draw.rect(screen,(255,0,0),(x,y,ghost_width,ghost_height)) | |
#Game loop | |
while True: | |
for event in pygame.event.get(): | |
if event.type == QUIT: | |
pygame.quit() | |
sys.exit() | |
#Move Pacman | |
keys = pygame.key.get_pressed() | |
if keys[K_LEFT]: | |
x_pos -= 5 | |
if keys[K_RIGHT]: | |
x_pos += 5 | |
if keys[K_UP]: | |
y_pos -= 5 | |
if keys[K_DOWN]: | |
y_pos += 5 | |
#Move Ghost | |
if ghost_xpos < x_pos: | |
ghost_xpos += 1 | |
if ghost_xpos > x_pos: | |
ghost_xpos -= 1 | |
if ghost_ypos < y_pos: | |
ghost_ypos += 1 | |
if ghost_ypos > y_pos: | |
ghost_ypos -= 1 | |
#Draw elements | |
screen.fill(bgcolor) | |
drawPacman(x_pos,y_pos) | |
drawGhost(ghost_xpos,ghost_ypos) | |
pygame.display.update() |