【Python】从0到1:从头开始写打砖块小游戏
程序员文章站
2022-05-18 19:15:27
Hi~ o( ̄▽ ̄)ブ 我又双叒叕来啦~ 等你很久啦~NOTE:楼主11岁女学生,文笔(排版风格)不好别介意啦~NOTE2:这个游戏是我从头开始写的,所以会比较慢,就相当于检测一下这个疫情期间的学习成果吧~关于这个游戏:它是我从头开始写的,每天写多少发多少,所以可能会比较慢,每天写4,50行的话大概五天左右~能力原因,每天在电脑前呆4,5个小时也就60行左右,再加上测试和改bug......没有抄袭!没有抄袭!没有抄袭!程序语言Python工具/原料电脑,Pyg...
Hi~ o( ̄▽ ̄)ブ 我又双叒叕来啦~ 等你很久啦~
NOTE:楼主11岁女学生,文笔(排版风格)不好别介意啦~
NOTE2:这个游戏是我从头开始写的,所以会比较慢,就相当于检测一下这个疫情期间的学习成果吧~
关于这个游戏:
- 它是我从头开始写的,每天写多少发多少,所以可能会比较慢,每天写4,50行的话大概五天左右~
- 能力原因,每天在电脑前呆4,5个小时也就60行左右,再加上测试和改bug......
- 没有抄袭!没有抄袭!没有抄袭!
程序语言 | Python |
工具/原料 | 电脑,Pygame |
其它 | 这个程序写完可能会比较久~ |
上篇说好了要写就要写,哪怕没人看我也要写,哈哈哈~【谁让写blog使我快乐~】
好啦好啦我不废话了,这是上一篇的链接~:
这是上一篇的代码~:(60行左右)
#导入模块
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
这是这一篇的代码~:(40行左右)
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
这是截止到现在的所有代码~:(100行左右)
#导入模块
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
哎呀~这成就感杠杠滴~
谢谢你看到这里~
还是那句话呀,代码如有bug或不足之处,评论区见~
本文地址:https://blog.csdn.net/weixin_46836893/article/details/107450387