User Tools

Site Tools


2048_game

2048 game

Step 1: PyGame Framework

  • Step 1: Use PyGame Framework.
  • Step 2: Create the Player class which is a circle with a number inside.

Step 2. Make a basic 'Player' that can move around.



import pygame
import time
from Color import *
from Screen import *

# Define the Bullet Sprite class
class Player(pygame.sprite.Sprite):
    SPEED = 5
    SIZE = 40
    COLOR = Color.WHITE

    def __init__(self):
        super().__init__()

        # Create a surface for the player with per-pixel alpha (so the circle is smooth)
        self.image = pygame.Surface((self.SIZE, self.SIZE), pygame.SRCALPHA)

        # Define the position and radius for the circle
        pos = (self.SIZE // 2, self.SIZE // 2)
        radius = self.SIZE // 2

        # Draw the circle on the surface
        pygame.draw.circle(self.image, self.COLOR, pos, radius, True)
        self.draw_text("2")  # Example with the number '2'

        # Get the rectangle for positioning
        self.rect = self.image.get_rect()

        # Set the initial position
        self.rect.center = (Screen.WIDTH // 2, Screen.HEIGHT - 50)


    def update(self):
        # Restrict to screen.
        if self.rect.x < 0:
            self.rect.x = 0
        if self.rect.x > Screen.WIDTH - self.SIZE:
            self.rect.x = Screen.WIDTH - self.SIZE
        if self.rect.y < 0:
            self.rect.y = 0
        if self.rect.y > Screen.HEIGHT - self.SIZE:
            self.rect.y = Screen.HEIGHT - self.SIZE

        # Easier to read, but, slower.
        #self.rect.x = max(0, min(self.rect.x, Screen.WIDTH - self.SIZE))
        #self.rect.y = max(0, min(self.rect.y, Screen.HEIGHT - self.SIZE))


    def move(self, dx, dy):
        self.rect.x += dx
        self.rect.y += dy

    def draw_text(self, text, color = "white"):
        # Load the default font, you can also specify your own font here
        font = Font(pygame.font.Font(None, 24)  # 24 is the font size

        # Render the text (True is for anti-aliasing)
        text_surface = font.render(text, True, color)

        # Get the rectangle of the text surface and center it within the circle
        text_rect = text_surface.get_rect(center=(self.SIZE // 2, self.SIZE // 2))

        # Blit (draw) the text surface onto the player surface (self.image)
        self.image.blit(text_surface, text_rect)

Basic Controls

Add this to Game.py:

    def checkEvents(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.running = False
                return

            if event.type == pygame.KEYDOWN:
                # key down event, process keys.

                if event.key == pygame.K_q:
                    self.quit_game()
                if event.key == pygame.K_ESCAPE:
                    self.quit_game()

        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT]:
            self.player.move(-1,0)

        if keys[pygame.K_RIGHT]:
            self.player.move(1, 0)

        if keys[pygame.K_UP]:
            self.player.move(0, -1)

        if keys[pygame.K_DOWN]:
            self.player.move(0, 1)

Font Problems

Notice the Font(None, 24). If you want more control you can copy code from Window or make your own Font class. You could try something like the following:

Font.py

You will notice this is just the “setFont()” from the old Window class.

import pygame

class Font:
    def __init__(self, filename, size):
        pygame.font.init()
        self.font = pygame.font.Font(filename, size)

        font_width, font_height = self.font.size("@")
        self.fontwidth = font_width
        self.fontheight = size - 2

Window.py

Modify setFont() as follows (don't forget 'from Font import *'):

    def setFont(self, filename, size):
        font = Font(filename, size)
        self.fontwidth = font.fontwidth
        self.fontheight = font.fontheight
        self.font = font.font
        return self.font
2048_game.txt · Last modified: 2024/09/21 02:38 by appledog

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki