-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a1a6f7f
Showing
4 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# pacman using pygame |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# import the libraries | ||
import sys, pygame | ||
|
||
# initialize pygame | ||
pygame.init() | ||
|
||
# set size of the window | ||
size = width, height = 1024, 720 | ||
|
||
# initially pacman does not move | ||
speed = [0,0] | ||
|
||
# windows color | ||
black = 0, 0, 0 | ||
|
||
screen = pygame.display.set_mode(size) | ||
|
||
# pacman image object | ||
pacman = pygame.image.load('pacman.gif') | ||
# rectangle around pacman | ||
pacmanrect = pacman.get_rect() | ||
|
||
# pacman not moving initially | ||
moving = False | ||
|
||
# the game loop | ||
while 1: | ||
# check all the current events | ||
for event in pygame.event.get(): | ||
if event.type == pygame.QUIT: sys.exit() | ||
if event.type == pygame.KEYDOWN: | ||
if event.key == pygame.K_LEFT: | ||
moving = True | ||
speed = [-8, 0] | ||
if event.key == pygame.K_RIGHT: | ||
moving = True | ||
speed = [8, 0] | ||
if event.key == pygame.K_UP: | ||
moving = True | ||
speed = [0, -8] | ||
if event.key == pygame.K_DOWN: | ||
moving = True | ||
speed = [0, 8] | ||
if moving: | ||
pacmanrect = pacmanrect.move(speed) | ||
if pacmanrect.left < 0 or pacmanrect.right > width: | ||
moving = False | ||
if pacmanrect.top < 0 or pacmanrect.bottom > height: | ||
moving = False | ||
|
||
screen.fill(black) | ||
screen.blit(pacman, pacmanrect) | ||
pygame.display.flip() | ||
pygame.display.update() |