20232402 2023-2024-2 《Python程序设计》实验一报告
课程:《Python程序设计》
班级: 2324
姓名: 王艺瑶
学号:2032402
实验教师:王志强
实验日期:2024年3月16日
必修/选修: 公选课
1.实验内容
1.熟悉Python开发环境;
2.练习Python运行、调试技能;
3.编写程序,练习变量和类型、字符串、对象、缩进和注释等;
4.掌握git技能(可把猜数字游戏上传到gitee)
2. 实验过程及结果
2.1 熟悉Python开发环境
2.1.1 环境配置
方法一:左下角Python编辑器
方法二:文件——设置——项目——Python解释器
2.1.2 软件包
方法一:左下角
方法二:文件——设置——项目——Python解释
2.1.3 常用快捷键:
注释:Ctrl+/
运行:Ctrl+Shift+F10
步入/步出:F7/F8
智能步入:Shift+F7
退出:Shift+F8
设置:Ctrl+Alt+S
2.1.4 申请学生版
2.2 练习Python运行、调试技能
2.2.1 运行
2.2.2 调试
Step1:设置断点
Step2:点调试
Step3:监视type(a):
Step4:输入a
Step5:步过
2.3 编写程序,练习变量和类型、字符串、对象、缩进和注释
2.3.1 game1:Flappy Bird
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("bird.png"),
pygame.image.load("bird2.png"),
pygame.image.load("bird3.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
def updatePipeline(self):
self.wallx -= 5
if self.wallx < -80:
global score
score+=1
self.wallx = 400
class Pipeline(object):
def __init__(self):
self.wallx = 400;
self.pipeUp = pygame.image.load("pipe1.png")
self.pipeDown = pygame.image.load("pipe2.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.pipeUp,(Pipeline.wallx,-300))
screen.blit(Pipeline.pipeDown,(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.pipeUp.get_width() - 10,
Pipeline.pipeUp.get_height())
downRect = pygame.Rect(Pipeline.wallx, 500,
Pipeline.pipeDown.get_width() - 10,
Pipeline.pipeDown.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 getResult1():
final_text1 = "Game Over"
final_text2 = "Your final score: " + str(score)
ft1_font = pygame.font.SysFont('Arial', 70)
ft1_surf = ft1_font.render(final_text1, 1, (242, 3, 36))
ft2_font = pygame.font.SysFont('Arial', 70)
ft2_surf = ft2_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 = 600, 680
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:
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("map.png")
if checkDead():
getResult1()
else:
createMap()
效果 be like:
2.3.2 game2:Ball game
import sys
import pygame
pygame.init()
size = width, height = 1040, 840
screen = pygame.display.set_mode(size)
color = (0, 0, 0)
ball = pygame.image.load("ball1.png")
ballrect = ball.get_rect()
speed = [5,5]
clock = pygame.time.Clock()
while True:
clock.tick(60)
for event in pygame.event.get():
if event in pygame.event.get():
pygame.quit()
sys.exit()
ballrect = ballrect.move(speed)
if ballrect.left < 0 or ballrect.right > width:
speed[0] = -speed[0]
if ballrect.top < 0 or ballrect.bottom > height:
speed[1] = -speed[1]
screen.fill(color)
screen.blit(ball, ballrect)
pygame.display.flip()
2.4 掌握git技能
2.4.1 下载git
2.4.2 pycharm配置
2.4.3 关闭项目后从版本控制中获取,粘贴复制建好的git库的URL
2.4.4 把代码上传到库中,三板斧:git添加—提交—推送
写注释并点击提交并推送
2.4.5 在gitee中能看到托管的代码(作业再也不会被狗吃啦)
3. 实验过程中遇到的问题和解决过程
- 问题1:运行快捷键Ctn+Shift+F10用不了
- 问题1解决方案:fn+esc解除锁定即可使用FX
- 问题2:调试总进入内置库
- 问题2解决方案:不要使用单步调试,点步过即可