pygame開發小遊戲

河北大学-徐小波發表於2024-08-16

程式碼:

#coding=utf-8

import os,sys,re,time
import pygame
import random
from win32api import GetSystemMetrics
from tkinter import messagebox
from sqlparse.filters import right_margin

#pyinstaller -F -w daziyan.py

pygame.init()
pygame.display.set_caption("瘋狂打紫嫣-小遊戲")

percent = 0.6
right_percent = 0.2
screen_width = GetSystemMetrics(0)
screen_height = GetSystemMetrics(1)
window_width = int(screen_width*percent)
window_height = int(screen_height*percent)
right_width = int(window_width*right_percent)

maxLine = 14
maxField = 18
perWidth = (window_width-right_width) // maxField
perHeight = window_height // maxLine

#每重新整理50次出現一次方塊
perRectNum1 = 80
perRectNum2 = 70
perRectNum3 = 60

font_path = os.path.join(os.path.dirname(sys.argv[0]), 'simsun.ttc')
img_path1 = os.path.join(os.path.dirname(sys.argv[0]), '001.png')
img_path2 = os.path.join(os.path.dirname(sys.argv[0]), '002.png')
img_path3 = os.path.join(os.path.dirname(sys.argv[0]), '003.png')
cursor_img_path = os.path.join(os.path.dirname(sys.argv[0]), '004.png')

rect_x1 = random.randint(0, maxField-1) * perWidth
rect_y1 = random.randint(0, maxLine-1) * perHeight
position1_value = [rect_x1, rect_y1, perWidth, perHeight]
position1 = tuple(position1_value)

rect_x2 = random.randint(0, maxField-1) * perWidth
rect_y2 = random.randint(0, maxLine-1) * perHeight
position2_value = [rect_x2, rect_y2, perWidth, perHeight]
position2 = tuple(position2_value)

rect_x3 = random.randint(0, maxField-1) * perWidth
rect_y3 = random.randint(0, maxLine-1) * perHeight
position3_value = [rect_x3, rect_y3, perWidth, perHeight]
position3 = tuple(position3_value)

dt = 0
clock = pygame.time.Clock()

screen = pygame.display.set_mode((window_width, window_height))

pygame.mouse.set_visible(False)
mouse_img1 = pygame.image.load(cursor_img_path).convert_alpha()
mouse_img1 = pygame.transform.scale(mouse_img1, (perWidth, perHeight))
mouse_img1 = pygame.transform.rotate(mouse_img1, 45)
mouse_img2 = mouse_img1

#停止處理輸入法事件
pygame.key.stop_text_input()

game_msg = '徐紫嫣正在逃跑,快點抓住她。。。'
game_msg_run = ''

class Button:
    def __init__(self, x, y, width, height, color, text):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.color = color
        self.text = text
 
    def draw(self, win, outline=None, line_width = 1):
        pygame.draw.rect(win, self.color, (self.x, self.y, self.width, self.height))
        if outline:
            pygame.draw.rect(win, outline, (self.x, self.y, self.width, self.height), 3)
        font = pygame.font.Font(font_path, 20)
        text = font.render(self.text, 1, (0, 0, 0))
        win.blit(text, (self.x + (self.width / 2 - text.get_width() / 2), self.y + (self.height / 2 - text.get_height() / 2)))
        
    def changeText(self, text):
        self.text = text

class ButtonMsg:
    def __init__(self, x, y, width, height, color, text):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.color = color
        self.text = text
 
    def draw(self, win, outline=None, line_width = 1):
        pygame.draw.rect(win, self.color, (self.x, self.y, self.width, self.height))
        if outline:
            pygame.draw.rect(win, outline, (self.x, self.y, self.width, self.height), 3)
        font = pygame.font.Font(font_path, 12)
        text = font.render(self.text, 1, 'Chocolate4')
        win.blit(text, (self.x + (self.width / 2 - text.get_width() / 2), self.y + (self.height / 2 - text.get_height() / 2)))
        
    def changeText(self, text):
        self.text = text

