自学一周python做的一个小游戏《大球吃小球》
需求
1,顯示一個(gè)窗口。
2,我們要做到的功能有鼠標(biāo)點(diǎn)擊屏幕生成小球。
3,生成的小球大小隨機(jī),顏色隨機(jī),向隨機(jī)方向移動(dòng),速度也隨機(jī)。
4,大的球碰到小球時(shí)可以吃掉小球,吃掉后會(huì)變大。
5,球碰到邊界會(huì)彈回去。
思路
思路很簡(jiǎn)單
1,這個(gè)游戲我們使用python的pygame,先生成一個(gè)帶有背景顏色固定大小的窗口
2,建一個(gè)顏色類,用來生成隨機(jī)顏色
3,建一個(gè)球類用于生成隨機(jī)的各樣小球
4,建主方法,調(diào)用顏色和球生成小游戲
5,打包
第一步 生成窗口
我們需要導(dǎo)入pygame模塊,如果你用的是PyCharm的話點(diǎn)擊下面這個(gè)代碼,PyCharm會(huì)自動(dòng)下載pygame模塊
import pygame
如果你沒用PyCharm的話就直接使用命令導(dǎo)入
pip install pygame
導(dǎo)入成功后我們建一個(gè)窗口對(duì)象
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption('大球吃小球')
screen.fill((224, 224, 224))
pygame.display.flip()
running = True
# 開啟一個(gè)事件循環(huán)處理發(fā)生的事件
while running:
# 從消息隊(duì)列中獲取事件并對(duì)事件進(jìn)行處理
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
800和600是窗口的長(zhǎng)寬,大球吃小球是窗口的標(biāo)題,(224,224,224)代表窗口顏色是RGB格式的顏色表達(dá)式下面是用來監(jiān)聽事件的后面會(huì)用到,現(xiàn)在開始運(yùn)行
if __name__=="__main__":
main()

很好,現(xiàn)在第一步完成了
第二步 建一個(gè)顏色類
@unique
class Color(Enum):
red = (255, 0, 0)
@staticmethod
def random_color():
"""獲得隨機(jī)顏色"""
r = randint(0, 255)
g = randint(0, 255)
b = randint(0, 255)
return r, g, b
顏色類,比較簡(jiǎn)單,生成三個(gè)隨機(jī)值就是一個(gè)隨機(jī)的顏色了,這也就是剛才上面說的RGB值
第三步 球類
球類復(fù)雜一些,需要傳入位置坐標(biāo),半徑,移動(dòng)距離和顏色
還要有吃,移動(dòng),和生成三個(gè)方法。代碼如下
class Ball():
def __init__(self, x, y, reduis, sx, sy, color=Color.red):
self._sy = sy
self._x = x
self._y = y
self._reduis = reduis
self._sx = sx
self._color = color
self._alive = True
def move(self, screen):
self._x += self._sx
self._y += self._sy
if self._x - self._reduis <= 0 or self._x + self._reduis >= screen.get_width():
self._sx = -self._sx
if self._y - self._reduis <= 0 or self._y + self._reduis >= screen.get_height():
self._sy = -self._sy
def eat(self, other):
if self._alive and other._alive and other != self:
dx = self._x - other._x
dy = self._y - other._y
distance = sqrt(dx ** 2 + dy ** 2)
print(distance)
if distance < int(self._reduis) + int(other._reduis) and int(self._reduis) > int(other._reduis):
other._alive = False
self._reduis = self._reduis + int(other._reduis * 0.146)
def draw(self, screen):
pygame.draw.circle(screen, self._color, (self._x, self._y), self._reduis, 0)
移動(dòng)和吃這兩個(gè)方法的邏輯不難,我就不在這說了,不懂的可以在面評(píng)論或私信。關(guān)于屬性在init里有個(gè)alive存活代表這小球是否存活的屬性,需要大家注意
我就說下生成draw這個(gè)方法吧
circle這個(gè)方法的參數(shù)分別是screen窗口對(duì)象,顏色,球的坐標(biāo),球的半徑,和是否填充
第四步 在主方法里調(diào)用并編寫點(diǎn)擊生成和反彈方法
代碼如下:
balls = [] #
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption('大球吃小球')
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:#
x, y = event.pos#
radius = randint(10, 100)#
if x - radius < 0:#
x = radius#
if y - radius < 0:#
y = radius#
sx, sy = randint(-10, 10), randint(-10, 10)#
color = Color.random_color()#
ball = Ball(x, y, radius, sx, sy, color)#
# 將球添加到列表容器中
balls.append(ball)#
screen.fill((224, 224, 224))#
for ball in balls:#
if ball._alive:#
ball.draw(screen)#
else:#
balls.remove(ball)#
pygame.display.flip()
pygame.time.delay(50)#
for ball in balls:#
ball.move(screen)#
# 檢查球有沒有吃到其他的球
for other in balls:#
ball.eat(other)#
我在更改代碼的部分后面加了#,表示區(qū)分
首先我們聲明一個(gè)balls用來做裝球的容器,然后在事件監(jiān)聽部分加上對(duì)鼠標(biāo)點(diǎn)擊事件的監(jiān)聽ifx-radius是為了防止在界面邊緣點(diǎn)擊時(shí)生成的球超出邊界,然后將生成的球的對(duì)象放入容器balls里遍歷容器,判斷是否存活,若存活則生成,若已死則移除容器。
將窗口設(shè)置為50毫秒刷新一次,最后再次遍歷判斷球有沒有吃其他球,現(xiàn)在運(yùn)行。
大家可以看到效果已經(jīng)出來了,現(xiàn)在還差最后一步。打包
打包
打包工具我用的是Pyinstaller需要先安裝一下
pip install Pyinstaller
然后打開pycharm底部的terminal面板輸入
pyinstaller -F xyx.py
回車就行了
如果沒有pycharm的話就在命令窗口進(jìn)入到項(xiàng)目目錄下,再輸入這個(gè)命令回車就行了,找到dist的exe雙擊就運(yùn)行了
總結(jié)
以上是生活随笔為你收集整理的自学一周python做的一个小游戏《大球吃小球》的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: git报错:error: Your lo
- 下一篇: Python 异常处理:try、exc