hhalim commited on
Commit
487aeac
·
1 Parent(s): 73c6e7a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -0
app.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pygame
3
+ from pygame.locals import *
4
+ import sys
5
+
6
+ #Initialize Pygame
7
+ pygame.init()
8
+
9
+ #Set the window size
10
+ window_width=640
11
+ window_height=480
12
+
13
+ screen=pygame.display.set_mode((window_width,window_height))
14
+
15
+ #Set window title
16
+ pygame.display.set_caption('Pacman')
17
+
18
+ #Set the background color
19
+ bgcolor = (0,0,0)
20
+ screen.fill(bgcolor)
21
+
22
+ #Pacman variables
23
+ x_pos = 50
24
+ y_pos = 50
25
+ pacman_width = 20
26
+
27
+ #Ghost variables
28
+ ghost_xpos = 300
29
+ ghost_ypos = 300
30
+ ghost_width = 20
31
+ ghost_height = 20
32
+
33
+ #Draw Pacman
34
+ def drawPacman(x,y):
35
+ pygame.draw.circle(screen,(255,255,0),(x,y),pacman_width)
36
+
37
+ #Draw Ghost
38
+ def drawGhost(x,y):
39
+ pygame.draw.rect(screen,(255,0,0),(x,y,ghost_width,ghost_height))
40
+
41
+ #Game loop
42
+ while True:
43
+ for event in pygame.event.get():
44
+ if event.type == QUIT:
45
+ pygame.quit()
46
+ sys.exit()
47
+ #Move Pacman
48
+ keys = pygame.key.get_pressed()
49
+ if keys[K_LEFT]:
50
+ x_pos -= 5
51
+ if keys[K_RIGHT]:
52
+ x_pos += 5
53
+ if keys[K_UP]:
54
+ y_pos -= 5
55
+ if keys[K_DOWN]:
56
+ y_pos += 5
57
+ #Move Ghost
58
+ if ghost_xpos < x_pos:
59
+ ghost_xpos += 1
60
+ if ghost_xpos > x_pos:
61
+ ghost_xpos -= 1
62
+ if ghost_ypos < y_pos:
63
+ ghost_ypos += 1
64
+ if ghost_ypos > y_pos:
65
+ ghost_ypos -= 1
66
+
67
+ #Draw elements
68
+ screen.fill(bgcolor)
69
+ drawPacman(x_pos,y_pos)
70
+ drawGhost(ghost_xpos,ghost_ypos)
71
+ pygame.display.update()