Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit14fe3d5

Browse files
committed
add pacman game tutorial
1 parenta7a830c commit14fe3d5

File tree

60 files changed

+488
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+488
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions

‎gui-programming/pacman-game/README.md

Lines changed: 1 addition & 0 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
fromosimportwalk
2+
importpygame
3+
4+
defimport_sprite(path):
5+
surface_list= []
6+
for_,__,img_fileinwalk(path):
7+
forimageinimg_file:
8+
full_path=f"{path}/{image}"
9+
img_surface=pygame.image.load(full_path).convert_alpha()
10+
surface_list.append(img_surface)
11+
returnsurface_list
903 Bytes
289 Bytes
259 Bytes
284 Bytes
280 Bytes
259 Bytes
284 Bytes
259 Bytes
322 Bytes
259 Bytes

‎gui-programming/pacman-game/berry.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
importpygame
2+
3+
fromsettingsimportCHAR_SIZE,PLAYER_SPEED
4+
5+
classBerry(pygame.sprite.Sprite):
6+
def__init__(self,row,col,size,is_power_up=False):
7+
super().__init__()
8+
self.power_up=is_power_up
9+
self.size=size
10+
self.color=pygame.Color("violetred")
11+
self.thickness=size
12+
self.abs_x= (row*CHAR_SIZE)+ (CHAR_SIZE//2)
13+
self.abs_y= (col*CHAR_SIZE)+ (CHAR_SIZE//2)
14+
15+
# temporary rect for colliderect-checking
16+
self.rect=pygame.Rect(self.abs_x,self.abs_y,self.size*2,self.size*2)
17+
18+
defupdate(self,screen):
19+
self.rect=pygame.draw.circle(screen,self.color, (self.abs_x,self.abs_y),self.size,self.thickness)
20+

‎gui-programming/pacman-game/cell.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
importpygame
2+
3+
classCell(pygame.sprite.Sprite):
4+
def__init__(self,row,col,length,width):
5+
super().__init__()
6+
self.width=length
7+
self.height=width
8+
self.id= (row,col)
9+
self.abs_x=row*self.width
10+
self.abs_y=col*self.height
11+
12+
self.rect=pygame.Rect(self.abs_x,self.abs_y,self.width,self.height)
13+
14+
self.occupying_piece=None
15+
16+
defupdate(self,screen):
17+
pygame.draw.rect(screen,pygame.Color("blue2"),self.rect)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
importpygame
2+
3+
fromsettingsimportWIDTH,HEIGHT,CHAR_SIZE
4+
5+
pygame.font.init()
6+
7+
classDisplay:
8+
def__init__(self,screen):
9+
self.screen=screen
10+
self.font=pygame.font.SysFont("ubuntumono",CHAR_SIZE)
11+
self.game_over_font=pygame.font.SysFont("dejavusansmono",48)
12+
self.text_color=pygame.Color("crimson")
13+
14+
defshow_life(self,life):
15+
img_path="assets/life/life.png"
16+
life_image=pygame.image.load(img_path)
17+
life_image=pygame.transform.scale(life_image, (CHAR_SIZE,CHAR_SIZE))
18+
life_x=CHAR_SIZE//2
19+
20+
iflife!=0:
21+
forlifeinrange(life):
22+
self.screen.blit(life_image, (life_x,HEIGHT+ (CHAR_SIZE//2)))
23+
life_x+=CHAR_SIZE
24+
25+
defshow_level(self,level):
26+
level_x=WIDTH//3
27+
level=self.font.render(f'Level{level}',True,self.text_color)
28+
self.screen.blit(level, (level_x, (HEIGHT+ (CHAR_SIZE//2))))
29+
30+
defshow_score(self,score):
31+
score_x=WIDTH//3
32+
score=self.font.render(f'{score}',True,self.text_color)
33+
self.screen.blit(score, (score_x*2, (HEIGHT+ (CHAR_SIZE//2))))
34+
35+
# add game over message
36+
defgame_over(self):
37+
message=self.game_over_font.render(f'GAME OVER!!',True,pygame.Color("chartreuse"))
38+
instruction=self.font.render(f'Press "R" to Restart',True,pygame.Color("aqua"))
39+
self.screen.blit(message, ((WIDTH//4), (HEIGHT//3)))
40+
self.screen.blit(instruction, ((WIDTH//4), (HEIGHT//2)))

‎gui-programming/pacman-game/ghost.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
importpygame
2+
importrandom
3+
importtime
4+
5+
fromsettingsimportWIDTH,CHAR_SIZE,GHOST_SPEED
6+
7+
classGhost(pygame.sprite.Sprite):
8+
def__init__(self,row,col,color):
9+
super().__init__()
10+
self.abs_x= (row*CHAR_SIZE)
11+
self.abs_y= (col*CHAR_SIZE)
12+
13+
self.rect=pygame.Rect(self.abs_x,self.abs_y,CHAR_SIZE,CHAR_SIZE)
14+
self.move_speed=GHOST_SPEED
15+
self.color=pygame.Color(color)
16+
self.move_directions= [(-1,0), (0,-1), (1,0), (0,1)]
17+
18+
self.moving_dir="up"
19+
self.img_path=f'assets/ghosts/{color}/'
20+
self.img_name=f'{self.moving_dir}.png'
21+
self.image=pygame.image.load(self.img_path+self.img_name)
22+
self.image=pygame.transform.scale(self.image, (CHAR_SIZE,CHAR_SIZE))
23+
self.rect=self.image.get_rect(topleft= (self.abs_x,self.abs_y))
24+
self.mask=pygame.mask.from_surface(self.image)
25+
26+
self.directions= {'left': (-self.move_speed,0),'right': (self.move_speed,0),'up': (0,-self.move_speed),'down': (0,self.move_speed)}
27+
self.keys= ['left','right','up','down']
28+
self.direction= (0,0)
29+
30+
defmove_to_start_pos(self):
31+
self.rect.x=self.abs_x
32+
self.rect.y=self.abs_y
33+
34+
defis_collide(self,x,y,walls_collide_list):
35+
tmp_rect=self.rect.move(x,y)
36+
iftmp_rect.collidelist(walls_collide_list)==-1:
37+
returnFalse
38+
returnTrue
39+
40+
def_animate(self):
41+
self.img_name=f'{self.moving_dir}.png'
42+
self.image=pygame.image.load(self.img_path+self.img_name)
43+
self.image=pygame.transform.scale(self.image, (CHAR_SIZE,CHAR_SIZE))
44+
self.rect=self.image.get_rect(topleft=(self.rect.x,self.rect.y))
45+
46+
defupdate(self,walls_collide_list):
47+
# ghost movement
48+
available_moves= []
49+
forkeyinself.keys:
50+
ifnotself.is_collide(*self.directions[key],walls_collide_list):
51+
available_moves.append(key)
52+
53+
randomizing=Falseiflen(available_moves)<=2andself.direction!= (0,0)elseTrue
54+
# 60% chance of randomizing ghost move
55+
ifrandomizingandrandom.randrange(0,100 )<=60:
56+
self.moving_dir=random.choice(available_moves)
57+
self.direction=self.directions[self.moving_dir]
58+
59+
ifnotself.is_collide(*self.direction,walls_collide_list):
60+
self.rect.move_ip(self.direction)
61+
else:
62+
self.direction= (0,0)
63+
64+
# teleporting to the other side of the map
65+
ifself.rect.right<=0:
66+
self.rect.x=WIDTH
67+
elifself.rect.left>=WIDTH:
68+
self.rect.x=0
69+
70+
self._animate()

‎gui-programming/pacman-game/main.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
importpygame,sys
2+
fromsettingsimportWIDTH,HEIGHT,NAV_HEIGHT
3+
fromworldimportWorld
4+
5+
pygame.init()
6+
7+
screen=pygame.display.set_mode((WIDTH,HEIGHT+NAV_HEIGHT))
8+
pygame.display.set_caption("PacMan")
9+
10+
classMain:
11+
def__init__(self,screen):
12+
self.screen=screen
13+
self.FPS=pygame.time.Clock()
14+
15+
defmain(self):
16+
world=World(self.screen)
17+
whileTrue:
18+
self.screen.fill("black")
19+
20+
foreventinpygame.event.get():
21+
ifevent.type==pygame.QUIT:
22+
pygame.quit()
23+
sys.exit()
24+
25+
world.update()
26+
pygame.display.update()
27+
self.FPS.tick(30)
28+
29+
30+
if__name__=="__main__":
31+
play=Main(screen)
32+
play.main()

‎gui-programming/pacman-game/pac.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
importpygame
2+
3+
fromsettingsimportCHAR_SIZE,PLAYER_SPEED
4+
fromanimationimportimport_sprite
5+
6+
classPac(pygame.sprite.Sprite):
7+
def__init__(self,row,col):
8+
super().__init__()
9+
10+
self.abs_x= (row*CHAR_SIZE)
11+
self.abs_y= (col*CHAR_SIZE)
12+
13+
# pac animation
14+
self._import_character_assets()
15+
self.frame_index=0
16+
self.animation_speed=0.5
17+
self.image=self.animations["idle"][self.frame_index]
18+
self.rect=self.image.get_rect(topleft= (self.abs_x,self.abs_y))
19+
self.mask=pygame.mask.from_surface(self.image)
20+
21+
self.pac_speed=PLAYER_SPEED
22+
self.immune_time=0
23+
self.immune=False
24+
25+
self.directions= {'left': (-PLAYER_SPEED,0),'right': (PLAYER_SPEED,0),'up': (0,-PLAYER_SPEED),'down': (0,PLAYER_SPEED)}
26+
self.keys= {'left':pygame.K_LEFT,'right':pygame.K_RIGHT,'up':pygame.K_UP,'down':pygame.K_DOWN}
27+
self.direction= (0,0)
28+
29+
# pac status
30+
self.status="idle"
31+
self.life=3
32+
self.pac_score=0
33+
34+
35+
# gets all the image needed for animating specific player action
36+
def_import_character_assets(self):
37+
character_path="assets/pac/"
38+
self.animations= {
39+
"up": [],
40+
"down": [],
41+
"left": [],
42+
"right": [],
43+
"idle": [],
44+
"power_up": []
45+
}
46+
foranimationinself.animations.keys():
47+
full_path=character_path+animation
48+
self.animations[animation]=import_sprite(full_path)
49+
50+
51+
def_is_collide(self,x,y):
52+
tmp_rect=self.rect.move(x,y)
53+
iftmp_rect.collidelist(self.walls_collide_list)==-1:
54+
returnFalse
55+
returnTrue
56+
57+
58+
defmove_to_start_pos(self):
59+
self.rect.x=self.abs_x
60+
self.rect.y=self.abs_y
61+
62+
63+
# update with sprite/sheets
64+
defanimate(self,pressed_key,walls_collide_list):
65+
animation=self.animations[self.status]
66+
67+
# loop over frame index
68+
self.frame_index+=self.animation_speed
69+
ifself.frame_index>=len(animation):
70+
self.frame_index=0
71+
image=animation[int(self.frame_index)]
72+
self.image=pygame.transform.scale(image, (CHAR_SIZE,CHAR_SIZE))
73+
74+
self.walls_collide_list=walls_collide_list
75+
forkey,key_valueinself.keys.items():
76+
ifpressed_key[key_value]andnotself._is_collide(*self.directions[key]):
77+
self.direction=self.directions[key]
78+
self.status=keyifnotself.immuneelse"power_up"
79+
break
80+
81+
ifnotself._is_collide(*self.direction):
82+
self.rect.move_ip(self.direction)
83+
self.status=self.statusifnotself.immuneelse"power_up"
84+
ifself._is_collide(*self.direction):
85+
self.status="idle"ifnotself.immuneelse"power_up"
86+
87+
88+
defupdate(self):
89+
# Timer based from FPS count
90+
self.immune=Trueifself.immune_time>0elseFalse
91+
self.immune_time-=1ifself.immune_time>0else0
92+
93+
self.rect=self.image.get_rect(topleft=(self.rect.x,self.rect.y))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pygame
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
MAP= [
2+
['1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1'],
3+
['1',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','1'],
4+
['1','B','1','1',' ','1','1','1',' ','1',' ','1','1','1',' ','1','1','B','1'],
5+
['1',' ',' ',' ',' ','1',' ',' ',' ','1',' ',' ',' ','1',' ',' ',' ',' ','1'],
6+
['1','1',' ','1',' ','1',' ','1',' ','1',' ','1',' ','1',' ','1',' ','1','1'],
7+
['1',' ',' ','1',' ',' ',' ','1',' ',' ',' ','1',' ',' ',' ','1',' ',' ','1'],
8+
['1',' ','1','1','1','1',' ','1','1','1','1','1',' ','1','1','1','1',' ','1'],
9+
['1',' ',' ',' ',' ',' ',' ',' ',' ','r',' ',' ',' ',' ',' ',' ',' ',' ','1'],
10+
['1','1',' ','1','1','1',' ','1','1','-','1','1',' ','1','1','1',' ','1','1'],
11+
[' ',' ',' ',' ',' ','1',' ','1','s','p','o','1',' ','1',' ',' ',' ',' ',' '],
12+
['1','1',' ','1',' ','1',' ','1','1','1','1','1',' ','1',' ','1',' ','1','1'],
13+
['1',' ',' ','1',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','1',' ',' ','1'],
14+
['1',' ','1','1','1','1',' ','1','1','1','1','1',' ','1','1','1','1',' ','1'],
15+
['1',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','1'],
16+
['1','1','1',' ','1','1','1',' ','1','1','1',' ','1','1','1',' ','1','1','1'],
17+
['1',' ',' ',' ','1',' ',' ',' ',' ','P',' ',' ',' ',' ','1',' ',' ',' ','1'],
18+
['1','B','1',' ','1',' ','1',' ','1','1','1',' ','1',' ','1',' ','1','B','1'],
19+
['1',' ','1',' ',' ',' ','1',' ',' ',' ',' ',' ','1',' ',' ',' ','1',' ','1'],
20+
['1',' ','1','1','1',' ','1','1','1',' ','1','1','1',' ','1','1','1',' ','1'],
21+
['1',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','1'],
22+
['1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1']
23+
]
24+
25+
BOARD_RATIO= (len(MAP[0]),len(MAP))
26+
CHAR_SIZE=32
27+
28+
WIDTH,HEIGHT= (BOARD_RATIO[0]*CHAR_SIZE,BOARD_RATIO[1]*CHAR_SIZE)
29+
NAV_HEIGHT=64
30+
31+
PLAYER_SPEED=CHAR_SIZE//4
32+
33+
GHOST_SPEED=4

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp