# 20221406 李昊蔚《Python程序设计》实验四报告
课程:《Python程序设计》
班级: 2214
姓名: 李昊蔚
学号:20221406
实验教师:王志强
实验日期:2022年5月20日
必修/选修: 公选课
## 1.实验内容
编写小游戏:坦克大战、贪吃蛇、扫雷等等
## 2. 实验过程及结果
调用库:
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("assets/1.png"), pygame.image.load("assets/2.png"), pygame.image.load("assets/dead.png")] self.status = 0 self.birdX = 120 self.birdY = 350 self.jump = False self.jumpSpeed = 10 self.gravity = 5 self.dead = 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 = 400; self.pineUp = pygame.image.load("assets/top.png") self.pineDown = pygame.image.load("assets/bottom.png") def updatePipeline(self): self.wallx -= 5 if self.wallx < -80: global score score += 1 self.wallx = 400
定义地图:
def createMap(): screen.fill((255, 255, 255)) screen.blit(background, (0, 0)) screen.blit(Pipeline.pineUp,(Pipeline.wallx,-300)); screen.blit(Pipeline.pineDown,(Pipeline.wallx,500)); Pipeline.updatePipeline() 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,(255, 255, 255)),(100, 50)) pygame.display.update()
检测小鸟是否撞管道:
def checkDead() upRect = pygame.Rect(Pipeline.wallx,-300, Pipeline.pineUp.get_width() - 10, Pipeline.pineUp.get_height()) downRect = pygame.Rect(Pipeline.wallx,500, Pipeline.pineDown.get_width() - 10, Pipeline.pineDown.get_height()) if upRect.colliderect(Bird.birdRect) or downRect.colliderect(Bird.birdRect): Bird.dead = True if not 0 < Bird.birdRect[1] < height: Bird.dead = True return True else : return False
文字:
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("Arial", 50) size = width, height = 400, 650 screen = pygame.display.set_mode(size) clock = pygame.time.Clock() Pipeline = Pipeline() Bird = Bird() score = 0 while True: clock.tick(60) for event in pygame.event.get(): if event.type == 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("assets/background.png") if checkDead() : getResutl() else : createMap() pygame.quit()
程序运行视频:正在审核,发给课代表了
码云仓库图片:
实现过程:
1.在pycharm上下载库:
2.跟《0基础学python》敲代码
3.从网上搜索图片:
4.将图片地址代入代码中:
5.完成!
## 3. 实验过程中遇到的问题和解决过程
1.一开始调用pygame函数时就报错
解决1:在pycharm中,file-settings-python interpreter再点加号就可以了
2.代码的理解问题
解决2:搜索书上的教程
3.敲完书上的代码程序无法运行
解决3:上csdn搜了,问题在于没有引用的图片,需要下载
##4.其他
感想与体会:flappy bird玩的很开心!这个实验很考验自学能力,有很多次我都破防了,做完实验采访其他同学才发现其他方向更难,实验也让我感受到了python功能的强大,调用库很快,代码也很简洁。
对全课进行总结:经过一学期的学习,我体验到了一种与c语言风格完全不同的计算机语言--python,它简洁而高效,对我这样的初学者也很友好,而志强老师的讲课风格幽默风趣,进一步激发了我对python的学习欲。经过学习,我学习到了Python中的循环语句、条件语句,还学习到Python中元组、字典、正则表达式等语法知识,socket,爬虫等技术。最后一节课,志强老师邀请七号创新社的学长们来给我们分享python的实际应用--爬虫,让我再一次感受到python功能的强大。感谢志强老师让我走进了python的大门,在今后的学习生活中,我也会继续钻研python,利用好这一强大的编程语言。
对课程的一些意见与建议:
1.讲课的时候能不能多一点给同学们敲一遍并运行的时间,这样同学们记得可能会更牢。
2.老师能不能多来几次像最后一次课那样请学长展示的课,这样同学们学习的积极性可能会更高
人生苦短,我用python