Certainly! Below is a Python script using PyQt6 to create a game launcher. The launcher will display game posters from the E:\game directory. When you hover over a poster, a gradient overlay will appear with a "Play" triangle button.
First, ensure you have PyQt6 installed:
pip install pyqt6
Here's the complete code:
import sys
import os
from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QPushButton, QGridLayout
from PyQt6.QtGui import QPainter, QBrush, QColor, QIcon, QPixmap, QPainterPath
from PyQt6.QtCore import Qt, QPropertyAnimation, QRect, QEasingCurve
import subprocess
class GameLauncher(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('Game Launcher')
self.setGeometry(100, 100, 800, 600)
layout = QVBoxLayout()
games_folder = "E:/game"
game_folders = [f for f in os.listdir(games_folder) if os.path.isdir(os.path.join(games_folder, f))]
grid_layout = QGridLayout()
row, col = 0, 0
for game_folder in game_folders:
game_path = os.path.join(games_folder, game_folder)
exe_file = self.find_exe(game_path)
poster_path = os.path.join(game_path, "1.jpg")
if exe_file and os.path.isfile(poster_path):
game_widget = GameWidget(poster_path, exe_file)
grid_layout.addWidget(game_widget, row, col)
col += 1
if col == 3: # 3 games per row
col = 0
row += 1
layout.addLayout(grid_layout)
self.setLayout(layout)
def find_exe(self, folder):
for file in os.listdir(folder):
if file.lower().endswith(".exe"):
return os.path.join(folder, file)
return None
class GameWidget(QWidget):
def __init__(self, poster_path, exe_path):
super().__init__()
self.exe_path = exe_path
self.poster = QLabel(self)
self.poster.setPixmap(QPixmap(poster_path).scaled(200, 150, Qt.AspectRatioMode.KeepAspectRatio))
self.poster.setScaledContents(True)
self.play_button = QPushButton(self)
self.play_button.setIcon(QIcon("play_triangle.png")) # You need to provide a play triangle icon
self.play_button.setStyleSheet("background-color: transparent; border: none;")
self.play_button.clicked.connect(self.run_game)
self.play_button.hide()
layout = QVBoxLayout(self)
layout.addWidget(self.poster)
layout.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.setLayout(layout)
self.setFixedSize(200, 150)
self.enterEvent = self.on_enter
self.leaveEvent = self.on_leave
self.animation = QPropertyAnimation(self, b"windowOpacity")
self.animation.setDuration(300)
self.animation.setEasingCurve(QEasingCurve.Type.InOutQuad)
def on_enter(self, event):
self.play_button.show()
self.animation.setStartValue(0)
self.animation.setEndValue(0.5)
self.animation.start()
def on_leave(self, event):
self.play_button.hide()
self.animation.setStartValue(0.5)
self.animation.setEndValue(0)
self.animation.start()
def paintEvent(self, event):
painter = QPainter(self)
painter.setRenderHint(QPainter.RenderHint.Antialiasing)
if self.underMouse():
gradient = QLinearGradient(0, self.height(), 0, 0)
gradient.setColorAt(0, QColor(0, 0, 0, 0))
gradient.setColorAt(1, QColor(0, 0, 0, 128))
path = QPainterPath()
path.addRoundedRect(self.rect(), 10, 10) # 10 is the radius for rounded corners
painter.fillPath(path, QBrush(gradient))
super().paintEvent(event)
def run_game(self):
subprocess.Popen(self.exe_path)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = GameLauncher()
ex.show()
sys.exit(app.exec())
Explanation:
-
GameLauncher Class:
- Initializes the main window and layout.
- Scans the
E:/game directory for game folders.
- Creates
GameWidget instances for each game.
-
GameWidget Class:
- Displays the game poster.
- Shows a "Play" button with a triangle icon when hovered.
- Applies a gradient overlay when hovered.
- Runs the game executable when the "Play" button is clicked.
Notes:
- You need to provide a
play_triangle.png icon for the play button.
- The gradient and rounded corners are applied in the
paintEvent method.
- The
QPropertyAnimation is used to animate the gradient overlay.