用pygame学习初级python(二) 15.5.11

  闲得无聊,对第一版的东西做了一些修改,让它更像一个游戏,也具有一些可玩性。

项目的github地址: https://github.com/lfkdsk/BrainHole_pygame

1.人物类进行了一些修改,添加了怪物类的生成机制:

  1 __author__ = 'Administrator'
  2 import pygame
  3 
  4 Window_width = 1365
  5 Window_height = 768
  6 screen = pygame.display.set_mode((Window_width, Window_height))
  7 first_location = [screen.get_width() / 2, screen.get_height() / 2]
  8 Player_image_up = ['resources/image/up-1.png', 'resources/image/up-2.png', 'resources/image/up-3.png',
  9                    'resources/image/up-4.png']
 10 Player_image_down = ['resources/image/down-1.png', 'resources/image/down-2.png', 'resources/image/down-3.png',
 11                      'resources/image/down-4.png']
 12 Player_image_left = ['resources/image/left-1.png', 'resources/image/left-2.png', 'resources/image/left-3.png',
 13                      'resources/image/left-4.png']
 14 Player_image_right = ['resources/image/right-1.png', 'resources/image/right-2.png', 'resources/image/right-3.png',
 15                       'resources/image/right-4.png']
 16 Enemy_image = ["resources/image/zm-1.png", "resources/image/zm-2.png"]
 17 Bullets = []
 18 
 19 
 20 class Enemy(pygame.sprite.Sprite):
 21     def __init__(self, init_pos):
 22         pygame.sprite.Sprite.__init__(self)
 23         self.image = pygame.image.load("resources/image/zm-2.png")
 24         self.rect = self.image.get_rect()
 25         self.rect.topleft = init_pos
 26         self.speed = 2
 27         self.down_index = 0
 28         self.walk = 0
 29 
 30     def move(self):
 31         topleft = self.rect.topleft
 32         if self.walk > 40:
 33             self.walk = 0
 34             self.image = pygame.image.load("resources/image/zm-2.png")
 35         if self.walk == 20:
 36             self.image = pygame.image.load("resources/image/zm-1.png")
 37         self.rect = self.image.get_rect()
 38         self.rect.topleft = topleft
 39         self.walk += 1
 40         self.rect.centerx -= self.speed
 41 
 42 
 43 class Bullet(pygame.sprite.Sprite):
 44     def __init__(self, init_pos):
 45         pygame.sprite.Sprite.__init__(self)
 46         self.image = pygame.image.load('resources/image/bullet.png')
 47         self.rect = self.image.get_rect()
 48         self.rect.midtop = init_pos
 49         self.speed = 10
 50         self.walk = 0
 51 
 52     def move(self, button_flag):
 53         if button_flag == 1:
 54             self.rect.centerx -= self.speed
 55         elif button_flag == 0:
 56             self.rect.centerx += self.speed
 57         elif button_flag == 2:
 58             self.rect.centery += self.speed
 59         elif button_flag == 3:
 60             self.rect.centery -= self.speed
 61             # self.rect.centery += self.speed
 62             # screen.blit(self.image,self.rect)
 63             # pygame.display.update()
 64 
 65 
 66 class Player(pygame.sprite.Sprite):
 67     def __init__(self):
 68         pygame.sprite.Sprite.__init__(self)
 69         self.image = pygame.image.load('resources/image/down-1.png')
 70         self.rect = self.image.get_rect()
 71         self.rect.center = first_location
 72         self.is_hit = False
 73         self.speed = 15
 74         self.down = 0
 75         self.left = 0
 76         self.right = 0
 77         self.up = 0
 78         # print self.rect.center
 79 
 80     def turn(self, distance):
 81         center = self.rect.center
 82         if distance == 2:
 83             if self.down > 3:
 84                 self.down = 0
 85             self.image = pygame.image.load(Player_image_down[self.down])
 86             self.rect = self.image.get_rect()
 87             self.rect.center = center
 88             self.down += 1
 89             # screen.blit(player.image, player.rect)
 90         elif distance == 0:
 91             if self.left > 3:
 92                 self.left = 0
 93             self.image = pygame.image.load(Player_image_left[self.left])
 94             self.rect = self.image.get_rect()
 95             self.rect.center = center
 96             self.left += 1
 97         elif distance == 3:
 98             if self.up > 3:
 99                 self.up = 0
