Pages

Showing posts with label pygame. Show all posts
Showing posts with label pygame. Show all posts

Monday, October 7, 2024

Fedora 42 : Fedora game project with pygame and agentpy - part 004.

... working on hacking game code , you can test two version of the unfinished game with agentpy at my pagure Fedora account.
* Hack the code based on minimal information versus total information. The code has 5 distinct letters. - click on the letters on the keypad - the number of guessed letters is displayed in the form: centered - guessed letters on positions and moved guessed letters but on other positions NOTE: the source code is under development, I need to enter a scroll to be able to enter more codes with letters

Saturday, September 28, 2024

Fedora 42 : Fedora game project with pygame and agentpy - part 003.

I have upgraded the source code with the following changes:
  • added agent with logic using the python agentpy module;
  • I increased the grid to 16 x 16;
  • I kept the win condition at 8 pieces aligned in any direction;
  • I added conditions for displaying the equality message;
The game is hard to win, you can try on my pagure account.

Wednesday, September 25, 2024

Fedora 42 : Fedora game project with pygame and agentpy - part 002.

... and update from 8 in 8 with SVG file type.
The source code I used or you can find it on the pagure project:
import pygame
from pygame.math import Vector2
import random
import os 
username = os.getlogin()
# Initialize Pygame
pygame.init()
# Set up the display
width, height = 800, 800
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Eight-in-a-Row")
# Font setup
font = pygame.font.Font(None, 74)
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
BLUE = (0, 0, 120)
# Game board dimensions
BOARD_WIDTH = 8
BOARD_HEIGHT = 8

