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

很好,現在第一步完成了
第二步 建一個顏色類
@unique
class Color(Enum):
red = (255, 0, 0)
@staticmethod
def random_color():
"""獲得隨機顏色"""
r = randint(0, 255)
g = randint(0, 255)
b = randint(0, 255)
return r, g, b
顏色類,比較簡單,生成三個隨機值就是一個隨機的顏色了,這也就是剛才上面說的RGB值
第三步 球類
球類復雜一些,需要傳入位置坐標,半徑,移動距離和顏色
還要有吃,移動,和生成三個方法。代碼如下
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)
移動和吃這兩個方法的邏輯不難,我就不在這說了,不懂的可以在面評論或私信。關于屬性在init里有個alive存活代表這小球是否存活的屬性,需要大家注意
我就說下生成draw這個方法吧
circle這個方法的參數分別是screen窗口對象,顏色,球的坐標,球的半徑,和是否填充
第四步 在主方法里調用并編寫點擊生成和反彈方法
代碼如下:
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)#
我在更改代碼的部分后面加了#,表示區分
首先我們聲明一個balls用來做裝球的容器,然后在事件監聽部分加上對鼠標點擊事件的監聽ifx-radius是為了防止在界面邊緣點擊時生成的球超出邊界,然后將生成的球的對象放入容器balls里遍歷容器,判斷是否存活,若存活則生成,若已死則移除容器。
將窗口設置為50毫秒刷新一次,最后再次遍歷判斷球有沒有吃其他球,現在運行。
大家可以看到效果已經出來了,現在還差最后一步。打包
打包
打包工具我用的是Pyinstaller需要先安裝一下
pip install Pyinstaller
然后打開pycharm底部的terminal面板輸入
pyinstaller -F xyx.py
回車就行了
如果沒有pycharm的話就在命令窗口進入到項目目錄下,再輸入這個命令回車就行了,找到dist的exe雙擊就運行了
總結
以上是生活随笔為你收集整理的自学一周python做的一个小游戏《大球吃小球》的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: git报错:error: Your lo
- 下一篇: 低代码平台探讨-MetaStore元数据