100             self.image = pygame.image.load(Player_image_up[self.up])
101             self.rect = self.image.get_rect()
102             self.rect.center = center
103             self.up += 1
104         elif distance == 1:
105             if self.right > 3:
106                 self.right = 0
107             self.image = pygame.image.load(Player_image_right[self.right])
108             self.rect = self.image.get_rect()
109             self.rect.center = center
110             self.right += 1
111 
112     def move(self, temper):
113         temper = int(temper)
114         if temper == 0:
115             for temp in range(1, 10):
116                 self.rect.centerx -= 3
117             print self.rect.centerx, self.rect.centery
118         if temper == 1:
119             self.rect.centerx += self.speed
120             print self.rect.centery
121         if temper == 2:
122             self.rect.centery -= self.speed
123             print self.rect.centerx, self.rect.centery
124         if temper == 3:
125             self.rect.centery += self.speed
126         if 0 > self.rect.centerx or self.rect.centerx > screen.get_width():
127             self.rect.centerx = -self.rect.centerx
128         if 0 > self.rect.centery or self.rect.centery > screen.get_height():
129             self.rect.centery = -self.rect.centery
130 
131     def shoot(self):
132         bullet = Bullet(self.rect.midtop)
133         Bullets.append(bullet)
View Code

 

 1 __author__ = 'Administrator'
 2 import pygame
 3 
 4 Window_width = 1365
 5 Window_height = 768
 6 screen = pygame.display.set_mode((Window_width, Window_height))
 7 first_location = [screen.get_width() / 2, screen.get_height() / 2]
 8 Player_image_up = ['resources/image/up-1.png', 'resources/image/up-2.png', 'resources/image/up-3.png',
 9                    'resources/image/up-4.png']
10 Player_image_down = ['resources/image/down-1.png', 'resources/image/down-2.png', 'resources/image/down-3.png',
11                      'resources/image/down-4.png']
12 Player_image_left = ['resources/image/left-1.png', 'resources/image/left-2.png', 'resources/image/left-3.png',
13                      'resources/image/left-4.png']
14 Player_image_right = ['resources/image/right-1.png', 'resources/image/right-2.png', 'resources/image/right-3.png',
15                       'resources/image/right-4.png']
16 Enemy_image = ["resources/image/zm-1.png", "resources/image/zm-2.png"]
17 Bullets = []

 

首先,初始化了屏幕的大小,并且设置了图片资源。

 1 class Enemy(pygame.sprite.Sprite):
 2     def __init__(self, init_pos):
 3         pygame.sprite.Sprite.__init__(self)
 4         self.image = pygame.image.load("resources/image/zm-2.png")
 5         self.rect = self.image.get_rect()
 6         self.rect.topleft = init_pos
 7         self.speed = 2
 8         self.down_index = 0
 9         self.walk = 0
10 
11     def move(self):
12         topleft = self.rect.topleft
13         if self.walk > 40:
14             self.walk = 0
15             self.image = pygame.image.load("resources/image/zm-2.png")
16         if self.walk == 20:
17             self.image = pygame.image.load("resources/image/zm-1.png")
18         self.rect = self.image.get_rect()
19         self.rect.topleft = topleft
20         self.walk += 1
21         self.rect.centerx -= self.speed

 

怪物类设置了一个move函数,在初始化里设定了一个walk的值,能让在自增的过程中,不断的切换图片。

就是这样的两张图片。

 1 class Bullet(pygame.sprite.Sprite):
 2     def __init__(self, init_pos):
 3         pygame.sprite.Sprite.__init__(self)
 4         self.image = pygame.image.load('resources/image/bullet.png')
 5         self.rect = self.image.get_rect()
 6         self.rect.midtop = init_pos
 7         self.speed = 10
 8         self.walk = 0
 9 
10     def move(self, button_flag):
11         if button_flag == 1:
12             self.rect.centerx -= self.speed
13         elif button_flag == 0:
14             self.rect.centerx += self.speed
15         elif button_flag == 2:
16             self.rect.centery += self.speed
17         elif button_flag == 3:
18             self.rect.centery -= self.speed

 

