首页 > 其他分享 >pygame开发小游戏

pygame开发小游戏

时间:2024-08-16 16:06:26浏览次数:8  
标签:text self width 小游戏 pygame 开发 mouse rect

代码:

#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()

 

效果:

 

标签:text,self,width,小游戏,pygame,开发,mouse,rect
From: https://www.cnblogs.com/xuxiaobo/p/18363050

相关文章

  • Workbench开发指南:仿真流程集成
    Workbench框架提供访问数据集成应用程序的接口,用户可以使用Python脚本功能去访问接口,执行目标组件程序的脚本命令,实现仿真流程定制和集成。1、Workbench框架概述数据集成(Data-integrated)应用程序,如MechanicalAPDL、Fluent、CFX、DM、SCDM、Mechanical等,都是独立于Workbenc......
  • Web前端开发 CSS常用样式大全
    Web前端开发CSS常用样式大全文章目录1CSS简介2选择器2.1标签选择器2.2类选择器2.3id选择器2.4复合选择器2.4.1后代选择器2.4.2子代选择器2.4.3并集选择器2.4.4交集选择器2.4.5伪类选择器2.5结构伪类选择器2.6伪元素选择器3CSS特性3.1继承性3.2层叠性......
  • 一个创新的国密前后分离快速开发平台,提供工作流、多租户、多数据源、Vue3表单设计器,高
    前言在当前的软件开发领域,尤其是企业级应用开发中,开发者面临着诸多挑战,如代码安-全、数据加密、国产化适配等。传统的开发平台往往难以满足这些日益增长的需求,特别是在国产化替代的大背景下,对于符合国家安-全标准的软件需求愈发迫切。这就需要一款能够解决上述痛点,同时提供高......
  • SSM-国外鞋服代购平台-97782(免费领源码+开发文档)可做计算机毕业设计JAVA、PHP、爬虫、
    SSM国外鞋服代购平台摘 要随着科学技术的飞速发展,社会的方方面面、各行各业都在努力与现代的先进技术接轨,通过科技手段来提高自身的优势,鞋服代购平台当然也不例外。代购平台是以实际运用为开发背景,运用软件工程原理和开发方法,采用Java技术构建的一个管理系统。整个开发过......
  • flink车联网项目前篇:数据开发(第66天)
    系列文章目录03_数据仓库开发开发规范1.1数据库划分规范1.2表命名规范1.3表字段类型规范开发前准备3.1业务系统表3.2数据导入04_维度主题相关表结构1.1dim_area-城市字典表1.2dim_car_info-车辆信息表1.3dim_car_vendor-车队信息表1.4dim_date_w......
  • Linux:开发工具(1)
    一、软件包管理器yum1.1Linux下安装软件的方式1、源代码安装(直接给你源码,你去进行编译——对用户要求太高!)    但是这样的话,就不光是源码,还需要把别人的一些相关编译环境配置也得搞过来,比如说我这个代码是用C语言写的,那么我们就需要C语言相关的编译器和库。对使用......
  • 用JavaScript做超级玛丽小游戏
    一、前言前几天用JS实现扫雷和贪吃蛇(通过HTML的DOM节点实现基本界面,界面背景简单,交互简单)。比较复杂的是植物大战僵尸,不同的关卡设置单独的函数。所以还比较难。超级玛丽通过canvas实现背景,交互很复杂,功能很多,JS代码完全是有汇编语言反编译成C语言,然后把C语言转换成JS实现的......
  • 嵌入式开发应该具备的思维方式
    能从PC机器编程去看嵌入式问题,那是第一步;学会用嵌入式编程思想,那是第二步;用PC的思想和嵌入式的思想结合在一起,应用于实际的项目,那是第三步。很多朋友都是从PC编程转向嵌入式编程的。在中国,嵌入式编程的朋友很少是正儿八经从计算机专业毕业的,都是从自动控制啊,电子相关的专......
  • Delphi开发新纪元:探索持续集成与持续部署的自动化之路
    标题:“Delphi开发新纪元:探索持续集成与持续部署的自动化之路”引言在软件工程领域,持续集成(CI)和持续部署(CD)是敏捷开发的关键实践,它们确保了代码的高质量和快速迭代。对于Delphi开发者而言,选择合适的CI/CD工具对于提高开发效率和软件质量至关重要。一、DelphiCI/CD工具概......
  • 【Django开发】前后端分离django美多商城项目第1篇:欢迎来到美多 项目主要页面介绍【附
    本教程的知识点为:项目准备项目准备配置1.修改settings/dev.py文件中的路径信息2.INSTALLED_APPS3.数据库用户部分图片1.后端接口设计:视图原型2.具体视图实现用户部分使用Celery完成发送判断帐号是否存在1.判断用户名是否存在后端接口设计:用户部分......