这星期长见识了,虽然每星期都在学习新的内容,都在长见识,但是这次挺惊讶的,竟然可以用python,vi,等结合起来做小游戏,然后懂得了做游戏的原理,知道了怎么做游戏。虽然可能只懂的
简单的小游戏制作,但是感觉也挺厉害的。真的是长见识了!
1 import random
2 import pygame
3 SCREEN_RECT = pygame.Rect(0,0,480,700)
4 FRAME_PER_SEC = 60
5 CREATE_ENEMY_EVENT =pygame.USEREVENT
6 CREATE_BULLET_EVENT =pygame.USEREVENT + 1
7
8 class GameSprite(pygame.sprite.Sprite):
9 def __init__(self,image_name,speed=1):
10 super().__init__()
11
12 self.image = pygame.image.load(image_name)
13 self.rect = self.image.get_rect()
14 self.speed = speed
15
16
17
18 def update(self):
19 self.rect.y += self.speed
20
21 class Background(GameSprite):
22 def __init__(self,is_alt=False):
23 image_name = "./images/background.png"
24 super().__init__(image_name)
25 if is_alt:
26 self.rect.y = -self.rect.height
27
28 def update(self):
29 super().update()
30 if self.rect.y > SCREEN_RECT.height:
31 self.rect.y = -self.rect.height
32
33 class Enemy(GameSprite):
34 def __init__(self):
35 image_name = "./images/enemy-1.gif"
36 super().__init__(image_name)
37 self.speed = random.randint(1,3)
38 max_x = SCREEN_RECT.width - self.rect.width
39 self.rect.x = random.randint(0,max_x)
40 self.rect.bottom = 0
41 def update(self):
42 super().update()
43 if self.rect.y >= SCREEN_RECT.height:
44 self.kill()
45 def __del__(self):
46 pass
47 class Hero(GameSprite):
48 def __init__(self):
49 image_name = "./images/hero.gif"
50 super().__init__(image_name,0)
51 self.speed1 = 0
52 self.rect.centerx = SCREEN_RECT.centerx
53 self.rect.bottom = SCREEN_RECT.bottom - 120
54 self.bullet_group = pygame.sprite.Group()
55 def update(self):
56 self.rect.x +=self.speed
57 self.rect.y += self.speed1
58 if self.rect.left < 0:
59 self.rect.left = 0
60 if self.rect.right > SCREEN_RECT.width:
61 self.rect.right = SCREEN_RECT.width
62
63 def fire(self):
64 bullet = Bullet()
65
66 bullet.rect.bottom = self.rect.y - 20
67 bullet.rect.centerx = self.rect.centerx
68 self.bullet_group.add(bullet)
69 class Bullet(GameSprite):
70 def __init__(self):
71 image_name = "./images/bullet1.png"
72 super().__init__(image_name,-10)
73 def update(self):
74 super().update()
75 if self.rect.bottom < 0:
76 self.kill()
这是设置基础命令 和条件
1 import pygame
2 from PlanSprite import *
3 class PlaneGame(object):
4 def __init__(self):
5 self.screen = pygame.display.set_mode(SCREEN_RECT.size)
6 self.clock = pygame.time.Clock()
7 self.__create_sprites()
8 pygame.time.set_timer(CREATE_ENEMY_EVENT,1000)
9 pygame.time.set_timer(CREATE_BULLET_EVENT,500)
10 self.enemy_group = pygame.sprite.Group()
11 def __create_sprites(self):
12 bg1 = Background()
13 bg2 = Background(True)
14 self.back_group = pygame.sprite.Group(bg1,bg2)
15
16 self.hero = Hero()
17 self.hero_group = pygame.sprite.Group(self.hero)
18 def start_game(self):
19 print("开始游戏")
20 while True:
21 self.clock.tick(FRAME_PER_SEC)
22 self.__event_handler()
23 self.__check_collide()
24 self.__update_sprites()
25 pygame.display.update()
26 def __event_handler(self):
27 for event in pygame.event.get():
28 if event.type == pygame.QUIT:
29 planeGame.__game_over()
30 elif event.type == CREATE_ENEMY_EVENT:
31 enemy = Enemy()
32 self.enemy_group.add(enemy)
33 elif event.type == CREATE_BULLET_EVENT:
34 self.hero.fire()
35 keys_pressed = pygame.key.get_pressed()
36 if keys_pressed[pygame.K_RIGHT]:
37 self.hero.speed = 2
38 self.hero.speed1 = 0
39 elif keys_pressed[pygame.K_LEFT]:
40 self.hero.speed = -2
41 self.hero.speed1 = 0
42 elif keys_pressed[pygame.K_UP]:
43 self.hero.speed1 = -2
44 self.hero.speed = 0
45 elif keys_pressed[pygame.K_DOWN]:
46 self.hero.speed1 = 2
47 self.hero.speed = 0
48 else:
49 self.hero.speed1 = 0
50 self.hero.speed = 0
51 def __check_collide(self):
52 b = pygame.sprite.groupcollide(self.hero.bullet_group,self.enemy_group,True,True)
53 print(b)
54 enemies = pygame.sprite.spritecollide(self.hero,self.enemy_group,True)
55 if len(enemies) > 0:
56 self.hero.kill()
57 planeGame.__game_over()
58 def __update_sprites(self):
59 self.back_group.update()
60 self.back_group.draw(self.screen)
61
62 self.enemy_group.update()
63 self.enemy_group.draw(self.screen)
64
65 self.hero_group.update()
66 self.hero_group.draw(self.screen)
67
68 self.hero.bullet_group.update()
69 self.hero.bullet_group.draw(self.screen)
70 @staticmethod
71 def game_over():
72 pygame.quit
73 exit()
74 if __name__ =="__main__":
75 game = PlaneGame()
76 game.start_game()
这里是执行的命令
上面图片是执行后的效果,自己目前没学太明白,还可以继续加强,设置分数,加入一些特殊道具等。