子弹类添加了在不同移动方向上的不同移动方法,可以向左右两个方向发射。

(发射飞剑!)

 1 class Player(pygame.sprite.Sprite):
 2     def __init__(self):
 3         pygame.sprite.Sprite.__init__(self)
 4         self.image = pygame.image.load('resources/image/down-1.png')
 5         self.rect = self.image.get_rect()
 6         self.rect.center = first_location
 7         self.is_hit = False
 8         self.speed = 15
 9         self.down = 0
10         self.left = 0
11         self.right = 0
12         self.up = 0
13         # print self.rect.center
14 
15     def turn(self, distance):
16         center = self.rect.center
17         if distance == 2:
18             if self.down > 3:
19                 self.down = 0
20             self.image = pygame.image.load(Player_image_down[self.down])
21             self.rect = self.image.get_rect()
22             self.rect.center = center
23             self.down += 1
24             # screen.blit(player.image, player.rect)
25         elif distance == 0:
26             if self.left > 3:
27                 self.left = 0
28             self.image = pygame.image.load(Player_image_left[self.left])
29             self.rect = self.image.get_rect()
30             self.rect.center = center
31             self.left += 1
32         elif distance == 3:
33             if self.up > 3:
34                 self.up = 0
35             self.image = pygame.image.load(Player_image_up[self.up])
36             self.rect = self.image.get_rect()
37             self.rect.center = center
38             self.up += 1
39         elif distance == 1:
40             if self.right > 3:
41                 self.right = 0
42             self.image = pygame.image.load(Player_image_right[self.right])
43             self.rect = self.image.get_rect()
44             self.rect.center = center
45             self.right += 1
46 
47     def move(self, temper):
48         temper = int(temper)
49         if temper == 0:
50             for temp in range(1, 10):
51                 self.rect.centerx -= 3
52             print self.rect.centerx, self.rect.centery
53         if temper == 1:
54             self.rect.centerx += self.speed
55             print self.rect.centery
56         if temper == 2:
57             self.rect.centery -= self.speed
58             print self.rect.centerx, self.rect.centery
59         if temper == 3:
60             self.rect.centery += self.speed
61         if 0 > self.rect.centerx or self.rect.centerx > screen.get_width():
62             self.rect.centerx = -self.rect.centerx
63         if 0 > self.rect.centery or self.rect.centery > screen.get_height():
64             self.rect.centery = -self.rect.centery
65 
66     def shoot(self):
67         bullet = Bullet(self.rect.midtop)
68         Bullets.append(bullet)

 

修改了turn函数让图片不断切换的,在字符数组里不断的进行切换图片,做出人物(李逍遥)走路的样子。

(利用图片的反复切换)

