python从入门到实践13-5题目答案


判定代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import sys
from blood import Blood
from ball import Ball
import pygame
 
def check_keydown_events(event,blood):
if event.key == pygame.K_RIGHT:
blood.moving_r = True
elif event.key == pygame.K_LEFT:
blood.moving_l = True
elif event.key == pygame.K_ESCAPE:
sys.exit()
 
def check_keyup_events(event, ship):
if event.key == pygame.K_RIGHT:
ship.moving_r = False
elif event.key == pygame.K_LEFT:
ship.moving_l = False
 
def check_event(blood):
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
check_keydown_events(event, blood)
elif event.type == pygame.KEYUP:
check_keyup_events(event, blood)
 
def screen(balls,blood,screen,ai_settings):
screen.fill(ai_settings.color)
blood.update()
blood.blitme()
balls.draw(screen)
 
def creat_ball(balls,screen):
ball=Ball(screen)
balls.add(ball)
 
def creat_balls(balls, screen,bloods):
for b in balls:
b.rect.y += b.speed
if b.rect.y > b.screen_rect.bottom:
balls.remove(b)
if(len(balls))<=0:
creat_ball(balls, screen)
check_bullent_alien_collisions(balls, bloods)
def check_bullent_alien_collisions(balls,bloods)
pygame.sprite.groupcollide(balls,bloods,True,True)

挡板代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import pygame
from pygame.sprite import Sprite
class Blood(Sprite):
def __init__(self, screen, settings):
super().__init__()
self.screen = screen
"""获取飞船的移动速度"""
self.settings = settings
"""获取图片素材的外接矩形"""
self.image = pygame.image.load('./1.png')
self.rect = self.image.get_rect()  # 获取图片外接矩形
self.screen_rect = screen.get_rect()  # 获取屏幕外接矩形
"""飞船默认位置为底部中心"""
self.rect.centerx = self.screen_rect.centerx  # 屏幕中心
self.rect.bottom = self.screen_rect.bottom  # 屏幕底部
self.center = float(self.rect.centerx)
"""移动判定标签"""
self.moving_r = False
self.moving_l = False
def update(self):
"""限制移动范围"""
if self.moving_r and self.rect.right < self.screen_rect.right: self.center += self.settings.speed self.rect.centerx = self.center if self.moving_l and self.rect.left > 0:
self.center -= self.settings.speed
self.rect.centerx = self.center
"""更新飞船的具体位置"""
self.rect.centerx = self.center
 
def blitme(self):
self.screen.blit(self.image, self.rect) # 重绘飞船

游戏主程序:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import sys
import pygame
from pygame.sprite import Group
from settings import Setting
import key
from blood import Blood
from ball import  Ball
def run():
ai_settings=Setting()
pygame.init()
screen=pygame.display.set_mode((ai_settings.weight,ai_settings.height))
pygame.display.set_caption("alien")
blood=Blood(screen,ai_settings)
balls = Group()
bloods=Group()
key.creat_ball(balls,screen)
while True:
key.check_event(blood)
key.creat_balls(balls , screen, bloods)
key.screen(balls,blood,screen,ai_settings)
pygame.display.flip()
 
run()

设置代码:

1
2
3
4
5
6
7
8
9
class Setting():
def __init__(self):
self.weight=800
self.height = 400
self.color=250,0,255.
self.speed = 10
self.ball_num=10</code> <code>from pygame.sprite import Sprite
from random import randint
import pygame</code> 球代码: class Ball(Sprite): """docstring for Ball""" def __init__(self, screen): super(Ball, self).__init__() self.screen = screen self.screen_rect = self.screen.get_rect() self.image = pygame.image.load('./2.png') self.rect = self.image.get_rect() # 设置位置 self.rect.x = randint(0, self.screen_rect.right - self.rect.width) self.rect.y = 0 self.x = float(self.rect.x) self.y = float(self.rect.y) self.speed = 1 def blitme(self): self.screen.blit(self.image, self.rect)
北关漫步

北关漫步

咸鱼....

留下你的评论

*评论支持代码高亮<pre class="prettyprint linenums">代码</pre>