区块链技术博客
www.b2bchain.cn

【Python】从0到1:从头开始写打砖块小游戏~(四)的讲解

这篇文章主要介绍了【Python】从0到1:从头开始写打砖块小游戏~(四)的讲解,通过具体代码讲解7683并且分析了【Python】从0到1:从头开始写打砖块小游戏~(四)的讲解的详细步骤与相关技巧,需要的朋友可以参考下

本文实例讲述了【Python】从0到1:从头开始写打砖块小游戏~(四)的讲解。分享给大家供大家参考文章查询地址https://www.b2bchain.cn/7683.html。具体如下:

世界上最遥远的距离,不是生与死,而是……

你亲手创造出来的bug就在眼前,可你怎么也找不到!

Hi~ o( ̄▽ ̄)ブ 我又双叒叕来啦~ 等你很久啦~
NOTE:楼主11岁女学生,文笔(排版风格)不好别介意啦~
NOTE2:这个游戏是我从头开始写的,所以会比较慢,就相当于检测一下这个疫情期间的学习成果吧~

关于这个游戏:

  • 它是我从头开始写的,每天写多少发多少,所以可能会比较慢,每天写4,50行的话大概五天左右~
  • 能力原因,每天在电脑前呆4,5个小时也就60行左右,再加上测试和改bug……
  • 没有抄袭!没有抄袭!没有抄袭!
程序语言 Python
工具/原料 电脑,Pygame
其它

这个程序写完可能会比较久~

因为8月要参加奥数比赛,所以最近每天要花3,4个小时刷题,写Python的时间被严重压缩……呜~