2.主函数体的修改:

  1 import random
  2 
  3 __author__ = 'Administrator'
  4 
  5 from pygame.locals import *
  6 from Class import *
  7 from sys import exit
  8 
  9 
 10 background_image_filename = 'resources/image/gamestart.png'
 11 start_image = 'resources/image/bg2.png'
 12 dead_image = 'resources/image/dead.png'
 13 
 14 Score = 0
 15 x = 0
 16 y = 0
 17 pygame.init()
 18 pygame.mixer.init()
 19 background = pygame.image.load(background_image_filename).convert()
 20 pygame.key.set_repeat(100, 100)
 21 clock = pygame.time.Clock()
 22 
 23 pygame.mixer.music.load("resources/music/butterflylove.mp3")# 初始化背景音乐
 24 pygame.mixer.music.set_volume(0.3)
 25 pygame.mixer.music.play(-1)
 26 
 27 button_music = pygame.mixer.Sound("resources/music/bullet.wav")# 初始化发射音乐
 28 button_music.set_volume(10)
 29 # music
 30 
 31 def Waitkey():
 32     while True:
 33         for event in pygame.event.get():
 34             if event.type == QUIT:
 35                 exit()
 36             if event.type == KEYDOWN:
 37                 if event.key == K_ESCAPE:  # pressing escape quits
 38                     exit()
 39                 if event.key == K_RETURN:
 40                     return
 41 # font
 42 flag = 0
 43 button_flag = 0
 44 enemies = []
 45 enemies_down = []
 46 enemy_frequency = 0
 47 running = True
 48 while running:
 49     clock.tick(60)    # 强置设定为不超过60帧每秒
 50     if enemy_frequency % 50 == 0:
 51         enemy1_pos = [screen.get_width(), random.randint(0, screen.get_height())]# 在屏幕的右侧随机生成怪物
 52         enemy1 = Enemy(enemy1_pos)
 53         enemies.append(enemy1)                
 54     enemy_frequency += 1
 55     if enemy_frequency >= 100:
 56         enemy_frequency = 0
 57     for event in pygame.event.get():
 58         if event.type == QUIT:
 59             exit()
 60         if event.type == KEYDOWN:
 61             if flag == 0:
 62                 if event.key == pygame.K_1:
 63                     background = pygame.image.load(start_image).convert()
 64                     player = Player()
 65                     print screen.get_width()
 66                     print screen.get_height()
 67                     flag = 1
 68             elif event.key == pygame.K_4:
 69                 exit()
 70             elif event.key == pygame.K_RIGHT:            # 分别有不同的移动方向
 71                 if flag == 1:
 72                     player.turn(1)
 73                     player.move(1)
 74                     button_flag = 0
 75                     # flag == 1
 76                     print "buttonflag" and button_flag
 77             elif event.key == pygame.K_LEFT:
 78                 if flag == 1:
 79                     player.turn(0)
 80                     player.move(0)
 81                     button_flag = 1
 82                     print "buttonflag" and button_flag
 83             elif event.key == pygame.K_UP:
 84                 if flag == 1:
 85                     player.turn(3)
 86                     player.move(2)
 87                     button_flag = 3
 88                     # flag == 1
 89             elif event.key == pygame.K_DOWN:
 90                 if flag == 1:
 91                     player.turn(2)
 92                     player.move(3)
 93                     button_flag = 2
 94             elif event.key == pygame.K_x:
 95                 if flag == 1:
 96                     player.shoot()
 97                     button_music.play()
 98                     print("shoot")
 99     screen.blit(background, (x, y))
100     if flag == 1:
101         score_font = pygame.font.Font(None, 50)                  # 初始化在左上角的score分数
102         score_surf = score_font.render("Score: " + str(Score), 1, (0, 0, 0))
103         screen.blit(score_surf, [10, 10])
104         screen.blit(player.image, player.rect)
105         for enemy in enemies:
106             enemy.move()
107             screen.blit(enemy.image, enemy.rect)
108             if enemy.rect.left < 0:
109                 enemies.remove(enemy)
110                 break
111             if pygame.sprite.collide_circle(enemy, player):
112                 enemies_down.append(enemy)
113                 enemies.remove(enemy)
114                 # player.is_hit = True
115                 Score -= 1                      # 每次人物和怪物碰撞的时候分数就会-1
116                 # Game_Over()
117                 break
118         for bullet in Bullets:
119             bullet.move(button_flag)
120             screen.blit(bullet.image, bullet.rect)
121             for enemy in enemies:
122                 if pygame.sprite.collide_circle(bullet, enemy):
123                     enemies_down.append(enemy)
124                     enemies.remove(enemy)
125                     Bullets.remove(bullet)
126                     Score += 1                      # 然后只要子弹和怪物碰撞子弹会消失然后怪物消失,分数+1
127                     break
128             if bullet is not None:
129                 if bullet.rect.bottom < 0 or bullet.rect.centerx > screen.get_width() or bullet.rect.centerx < 0:
130                     Bullets.remove(bullet)
131         if Score < -10:
132             background_dead = pygame.image.load(dead_image).convert()    # -10分以下就会播放失败图片然后退出
133             screen.blit(background_dead, (x, y))
134     pygame.display.update()
135     if flag == 0:
136         pygame.display.update()

 

(死亡的界面)

注释里写的很详细了哦,这次改完之后会出现可随按键移动的子弹类似飞剑的效果哦!

好了第二篇就是这样,喜欢就点赞吧,=-=!

项目的github地址: https://github.com/lfkdsk/BrainHole_pygame

 

posted @ 2015-05-11 16:42  刘丰恺  阅读(1525)  评论(4编辑  收藏