score = 0
button = Button(window_width-right_width+50, 45, 100, 30, 'Gold3', '暫停')
mouse_pos = (-1, -1)
j = 0
running = True
zantingFlag = False
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            mouse_pos = pygame.mouse.get_pos()
            if button.x + button.width > event.pos[0] > button.x and button.y + button.height > event.pos[1] > button.y:
                if zantingFlag == True:
                    zantingFlag = False
                    game_msg = '徐紫嫣正在逃跑,快點抓住她。。。'
                    button.changeText('暫停')
                elif zantingFlag == False:
                    zantingFlag = True
                    game_msg = '休息一會,好累。。。'
                    button.changeText('開始')
            mouse_img2 = pygame.transform.rotate(mouse_img1, 45)
        if event.type == pygame.MOUSEBUTTONUP:
            mouse_img2 = pygame.transform.rotate(mouse_img1, -45)
    
    keys_pressed = pygame.key.get_pressed()
    
    #ESC鍵
    if keys_pressed[pygame.K_ESCAPE]:
        running = False
        
    screen.fill("black")
    
    #左側塊
    rect1 = pygame.Rect(0, 0, perWidth*maxField, perHeight*maxLine)
    pygame.draw.rect(screen, 'LightYellow1', rect1)
    
    #右側塊
    rect2 = pygame.Rect(perWidth*maxField, 0, window_width-perWidth*maxField, perHeight*maxLine)
    pygame.draw.rect(screen, 'Honeydew', rect2)
    
    #橫線
    for i in range(0, maxLine+1):
        line_width = window_width-right_width
        border_width = 1
        if i in [0, maxLine]:
            line_width = window_width
            border_width = 5
        pygame.draw.line(screen, 'black', (0, i*perHeight), (line_width, i*perHeight), border_width)
    
    #豎線
    for i in range(0, maxField+1):
        border_width = 1
        if i in [0, maxField]:
            border_width = 5
        pygame.draw.line(screen, 'black', (i*perWidth, 0), (i*perWidth, window_height), border_width)
        
    pygame.draw.line(screen, 'black', (window_width, 0), (window_width, window_height), 5)
    
    if j % perRectNum1 == 0 and zantingFlag == False:
        rect_x1 = random.randint(0, maxField-1) * perWidth
        rect_y1 = random.randint(0, maxLine-1) * perHeight
        position1_value = [rect_x1, rect_y1, perWidth, perHeight]
        position1 = tuple(position1_value)
        
    if j % perRectNum2 == 0 and zantingFlag == False:
        rect_x2 = random.randint(0, maxField-1) * perWidth
        rect_y2 = random.randint(0, maxLine-1) * perHeight
        position2_value = [rect_x2, rect_y2, perWidth, perHeight]
        position2 = tuple(position2_value)
        
    if j % perRectNum3 == 0 and zantingFlag == False:
        rect_x3 = random.randint(0, maxField-1) * perWidth
        rect_y3 = random.randint(0, maxLine-1) * perHeight
        position3_value = [rect_x3, rect_y3, perWidth, perHeight]
        position3 = tuple(position3_value)
    
    rectx1 = pygame.Rect(position1[0], position1[1], position1[2], position1[3])
    if rectx1.collidepoint(mouse_pos) and zantingFlag == False:
        game_msg_run = '臥槽,牛逼'
        score += 1
        mouse_pos = (-1, -1)
    
    rectx2 = pygame.Rect(position2[0], position2[1], position2[2], position2[3])
    if rectx2.collidepoint(mouse_pos) and zantingFlag == False:
        game_msg_run = '加油,小紫嫣'
        score -= 1
        mouse_pos = (-1, -1)
    
    rectx3 = pygame.Rect(position3[0], position3[1], position3[2], position3[3])
    if rectx3.collidepoint(mouse_pos) and zantingFlag == False:
        game_msg_run = '徐紫嫣,加油'
        score -= 1
        mouse_pos = (-1, -1)
    
    #pygame.draw.rect(screen, 'yellow', rectx1)
    
    img1 = pygame.image.load(img_path1).convert_alpha()
    timg1 = pygame.transform.scale(img1, (perWidth-4, perHeight-4))
    screen.blit(timg1, (position1[0]+2, position1[1]+2))
    
    #干擾圖1
    img2 = pygame.image.load(img_path2).convert_alpha()
    timg2 = pygame.transform.scale(img2, (perWidth-4, perHeight-4))
    screen.blit(timg2, (position2[0]+2, position2[1]+2))
    
    #干擾圖2
    img3 = pygame.image.load(img_path3).convert_alpha()
    timg3 = pygame.transform.scale(img3, (perWidth-4, perHeight-4))
    screen.blit(timg3, (position3[0]+2, position3[1]+2))
    
    #得分按鈕
    defen_bt = Button(window_width-right_width+50, 5, 100, 30, 'LawnGreen', '得分:'+str(score))
    defen_bt.draw(screen, 'LawnGreen')
    
    #暫停開始按鈕
    button.draw(screen, 'Gold1')
    
    #展示操作資訊
    if game_msg_run != '':
        tmp_msg = game_msg_run
    else:
        tmp_msg = game_msg
    defen_msg = ButtonMsg(window_width-right_width+50, 85, 100, 180, 'Honeydew', tmp_msg)
    defen_msg.draw(screen, 'Honeydew')
    
    mouse_pos1 = pygame.mouse.get_pos()
    mouse_pos_list1 = list(mouse_pos1)
    mouse_pos_list1[0] -= perWidth
    mouse_pos_list1[1] -= perHeight
    mouse_pos1 = tuple(mouse_pos_list1)
    screen.blit(mouse_img2, mouse_pos1)
    
    #更新顯示
    pygame.display.flip()
    #pygame.display.update()
    
    dt = clock.tick(60) / 600
    j += 1
    
pygame.quit()

效果:

相關文章