144
社区成员




课程:《Python程序设计》
班级: 2213
姓名: 马申明
学号:1328
实验教师:王志强
实验日期:2023年5月4日
必修/选修: 公选课
Python综合应用:爬虫、数据处理、可视化、机器学习、神经网络、游戏、网络安全等。
课代表和各小组负责人收集作业(源代码、视频、综合实践报告)
例如:编写从社交网络爬取数据,实现可视化舆情监控或者情感分析。
例如:利用公开数据集,开展图像分类、恶意软件检测等
例如:利用Python库,基于OCR技术实现自动化提取图片中数据,并填入excel中。
例如:爬取天气数据,实现自动化微信提醒
例如:利用爬虫,实现自动化下载网站视频、文件等。
例如:编写小游戏:坦克大战、贪吃蛇、扫雷等等
注:在Windows/Linux系统上使用VIM、PDB、IDLE、Pycharm等工具编程实现。
import pygame
import sys
import random
class Bird(object):
def __init__(self):
self.birdRect = pygame.Rect(65,50,50,50)
self.birdStatus = [pygame.image.load("bird2.png"),
pygame.image.load("bird0.png"),
pygame.image.load("birddead1.png")]
self.status = 0
self.birdX = 120
self.birdY = 300
self.jump = False
self.jumpSpeed = 10
self.gravity = 5
self.dead = False
self.strong = False
def birdUpdate(self):
if self.jump:
self.jumpSpeed -= 1
self.birdY -= self.jumpSpeed
else:
self.gravity += 0.2
self.birdY += self.gravity
self.birdRect[1] = self.birdY
class Pipeline(object):
def __init__(self):
self.wallx = 1024
self.wallx2 = 1424
self.wallx3 = 1824
self.wally = 0
self.wally2 = -80
self.wally3 = -50
self.pineUp = pygame.image.load("管道上.jpg")
self.pineDown = pygame.image.load("管道下.jpg")
def updatePipeline(self):
global score
if score <= 30:
self.wallx -= 5
self.wallx2 -= 5
self.wallx3 -= 5
elif score <= 60:
self.wallx -= 7
self.wallx2 -= 7
self.wallx3 -= 7
else:
self.wallx -= 9
self.wallx2 -= 9
self.wallx3 -= 9
if self.wallx < -133:
score += 1
self.wallx = 1024
self.wally = random.randrange(-100,0)
elif self.wallx2 < -133:
score += 1
self.wallx2 = 1024
self.wally2 = random.randrange(-100,0)
elif self.wallx3 < -133:
score += 1
self.wallx3 = 1024
self.wally3 = random.randrange(-100,0)
#class shield(object):
# def __init__(self):
# self.Sx = random.randrange(1168, 1381)
# self.Sy = random.randrange(0, 1000)
# self.img = pygame.image.load("shield_副本.png")
#def Supdate (self):
# if score <= 30:
# self.Sx -= 5
# elif score <= 60:
# self.Sx -= 7
#else:
# self.Sx -= 9
#if self.Sx < -30:
# self.Sx = random.randrange(1168, 1381)
# self.Sy = random.randrange(0, 599)
def createMap():
screen.fill((255,255,255))
screen.blit(background,(0,0))
screen.blit(Pipeline.pineUp,(Pipeline.wallx,Pipeline.wally))
screen.blit(Pipeline.pineUp,(Pipeline.wallx2,Pipeline.wally2))
screen.blit(Pipeline.pineUp, (Pipeline.wallx3, Pipeline.wally3))
if score < 90:
d = 550
else:
d = 500
screen.blit(Pipeline.pineDown,(Pipeline.wallx,Pipeline.wally + d))
screen.blit(Pipeline.pineDown, (Pipeline.wallx2, Pipeline.wally2 + d))
screen.blit(Pipeline.pineDown, (Pipeline.wallx3, Pipeline.wally3 + d))
Pipeline.updatePipeline()
#screen.blit(shield.img,(shield.Sx,shield.Sy))
#shield.Supdate()
if Bird.dead:
Bird.status = 2
elif Bird.jump:
Bird.status = 1
screen.blit(Bird.birdStatus[Bird.status],(Bird.birdX,Bird.birdY))
Bird.birdUpdate()
screen.blit(font.render('Score:' + str(score), -1, (253, 177, 6)), (100, 50))
pygame.display.update()
def checkDead(score):
upRect = pygame.Rect(Pipeline.wallx,Pipeline.wally,
Pipeline.pineUp.get_width() - 10,
Pipeline.pineUp.get_height())
downRect = pygame.Rect(Pipeline.wallx,Pipeline.wally + 550,
Pipeline.pineDown.get_width() - 10,
Pipeline.pineDown.get_height())
if upRect.colliderect(Bird.birdRect) or downRect.colliderect(Bird.birdRect):
Bird.dead = True
if Bird.strong == True:
score = score + 1
Bird.dead = False
Bird.strong = False
return False
upRect = pygame.Rect(Pipeline.wallx2, Pipeline.wally2,
Pipeline.pineUp.get_width() - 10,
Pipeline.pineUp.get_height())
downRect = pygame.Rect(Pipeline.wallx2, Pipeline.wally2 + 550,
Pipeline.pineDown.get_width() - 10,
Pipeline.pineDown.get_height())
if upRect.colliderect(Bird.birdRect) or downRect.colliderect(Bird.birdRect):
Bird.dead = True
if Bird.strong == True:
score = score + 1
Bird.dead = False
Bird.strong = False
return False
upRect = pygame.Rect(Pipeline.wallx3, Pipeline.wally3,
Pipeline.pineUp.get_width() - 10,
Pipeline.pineUp.get_height())
downRect = pygame.Rect(Pipeline.wallx3, Pipeline.wally3 + 550,
Pipeline.pineDown.get_width() - 10,
Pipeline.pineDown.get_height())
if upRect.colliderect(Bird.birdRect) or downRect.colliderect(Bird.birdRect):
Bird.dead = True
if Bird.strong == True:
score = score + 1
Bird.dead = False
Bird.strong = False
return False
if not 0 < Bird.birdRect[1] < height:
Bird.dead = True
if Bird.strong == True:
score = score + 1
Bird.dead = False
Bird.strong = False
return False
return True
else:
return False
def strong(score):
Rect = pygame.Rect(shield.Sx, shield.Sy,
shield.img.get_width(),
shield.img.get_height())
if Rect.colliderect(Bird.birdRect):
score = score + 1
Bird.strong = True
def getResutl():
final_text1 = "Game Over"
final_text2 = "Your final score is: " + str(score)
ft1_font = pygame.font.SysFont("Arial", 70) # 设置第一行文字字体
ft1_surf = font.render(final_text1, 1, (242, 3, 36)) # 设置第一行文字颜色
ft2_font = pygame.font.SysFont("Arial", 50) # 设置第二行文字字体
ft2_surf = font.render(final_text2, 1, (253, 177, 6)) # 设置第二行文字颜色
screen.blit(ft1_surf, [screen.get_width() / 2 - ft1_surf.get_width() / 2, 100]) # 设置第一行文字显示位置
screen.blit(ft2_surf, [screen.get_width() / 2 - ft2_surf.get_width() / 2, 200]) # 设置第二行文字显示位置
pygame.display.flip()
if __name__ == '__main__':
pygame.init()
pygame.font.init()
font = pygame.font.SysFont(None,50)
size = width , height = 1024,650
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()
Pipeline = Pipeline()
Bird = Bird()
#shield = shield()
score = 0
while True:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if (event.type == pygame.KEYDOWN or event.type == pygame.MOUSEBUTTONDOWN)and not Bird.dead:
Bird.jump = True
Bird.gravity = 5
Bird.jumpSpeed = 10
background = pygame.image.load("blue sky.jpg")
#pygame.mixer.music.load(r"Flappy Bird.MP3")
#pygame.mixer.music.play(loops=-1, start=0.0)
if checkDead(score):
pygame.mixer.music.fadeout(3000)
getResutl()
else:
createMap()
pygame.quit()
sys.exit()
想法未能成功转变成实际,难受;