前言
本文的文字及圖片來源于網路,僅供學習、交流使用,不具有任何商業用途,著作權歸原作者所有,如有問題請及時聯系我們以作處理
以下文章來源于 Python技術 ,作者:派森醬

Python GUI編程:高清電影在線觀看平臺制作,全網電影免費看
https://www.bilibili.com/video/BV1tz4y1o7Yc/
相信大家在初中電腦課上都偷偷玩過 Flash 游戲--是男人就堅持 100 秒,在游戲中無數的小球隨機運動,玩家用滑鼠控制大球,當大球碰撞到小球后,游戲結束,顯示堅持的時間,今天我們一起來開發這個小游戲吧,
步驟分布:
- 設定螢屏大小和標題
- 小球繪制、移動
- 大球繪制、滑鼠控制大球
- 大球碰撞到小球后游戲結束
設定螢屏大小和標題
import pygame W = 600 H = 500 # 初始化pygame模塊 pygame.init() # 設定視窗大小 screen = pygame.display.set_mode((W,H)) # 設定視窗標題 pygame.display.set_caption('是男人就堅持100秒')
繪制小球、移動
小球是圓形的,圓的半徑決定了小球的大小并且在小球移動的時它的 X 坐標和 Y 坐標一直是在變動的,所以設定 X 坐標、Y 坐標、X 方向移動速度、Y 方向移動速度變數,小球每次移動的坐標都是 X 坐標 + X 方向移動速度、Y 坐標 + Y 方向移動速度,time.sleep(0.001) 可以調整小球的移動的時間,時間越大移動越慢,當小球碰到左右邊界的時候需要調整 X 方向移動速度、Y 方向移動速度為負的
class Ball: x = None # x坐標 y = None # y坐標 speed_x = None # x方向移動的速度 speed_y = None # y方向移動的速度 radius = None # 小半徑 color = None # 顏色 def __init__(self, x, y, speed_x, speed_y, radius, color): """ 初始化 :param x: X坐標 :param y: Y坐標 :param speed_x: X軸方向速度 :param speed_y: Y軸方向速度 :param radius: 半徑 :param color: 顏色 """ self.x = x self.y = y self.speed_x = speed_x self.speed_y = speed_y self.radius = radius self.color = color def draw(self, screen): """ 繪制小球 :param screen: 視窗 :return: """ pygame.draw.circle(screen, self.color, [self.x, self.y], self.radius) def move(self, screen): """ 小球移動 :param screen: 視窗 :return: """ self.x += self.speed_x self.y += self.speed_y # 左右邊界 if self.x > W - self.radius or self.x < self.radius: self.speed_x = -self.speed_y # 上下邊界 if self.y > H - self.radius or self.y < self.radius: self.vy = -self.vy # 移動頻率 time.sleep(0.001) self.draw(screen) balls = [] def create_ball(screen): """ 創建小球 :param screen: :return: """ x = random.randint(0, W) y = random.randint(0, H) speed_x = random.randint(-5, 5) speed_y = random.randint(-5, 5) r = 3 color = 'white' b = Ball(x, y, speed_x, speed_y, r, color) balls.append(b) b.draw(screen)
大球的繪制和滑鼠控制大球
大球主要的屬性有半徑、顏色,移動的速度和方向都是跟隨滑鼠運動的,捕獲滑鼠的位置設定大球的 X、Y 坐標
class Player: radius = None color = None x = 1000 y = 1000 def __init__(self, radius, color): """ 初始化 :param radius: 半徑 :param color: 顏色 """ self.radius = radius self.color = color def move(self, screen): """ 大球移動 :return: """ # 滑鼠檢測 if pygame.mouse.get_focused(): # 獲取游標位置, x, y = pygame.mouse.get_pos() mouse = pygame.mouse.get_pressed() pygame.draw.circle(screen, self.color, [x, y], self.radius) self.x = x self.y = y
大球碰撞到小球后游戲結束
當大球碰撞到小球后游戲就結束了,計算大球的坐標減去小球的坐標小于兩球的半徑之和就表示它們碰撞了
# 小球每次移動后計算碰撞結果 for ball in balls: ball.move(screen) if abs(p.x - ball.x) < 13 and abs(p.y - ball.y) < 13: is_loop = False #結束程式回圈標志 break
顯示 GAME OVER 字樣和游戲的時間
def show_text(screen, text, pos, color, font_bold=False, font_size=18, font_italic=False): """ 顯示文字 :param screen: 視窗 :param text: 文字 :param pos: 坐標 :param color: 顏色 :param font_bold: 是否粗體 :param font_size: 大小 :param font_italic: 是否斜體 :return: """ cur_font = pygame.font.SysFont('Courier', font_size) cur_font.set_bold(font_bold) cur_font.set_italic(font_italic) text_fmt = cur_font.render(text, 1, color) screen.blit(text_fmt, pos) show_text(screen, "Game over!", (120, 180), "green", True, 60) show_text(screen, text_time, (220, 270), "green", True, 30)
游戲效果
總結
本文使用了 Python 是實作了一個簡單的是男人就堅持 100 秒的小游戲,有興趣的小伙伴可以對游戲進一步擴展,比如過幾秒鐘加幾個小球等等,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/240367.html
標籤:其他
