您所在的位置:小祥子 » 编程 » Python » 正文

用pygame学习初级python(二) 15.5.11

时间:2015-05-11 编辑:刘丰恺 来源:本站整理

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

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

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

 __author__ = 'Administrator'
 import pygame
 
 Window_width = 1365
 Window_height = 768
 screen = pygame.display.set_mode((Window_width, Window_height))
 first_location = [screen.get_width() / 2, screen.get_height() / 2]
 Player_image_up = ['resources/image/up-1.png', 'resources/image/up-2.png', 'resources/image/up-3.png',
                    'resources/image/up-4.png']
 Player_image_down = ['resources/image/down-1.png', 'resources/image/down-2.png', 'resources/image/down-3.png',
                      'resources/image/down-4.png']
 Player_image_left = ['resources/image/left-1.png', 'resources/image/left-2.png', 'resources/image/left-3.png',
                      'resources/image/left-4.png']
 Player_image_right = ['resources/image/right-1.png', 'resources/image/right-2.png', 'resources/image/right-3.png',
                       'resources/image/right-4.png']
 Enemy_image = ["resources/image/zm-1.png", "resources/image/zm-2.png"]
 Bullets = []
 
 
 class Enemy(pygame.sprite.Sprite):
     def __init__(self, init_pos):
         pygame.sprite.Sprite.__init__(self)
         self.image = pygame.image.load("resources/image/zm-2.png")
         self.rect = self.image.get_rect()
         self.rect.topleft = init_pos
         self.speed = 2
         self.down_index = 0
         self.walk = 0
 
     def move(self):
         topleft = self.rect.topleft
         if self.walk > 40:
             self.walk = 0
             self.image = pygame.image.load("resources/image/zm-2.png")
         if self.walk == 20:
             self.image = pygame.image.load("resources/image/zm-1.png")
         self.rect = self.image.get_rect()
         self.rect.topleft = topleft
         self.walk += 1
         self.rect.centerx -= self.speed
 
 
 class Bullet(pygame.sprite.Sprite):
     def __init__(self, init_pos):
         pygame.sprite.Sprite.__init__(self)
         self.image = pygame.image.load('resources/image/bullet.png')
         self.rect = self.image.get_rect()
         self.rect.midtop = init_pos
         self.speed = 10
         self.walk = 0
 
     def move(self, button_flag):
         if button_flag == 1:
             self.rect.centerx -= self.speed
         elif button_flag == 0:
             self.rect.centerx += self.speed
         elif button_flag == 2:
             self.rect.centery += self.speed
         elif button_flag == 3:
             self.rect.centery -= self.speed
             # self.rect.centery += self.speed
             # screen.blit(self.image,self.rect)
             # pygame.display.update()
 
 
 class Player(pygame.sprite.Sprite):
     def __init__(self):
         pygame.sprite.Sprite.__init__(self)
         self.image = pygame.image.load('resources/image/down-1.png')
         self.rect = self.image.get_rect()
         self.rect.center = first_location
         self.is_hit = False
         self.speed = 15
         self.down = 0
         self.left = 0
         self.right = 0
         self.up = 0
         # print self.rect.center
 
     def turn(self, distance):
         center = self.rect.center
         if distance == 2:
             if self.down > 3:
                 self.down = 0
             self.image = pygame.image.load(Player_image_down[self.down])
             self.rect = self.image.get_rect()
             self.rect.center = center
             self.down += 1
             # screen.blit(player.image, player.rect)
         elif distance == 0:
             if self.left > 3:
                 self.left = 0
             self.image = pygame.image.load(Player_image_left[self.left])
             self.rect = self.image.get_rect()
             self.rect.center = center
             self.left += 1
         elif distance == 3:
             if self.up > 3:
                 self.up = 0
             self.image = pygame.image.load(Player_image_up[self.up])
             self.rect = self.image.get_rect()
             self.rect.center = center
             self.up += 1
         elif distance == 1:
             if self.right > 3:
                 self.right = 0
             self.image = pygame.image.load(Player_image_right[self.right])
             self.rect = self.image.get_rect()
             self.rect.center = center
             self.right += 1
 
     def move(self, temper):
         temper = int(temper)
         if temper == 0:
             for temp in range(1, 10):
                 self.rect.centerx -= 3
             print self.rect.centerx, self.rect.centery
         if temper == 1:
             self.rect.centerx += self.speed
             print self.rect.centery
         if temper == 2:
             self.rect.centery -= self.speed
             print self.rect.centerx, self.rect.centery
         if temper == 3:
             self.rect.centery += self.speed
         if 0 > self.rect.centerx or self.rect.centerx > screen.get_width():
             self.rect.centerx = -self.rect.centerx
         if 0 > self.rect.centery or self.rect.centery > screen.get_height():
             self.rect.centery = -self.rect.centery
 
     def shoot(self):
         bullet = Bullet(self.rect.midtop)
         Bullets.append(bullet)