class Player:
    def __init__(self, color, is_computer=False):
        self.color = color
        self.pieces = set()
        self.is_computer = is_computer
        self.svg_image = self.load_svg_image()

    def add_piece(self, x, y):
        self.pieces.add((x, y))

    # def draw_pieces(self):
    #     for x, y in self.pieces:
    #         pygame.draw.circle(screen, self.color, 
    #                            (x * width // BOARD_WIDTH + width // (2 * BOARD_WIDTH), 
    #                             y * height // BOARD_HEIGHT + height // (2 * BOARD_HEIGHT)), 
    #                            min(width, height) // (2 * BOARD_WIDTH) - 5)
    
    def load_svg_image(self):
        if self.color == BLUE:
            svg_path = "penguin-svgrepo-com.svg"
        elif self.color == WHITE:
            svg_path = "cube-svgrepo-com.svg"
        else:
            svg_path = "cube-svgrepo-com.svg"
        return pygame.image.load(svg_path)

    def draw_pieces(self):
        piece_size = min(width, height) // (BOARD_WIDTH) - 10
        for x, y in self.pieces:
            pos = Vector2(x * width // BOARD_WIDTH + width // (2 * BOARD_WIDTH),
                          y * height // BOARD_HEIGHT + height // (2 * BOARD_HEIGHT))
            scaled_image = pygame.transform.scale(self.svg_image, (piece_size, piece_size))
            image_rect = scaled_image.get_rect(center=pos)
            screen.blit(scaled_image, image_rect)

    def make_move(self, board):
        if self.is_computer:
            empty_squares = [(x, y) for x in range(BOARD_WIDTH) for y in range(BOARD_HEIGHT) 
                             if (x, y) not in board[0] and (x, y) not in board[1]]
            if empty_squares:
                return random.choice(empty_squares)
        return None

def check_winner(player):
    directions = [(0, 1), (1, 0), (1, 1), (1, -1)]
    for x, y in player.pieces:
        for dx, dy in directions:
            if all((x + i*dx, y + i*dy) in player.pieces for i in range(8)):
                return True
    return False
    
def display_winner(winner):
    text = font.render(f"Player {winner} wins!", True, [145,190,190])
    text_rect = text.get_rect(center=(width // 2, height // 2))
    screen.blit(text, text_rect)
    pygame.display.flip()
    pygame.time.wait(3000)  # Display the message for 3 seconds

# Create players
player1 = Player(BLUE)
player2 = Player(WHITE, is_computer=True)
# Game loop
running = True
turn = 0
game_over = False

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEBUTTONDOWN and not game_over:
            if turn % 2 == 0:  # Human player's turn
                mouse_x, mouse_y = event.pos
                column = mouse_x // (width // BOARD_WIDTH)
                row = mouse_y // (height // BOARD_HEIGHT)
                
                if (column, row) not in player1.pieces and (column, row) not in player2.pieces:
                    player1.add_piece(column, row)
                    
                    if check_winner(player1):
                        # print("Player 1 wins!")
                        display_winner(username)
                        game_over = True
                    
                    turn += 1

    if not game_over and turn % 2 == 1:  # Computer player's turn
        move = player2.make_move((player1.pieces, player2.pieces))
        if move:
            player2.add_piece(*move)
            
            if check_winner(player2):
                # print("Player 2 (Computer) wins!")
                display_winner("Computer")
                game_over = True
            turn += 1

    screen.fill(BLACK)
    
    # Draw game board
    for i in range(BOARD_WIDTH + 1):
        pygame.draw.line(screen, WHITE, (i * width // BOARD_WIDTH, 0), (i * width // BOARD_WIDTH, height), 2)
    for i in range(BOARD_HEIGHT + 1):
        pygame.draw.line(screen, WHITE, (0, i * height // BOARD_HEIGHT), (width, i * height // BOARD_HEIGHT), 2)

    # Draw pieces
    player1.draw_pieces()
    player2.draw_pieces()
    pygame.display.flip()

pygame.quit()

Saturday, September 21, 2024

Fedora 42 : Fedora game project with pygame and agentpy - part 001.

I started a game project with the python packages pygame and agentpy in the Fedora 42 Linux distribution.
I used this version of Fedora:
[mythcat@fedora fedora_game]$ uname -a
Linux fedora 6.11.0-63.fc42.x86_64 #1 SMP PREEMPT_DYNAMIC Sun Sep 15 17:14:12 UTC 2024 x86_64 GNU/Linux
... and this python version:
Python 3.12.3 (main, Apr 17 2024, 00:00:00) [GCC 14.0.1 20240411 (Red Hat 14.0.1-0)] on linux
You can find it on my fedora pagure repo

Saturday, November 7, 2020

Fedora 33 : Install PyGame 2.0 on Fedora.

Today I will show you how to install the python PyGame version 2.0 package with python version 3.9 in Fedora 33 distro. Let's install all Fedora packages need for this python package:
[root@desk pygame]# dnf install SDL2-devel.x86_64 
...
Installed:
  SDL2-devel-2.0.12-4.fc33.x86_64                                               

Complete!
[root@desk pygame]# dnf install SDL2_ttf-devel.x86_64
...
Installed:
  SDL2_ttf-2.0.15-6.fc33.x86_64       SDL2_ttf-devel-2.0.15-6.fc33.x86_64      

Complete!
[root@desk pygame]# dnf install SDL2_image-devel.x86_64
...
Installed:
  SDL2_image-2.0.5-5.fc33.x86_64      SDL2_image-devel-2.0.5-5.fc33.x86_64     

Complete!
[root@desk pygame]# dnf install SDL2_mixer-devel.x86_64 
...
Installed:
  SDL2_mixer-2.0.4-7.fc33.x86_64      SDL2_mixer-devel-2.0.4-7.fc33.x86_64     

Complete!
[root@desk pygame]# dnf install SDL2_gfx-devel.x86_64 
...
Installed:
  SDL2_gfx-1.0.4-3.fc33.x86_64        SDL2_gfx-devel-1.0.4-3.fc33.x86_64       

Complete!
[root@desk pygame]# dnf install portmidi-devel.x86_64 
...
Installed:
  portmidi-devel-217-38.fc33.x86_64                                             

Complete!
Use this command to clone it from GitHub and install it:
[mythcat@desk ~]$ git clone https://github.com/pygame/pygame
Cloning into 'pygame'...
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 38509 (delta 0), reused 0 (delta 0), pack-reused 38505
Receiving objects: 100% (38509/38509), 17.78 MiB | 11.66 MiB/s, done.
Resolving deltas: 100% (29718/29718), done.
[mythcat@desk ~]$ cd pygame/
[mythcat@desk pygame]$ python3.9 setup.py install --user


WARNING, No "Setup" File Exists, Running "buildconfig/config.py"
Using UNIX configuration...


Hunting dependencies...
SDL     : found 2.0.12
FONT    : found
IMAGE   : found
MIXER   : found
PNG     : found
JPEG    : found
SCRAP   : found
PORTMIDI: found
PORTTIME: found
FREETYPE: found 23.4.17

If you get compiler errors during install, double-check
the compiler flags in the "Setup" file.
...
copying docs/pygame_tiny.gif -> build/bdist.linux-x86_64/egg/pygame/docs
creating build/bdist.linux-x86_64/egg/EGG-INFO
copying pygame.egg-info/PKG-INFO -> build/bdist.linux-x86_64/egg/EGG-INFO
copying pygame.egg-info/SOURCES.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying pygame.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying pygame.egg-info/entry_points.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying pygame.egg-info/not-zip-safe -> build/bdist.linux-x86_64/egg/EGG-INFO
copying pygame.egg-info/top_level.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
writing build/bdist.linux-x86_64/egg/EGG-INFO/native_libs.txt
creating dist
creating 'dist/pygame-2.0.1.dev1-py3.9-linux-x86_64.egg' and adding 'build/bdist.linux-x86_64/egg' to it
removing 'build/bdist.linux-x86_64/egg' (and everything under it)
Processing pygame-2.0.1.dev1-py3.9-linux-x86_64.egg
creating /home/mythcat/.local/lib/python3.9/site-packages/pygame-2.0.1.dev1-py3.9-linux-x86_64.egg
Extracting pygame-2.0.1.dev1-py3.9-linux-x86_64.egg to /home/mythcat/.local/lib/python3.9/site-packages
Adding pygame 2.0.1.dev1 to easy-install.pth file

Installed /home/mythcat/.local/lib/python3.9/site-packages/pygame-2.0.1.dev1-py3.9-linux-x86_64.egg
Processing dependencies for pygame==2.0.1.dev1
Finished processing dependencies for pygame==2.0.1.dev1
Let's test it:
[mythcat@desk pygame]$ ls
build	     dist  examples	    README.rst	setup.cfg  src_c   test
buildconfig  docs  pygame.egg-info  Setup	setup.py   src_py
[mythcat@desk pygame]$ python3.9
Python 3.9.0 (default, Oct  6 2020, 00:00:00) 
[GCC 10.2.1 20200826 (Red Hat 10.2.1-3)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygame
pygame 2.0.1.dev1 (SDL 2.0.12, python 3.9.0)
Hello from the pygame community. https://www.pygame.org/contribute.html
>>> 

Saturday, September 7, 2013

Make one menu using pygame

This is the result of my tutorial named : PyGame - first interface - part 6 .

I use the rect function to make buttons.

Also I check the mouse position and changed the color.

Monday, September 2, 2013

Test pygame , pyopengl and some features ...

Today I worked with pygame and OpenGL python modules.

I want to make a simple script to test and load pygame , OpenGL modules.

The script can be found here.

Thursday, August 15, 2013

Few tutorials with pygame and android ...

Linux can be a goof tool for development...

In the last days I deal with pygame and android.

So if you want to make applications for your tablet or mobile phone then see this:

pygame-first-interface-part-4

working-with-pygame-subset-for-android

working-with-android-emulator-part-001

... also I saw the pygame can give some errors on android. I got this error : android pygame error : failed to dequeue buffer from native window.

So if is somebody know pygame then you can read more here.

Monday, February 9, 2009

PyGame

Pygame is a free set of Python modules.
Is portable and runs on every platform and operating system.
More about pygame documentation on www.pygame.org/docs/
Other links: pygame-catalin.blogspot.com