昨天没发,今天把两天的代码都发了!(拖延症患者在此……各位大佬随便催更~

上篇链接~:

                                   【Python】从0到1:从头开始写打砖块小游戏~(三)

截止到现在的所有代码~:(250行左右~)

哦吼!终于把碰撞检测写完了!我太难了……

 预计明天完结~ 

 #导入模块 import pygame from pygame.locals import * import sys,random,time,math  class GameWindow(object): 	'''游戏窗口类''' 	def __init__(self,*args,**kw):		 		self.window_length = 600 		self.window_wide = 500 		#绘制游戏窗口,设置窗口大小 		self.game_window = pygame.display.set_mode((self.window_length,self.window_wide)) 		#游戏窗口标题 		pygame.display.set_caption("CatchBallGame") 		#游戏窗口背景颜色 		self.window_color = (135,206,250)  	def backgroud(self): 		#绘制游戏窗口背景颜色 		self.game_window.fill(self.window_color)  class Ball(object): 	'''Ball类''' 	def __init__(self,*args,**kw): 		#设置球的半径、颜色、移动速度参数 		self.ball_color = (255,215,0)		 		self.move_x = 1 		self.move_y = 1 		self.radius = 10  	def ballready(self): 		#设置球的初始位置 		self.ball_x = self.mouse_x 		self.ball_y = self.window_wide-self.rect_wide-self.radius 		#绘制球,设置反弹触发の条件			 		pygame.draw.circle(self.game_window,self.ball_color,(self.ball_x,self.ball_y),self.radius)  	def ballmove(self): 		#绘制球,设置反弹触发の条件			 		pygame.draw.circle(self.game_window,self.ball_color,(self.ball_x,self.ball_y),self.radius)		 		self.ball_x += self.move_x 		self.ball_y -= self.move_y 		#调用碰撞检测函数(终于写完了!) 		self.ball_window() 		self.ball_rect() 		#每接5次球球速增加一倍 		if self.distance < self.radius: 			self.frequency += 1 			if self.frequency == 5: 				self.frequency = 0 				self.move_x += self.move_x 				self.move_y += self.move_y 				self.point += self.point 		#设置游戏失败的条件 		if self.ball_y > 520: 			self.gameover = self.over_font.render("Game Over",False,(0,0,0)) 			self.game_window.blit(self.gameover,(100,130)) 			self.over_sign = 1  class Rect(object): 	'''球拍类''' 	def __init__(self,*args,**kw): 		#设置球拍颜色 		self.rect_color = (255,0,0) 		self.rect_length = 100 		self.rect_wide = 10  	def rectmove(self): 		#鼠标位置 		self.mouse_x,self.mouse_y = pygame.mouse.get_pos() 		#绘制球拍,限定横向边界					 		if self.mouse_x >= self.window_length-self.rect_length//2: 			self.mouse_x = self.window_length-self.rect_length//2 		if self.mouse_x <= self.rect_length//2: 			self.mouse_x = self.rect_length//2 		pygame.draw.rect(self.game_window,self.rect_color,((self.mouse_x-self.rect_length//2),(self.window_wide-self.rect_wide),self.rect_length,self.rect_wide))  class Brick(object): 	def __init__(self,*args,**kw): 		#设置砖块颜色 		self.brick_color = (139,126,102) 		self.brick_list = [[1,1,1,1,1,1],[1,1,1,1,1,1],[1,1,1,1,1,1],[1,1,1,1,1,1],[1,1,1,1,1,1]] 		self.brick_length = 80 		self.brick_wide = 20  	def brickarrange(self):		 		for i in range(5): 			for j in range(6): 				self.brick_x = j*(self.brick_length+24) 				self.brick_y = i*(self.brick_wide+20)+40 				if self.brick_list[i][j] == 1: 					#绘制砖块 					pygame.draw.rect(self.game_window,self.brick_color,(self.brick_x,self.brick_y,self.brick_length,self.brick_wide))					 					#调用碰撞检测函数 					self.ball_brick()										 					if self.distanceb < self.radius: 						self.brick_list[i][j] = 0 						self.score += self.point 		#设置游戏胜利条件 		if self.brick_list == [[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0]]: 			self.win = self.win_font.render("You Win",False,(0,0,0)) 			self.game_window.blit(self.win,(100,130)) 			self.win_sign = 1  class Score(object): 	'''分数类''' 	def __init__(self,*args,**kw):		 		#设置初始分数 		self.score = 0 		#设置分数的字体 		self.score_font = pygame.font.SysFont('arial',20) 		#设置初始加分 		self.point = 1 		#设置初始接球次数 		self.frequency = 0  	def countscore(self): 		#绘制玩家分数			 		my_score = self.score_font.render(str(self.score),False,(255,255,255)) 		self.game_window.blit(my_score,(555,15))  class GameOver(object): 	'''游戏结束类''' 	def __init__(self,*args,**kw): 		#设置Game Over字体 		self.over_font = pygame.font.SysFont('arial',80) 		#Game Over标识 		self.over_sign = 0  class Win(object): 	'''游戏胜利类''' 	def __init__(self,*args,**kw): 		#设置You Win字体 		self.win_font = pygame.font.SysFont('arial',80) 		#定义Win标识 		self.win_sign = 0  class Collision(object): 	'''碰撞检测类''' 	#球与窗口边框的碰撞检测 	def ball_window(self): 		if self.ball_x <= self.radius or self.ball_x >= (self.window_length-self.radius): 			self.move_x = -self.move_x 		if self.ball_y <= self.radius: 			self.move_y = -self.move_y  	#球与球拍的碰撞检测 	def ball_rect(self): 		#碰撞标识 		self.collision_sign_x = 0 		self.collision_sign_y = 0  		if self.ball_x < (self.mouse_x-self.rect_length//2): 			self.closestpoint_x = self.mouse_x-self.rect_length//2 			self.collision_sign_x = 1 		elif self.ball_x > (self.mouse_x+self.rect_length//2): 			self.closestpoint_x = self.mouse_x+self.rect_length//2 			self.collision_sign_x = 2 		else: 			self.closestpoint_x = self.ball_x 			self.collision_sign_x = 3  		if self.ball_y < (self.window_wide-self.rect_wide): 			self.closestpoint_y = (self.window_wide-self.rect_wide) 			self.collision_sign_y = 1 		elif self.ball_y > self.window_wide: 			self.closestpoint_y = self.window_wide 			self.collision_sign_y = 2 		else: 			self.closestpoint_y = self.ball_y 			self.collision_sign_y = 3 	 		self.distance = math.sqrt(math.pow(self.closestpoint_x-self.ball_x,2)+math.pow(self.closestpoint_y-self.ball_y,2)) 		#球在球拍上左、上中、上右3种情况的碰撞检测 		if self.distance < self.radius and self.collision_sign_y == 1 and (self.collision_sign_x == 1 or self.collision_sign_x == 2): 			if self.collision_sign_x == 1 and self.move_x > 0: 				self.move_x = - self.move_x 				self.move_y = - self.move_y 			if self.collision_sign_x == 1 and self.move_x < 0: 				self.move_y = - self.move_y 			if self.collision_sign_x == 2 and self.move_x < 0: 				self.move_x = - self.move_x 				self.move_y = - self.move_y 			if self.collision_sign_x == 2 and self.move_x > 0: 				self.move_y = - self.move_y 		if self.distance < self.radius and self.collision_sign_y == 1 and self.collision_sign_x == 3: 			self.move_y = - self.move_y 		#球在球拍左、右两侧中间的碰撞检测 		if self.distance < self.radius and self.collision_sign_y == 3: 			self.move_x = - self.move_x  	#球与砖块的碰撞检测 	def ball_brick(self): 		#定义碰撞标识 		self.collision_sign_bx = 0 		self.collision_sign_by = 0  		if self.ball_x < self.brick_x: 			self.closestpoint_bx = self.brick_x 			self.collision_sign_bx = 1 		elif self.ball_x > self.brick_x+self.brick_length: 			self.closestpoint_bx = self.brick_x+self.brick_length 			self.collision_sign_bx = 2 		else: 			self.closestpoint_bx = self.ball_x 			self.collision_sign_bx = 3  		if self.ball_y < self.brick_y: 			self.closestpoint_by = self.brick_y 			self.collision_sign_by = 1 		elif self.ball_y > self.brick_y+self.brick_wide: 			self.closestpoint_by = self.brick_y+self.brick_wide 			self.collision_sign_by = 2 		else: 			self.closestpoint_by = self.ball_y 			self.collision_sign_by = 3 		 		self.distanceb = math.sqrt(math.pow(self.closestpoint_bx-self.ball_x,2)+math.pow(self.closestpoint_by-self.ball_y,2)) 		#球在砖块上左、上中、上右3种情况的碰撞检测 		if self.distanceb < self.radius and self.collision_sign_by == 1 and (self.collision_sign_bx == 1 or self.collision_sign_bx == 2): 			if self.collision_sign_bx == 1 and self.move_x > 0: 				self.move_x = - self.move_x 				self.move_y = - self.move_y 			if self.collision_sign_bx == 1 and self.move_x < 0: 				self.move_y = - self.move_y 			if self.collision_sign_bx == 2 and self.move_x < 0: 				self.move_x = - self.move_x 				self.move_y = - self.move_y 			if self.collision_sign_bx == 2 and self.move_x > 0: 				self.move_y = - self.move_y 		if self.distanceb < self.radius and self.collision_sign_by == 1 and self.collision_sign_bx == 3: 			self.move_y = - self.move_y 		#球在砖块下左、下中、下右3种情况的碰撞检测 		if self.distanceb < self.radius and self.collision_sign_by == 2 and (self.collision_sign_bx == 1 or self.collision_sign_bx == 2): 			if self.collision_sign_bx == 1 and self.move_x > 0: 				self.move_x = - self.move_x 				self.move_y = - self.move_y 			if self.collision_sign_bx == 1 and self.move_x < 0: 				self.move_y = - self.move_y 			if self.collision_sign_bx == 2 and self.move_x < 0: 				self.move_x = - self.move_x 				self.move_y = - self.move_y 			if self.collision_sign_bx == 2 and self.move_x > 0: 				self.move_y = - self.move_y 		if self.distanceb < self.radius and self.collision_sign_by == 2 and self.collision_sign_bx == 3: 			self.move_y = - self.move_y 		#球在砖块左、右两侧中间的碰撞检测 		if self.distanceb < self.radius and self.collision_sign_by == 3: 			self.move_x = - self.move_x

还剩主程序了!明天一定加油写完!我爱Python~

您的催更是我的动力~(某位不知名Python小白~)

尽情催更吧!

溜了溜了~

本文地址https://www.b2bchain.cn/7683.html

赞(0) 打赏
部分文章转自网络,侵权联系删除b2bchain区块链学习技术社区 » 【Python】从0到1:从头开始写打砖块小游戏~(四)的讲解
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

b2b链

联系我们联系我们