View Code
 __author__ = 'Administrator'
 import pygame
 
 Window_width = 1365
 Window_height = 768
 screen = pygame.display.set_mode((Window_width, Window_height))
 first_location = [screen.get_width() / 2, screen.get_height() / 2]
 Player_image_up = ['resources/image/up-1.png', 'resources/image/up-2.png', 'resources/image/up-3.png',
                    'resources/image/up-4.png']
 Player_image_down = ['resources/image/down-1.png', 'resources/image/down-2.png', 'resources/image/down-3.png',
                      'resources/image/down-4.png']
 Player_image_left = ['resources/image/left-1.png', 'resources/image/left-2.png', 'resources/image/left-3.png',
                      'resources/image/left-4.png']
 Player_image_right = ['resources/image/right-1.png', 'resources/image/right-2.png', 'resources/image/right-3.png',
                       'resources/image/right-4.png']
 Enemy_image = ["resources/image/zm-1.png", "resources/image/zm-2.png"]
 Bullets = []

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

 class Enemy(pygame.sprite.Sprite):
     def __init__(self, init_pos):
         pygame.sprite.Sprite.__init__(self)
         self.image = pygame.image.load("resources/image/zm-2.png")
         self.rect = self.image.get_rect()
         self.rect.topleft = init_pos
         self.speed = 2
         self.down_index = 0
         self.walk = 0
 
     def move(self):
         topleft = self.rect.topleft
         if self.walk > 40:
             self.walk = 0
             self.image = pygame.image.load("resources/image/zm-2.png")
         if self.walk == 20:
             self.image = pygame.image.load("resources/image/zm-1.png")
         self.rect = self.image.get_rect()
         self.rect.topleft = topleft
         self.walk += 1
         self.rect.centerx -= self.speed

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

就是这样的两张图片。

 class Bullet(pygame.sprite.Sprite):
     def __init__(self, init_pos):
         pygame.sprite.Sprite.__init__(self)
         self.image = pygame.image.load('resources/image/bullet.png')
         self.rect = self.image.get_rect()
         self.rect.midtop = init_pos
         self.speed = 10
         self.walk = 0
 
     def move(self, button_flag):
         if button_flag == 1:
             self.rect.centerx -= self.speed
         elif button_flag == 0:
             self.rect.centerx += self.speed
         elif button_flag == 2:
             self.rect.centery += self.speed
         elif button_flag == 3:
             self.rect.centery -= self.speed

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

(发射飞剑!)

 class Player(pygame.sprite.Sprite):
     def __init__(self):
         pygame.sprite.Sprite.__init__(self)
         self.image = pygame.image.load('resources/image/down-1.png')
         self.rect = self.image.get_rect()
         self.rect.center = first_location
         self.is_hit = False
         self.speed = 15
         self.down = 0
         self.left = 0
         self.right = 0
         self.up = 0
         # print self.rect.center
 
     def turn(self, distance):
         center = self.rect.center
         if distance == 2:
             if self.down > 3:
                 self.down = 0
             self.image = pygame.image.load(Player_image_down[self.down])
             self.rect = self.image.get_rect()
             self.rect.center = center
             self.down += 1
             # screen.blit(player.image, player.rect)
         elif distance == 0:
             if self.left > 3:
                 self.left = 0
             self.image = pygame.image.load(Player_image_left[self.left])
             self.rect = self.image.get_rect()
             self.rect.center = center
             self.left += 1
         elif distance == 3:
             if self.up > 3:
                 self.up = 0
             self.image = pygame.image.load(Player_image_up[self.up])
             self.rect = self.image.get_rect()
             self.rect.center = center
             self.up += 1
         elif distance == 1:
             if self.right > 3:
                 self.right = 0
             self.image = pygame.image.load(Player_image_right[self.right])
             self.rect = self.image.get_rect()
             self.rect.center = center
             self.right += 1
 
     def move(self, temper):
         temper = int(temper)
         if temper == 0:
             for temp in range(1, 10):
                 self.rect.centerx -= 3
             print self.rect.centerx, self.rect.centery
         if temper == 1:
             self.rect.centerx += self.speed
             print self.rect.centery
         if temper == 2:
             self.rect.centery -= self.speed
             print self.rect.centerx, self.rect.centery
         if temper == 3:
             self.rect.centery += self.speed
         if 0 > self.rect.centerx or self.rect.centerx > screen.get_width():
             self.rect.centerx = -self.rect.centerx
         if 0 > self.rect.centery or self.rect.centery > screen.get_height():
             self.rect.centery = -self.rect.centery
 
     def shoot(self):
         bullet = Bullet(self.rect.midtop)
         Bullets.append(bullet)

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

(利用图片的反复切换)

