File size: 1,410 Bytes
487aeac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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()