Python Forum

Full Version: a question
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
is it possible to make smouth games just with python and tkinter like this games in the link

https://codeincomplete.com/games/
Everything there is possible in just pygame. They won't, of course, run in the web; but that is a completely different issue.
(Sep-25-2018, 01:21 PM)hammza Wrote: [ -> ]python and tkinter
You wouldn't want to use tkinter. But with python, sure. As Mekire suggested, pygame is where you should be looking: https://www.pygame.org/news
pygame will end up smoother on games with alot of moving parts.
tkinter on my computer. start to slow down when I reach hundred moving parts.
import tkinter as tk
import random
import time

class Timer:
    def __init__(self):
        self.last = time.perf_counter()
        self.fps = 0

    def tick(self):
        self.fps += 1
        if time.perf_counter() - self.last > 1:
            print(self.fps)
            self.fps = 0
            self.last = time.perf_counter()

class Block:
    def __init__(self, canvas):
        color = "#{:02x}{:02x}{:02x}".format(random.randint(0, 255),
                                             random.randint(0, 255),
                                             random.randint(0, 255))

        x, y = random.randint(50, 700), random.randint(50, 500)
        self.block = canvas.create_rectangle(x, y, x + 50, y + 50, fill=color)

        self.direction = [(random.randint(0, 400) - 200) / 100.0,
                          (random.randint(0, 400) - 200) / 100.0]

    def update(self, canvas):
        canvas.move(self.block, *self.direction)
        coords = canvas.coords(self.block)
        if coords[3] > 599 or coords[1] < 1:
            self.direction[1] = -self.direction[1]

        if coords[2] > 799 or coords[0] < 1:
            self.direction[0] = -self.direction[0]


class App:
    def __init__(self):
        self.root = tk.Tk()
        self.canvas = tk.Canvas(self.root, width=800, height=600)
        self.canvas.pack()
        self.blocks = []
        for i in range(500):
            self.blocks.append(Block(self.canvas))

        self.timer = Timer()
        self.loop()

    def loop(self):
        for block in self.blocks:
            block.update(self.canvas)

        self.timer.tick()
        self.root.after(int(1000/60), self.loop)

app = App()
app.root.mainloop()
import pygame
import random

class Timer:
    def __init__(self):
        self.last = pygame.time.get_ticks()
        self.fps = 0

    def tick(self):
        tick = pygame.time.get_ticks()
        self.fps += 1
        if tick - self.last > 1000:
            print(self.fps)
            self.fps = 0
            self.last = tick

class Block:
    def __init__(self):
        self.rect = pygame.Rect(float(random.randint(50, 700)),
                                float(random.randint(50, 500)), 50, 50)

        self.color = (random.randint(0, 255),
                      random.randint(0, 255),
                      random.randint(0, 255))

        self.direction = [(random.randint(0, 400) - 200) / 100.0,
                          (random.randint(0, 400) - 200) / 100.0]

        self.position = list(map(float, self.rect.topleft))

    def update(self, rect):
        self.position[0] += self.direction[0]
        self.position[1] += self.direction[1]
        self.rect.topleft = tuple(map(int, self.position))

        clamp = self.rect.clamp(rect)
        boolean = False

        if clamp.x != self.rect.x:
            boolean = True
            self.direction[0] = -self.direction[0]

        if clamp.y != self.rect.y:
            boolean = True
            self.direction[1] = -self.direction[1]

        if boolean:
            self.rect = clamp
            self.position = list(map(float, self.rect.topleft))

    def blit(self, surface):
        self.update(surface.get_rect())
        pygame.draw.rect(surface, self.color, self.rect)

def main():
    pygame.init()
    pygame.display.set_caption("Bouncing Blocks")
    surface = pygame.display.set_mode((800, 600))
    clock = pygame.time.Clock()

    blocks = []
    for i in range(500):
        blocks.append(Block())

    running = True
    timer = Timer()

    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False

        timer.tick()
        surface.fill((0,0,0))
        for block in blocks:
            block.blit(surface)

        pygame.display.flip()
        clock.tick(60)

    pygame.quit()

main()
hi windspar nice to see you again :)

thank you for the codes