2.主函数体的修改:

 import random
 
 __author__ = 'Administrator'
 
 from pygame.locals import *
 from Class import *
 from sys import exit
 
 
 background_image_filename = 'resources/image/gamestart.png'
 start_image = 'resources/image/bg2.png'
 dead_image = 'resources/image/dead.png'
 
 Score = 0
 x = 0
 y = 0
 pygame.init()
 pygame.mixer.init()
 background = pygame.image.load(background_image_filename).convert()
 pygame.key.set_repeat(100, 100)
 clock = pygame.time.Clock()
 
 pygame.mixer.music.load("resources/music/butterflylove.mp3")# 初始化背景音乐
 pygame.mixer.music.set_volume(0.3)
 pygame.mixer.music.play(-1)
 
 button_music = pygame.mixer.Sound("resources/music/bullet.wav")# 初始化发射音乐
 button_music.set_volume(10)
 # music
 
 def Waitkey():
     while True:
         for event in pygame.event.get():
             if event.type == QUIT:
                 exit()
             if event.type == KEYDOWN:
                 if event.key == K_ESCAPE:  # pressing escape quits
                     exit()
                 if event.key == K_RETURN:
                     return
 # font
 flag = 0
 button_flag = 0
 enemies = []
 enemies_down = []
 enemy_frequency = 0
 running = True
 while running:
     clock.tick(60)    # 强置设定为不超过60帧每秒
     if enemy_frequency % 50 == 0:
         enemy1_pos = [screen.get_width(), random.randint(0, screen.get_height())]# 在屏幕的右侧随机生成怪物
         enemy1 = Enemy(enemy1_pos)
         enemies.append(enemy1)                
     enemy_frequency += 1
     if enemy_frequency >= 100:
         enemy_frequency = 0
     for event in pygame.event.get():
         if event.type == QUIT:
             exit()
         if event.type == KEYDOWN:
             if flag == 0:
                 if event.key == pygame.K_1:
                     background = pygame.image.load(start_image).convert()
                     player = Player()
                     print screen.get_width()
                     print screen.get_height()
                     flag = 1
             elif event.key == pygame.K_4:
                 exit()
             elif event.key == pygame.K_RIGHT:            # 分别有不同的移动方向
                 if flag == 1:
                     player.turn(1)
                     player.move(1)
                     button_flag = 0
                     # flag == 1
                     print "buttonflag" and button_flag
             elif event.key == pygame.K_LEFT:
                 if flag == 1:
                     player.turn(0)
                     player.move(0)
                     button_flag = 1
                     print "buttonflag" and button_flag
             elif event.key == pygame.K_UP:
                 if flag == 1:
                     player.turn(3)
                     player.move(2)
                     button_flag = 3
                     # flag == 1
             elif event.key == pygame.K_DOWN:
                 if flag == 1:
                     player.turn(2)
                     player.move(3)
                     button_flag = 2
             elif event.key == pygame.K_x:
                 if flag == 1:
                     player.shoot()
                     button_music.play()
                     print("shoot")
     screen.blit(background, (x, y))
     if flag == 1:
         score_font = pygame.font.Font(None, 50)                  # 初始化在左上角的score分数
         score_surf = score_font.render("Score: " + str(Score), 1, (0, 0, 0))
         screen.blit(score_surf, [10, 10])
         screen.blit(player.image, player.rect)
         for enemy in enemies:
             enemy.move()
             screen.blit(enemy.image, enemy.rect)
             if enemy.rect.left < 0:
                 enemies.remove(enemy)
                 break
             if pygame.sprite.collide_circle(enemy, player):
                 enemies_down.append(enemy)
                 enemies.remove(enemy)
                 # player.is_hit = True
                 Score -= 1                      # 每次人物和怪物碰撞的时候分数就会-1
                 # Game_Over()
                 break
         for bullet in Bullets:
             bullet.move(button_flag)
             screen.blit(bullet.image, bullet.rect)
             for enemy in enemies:
                 if pygame.sprite.collide_circle(bullet, enemy):
                     enemies_down.append(enemy)
                     enemies.remove(enemy)
                     Bullets.remove(bullet)
                     Score += 1                      # 然后只要子弹和怪物碰撞子弹会消失然后怪物消失,分数+1
                     break
             if bullet is not None:
                 if bullet.rect.bottom < 0 or bullet.rect.centerx > screen.get_width() or bullet.rect.centerx < 0:
                     Bullets.remove(bullet)
         if Score < -10:
             background_dead = pygame.image.load(dead_image).convert()    # -10分以下就会播放失败图片然后退出
             screen.blit(background_dead, (x, y))
     pygame.display.update()
     if flag == 0:
         pygame.display.update()

(死亡的界面)

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

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

项目的github地址: https://github.com/lfkdsk/BrainHole_pygame
关键词:学习