S
S
Serhii Koval2021-10-09 02:06:28
Python
Serhii Koval, 2021-10-09 02:06:28

I can't start the application, it gives me an error. How to decide?

import pygame
from random import randrange
from pygame.locals import *
from tkinter import *
import tkinter as tk
import pygame as pg
import sys
import random

pygame.init()

pygame.mixer.music.load('ms1.mp3')
pygame.mixer.music.play(-1)

root = Tk()
root.geometry('1300x760')
cube = 10

x, y = randrange(cube, root - cube, cube), randrange(cube, root - cube, cube)
apple = randrange(cube, root - cube, cube), randrange(cube, root - cube, cube)
length = 1
snake = [(x, y)]
dx, dy = 0, 0
fps = 60
kl = {'W': True, 'S': True, 'A': True, 'D': True, }
score = 0
speed_count, snake_speed = 0, 10

pygame.init()
surface = pygame.display.set_mode([root, root])
clock = pygame.time.Clock()
font_score = pygame.font.SysFont('Arial', 26, bold=True)
font_end = pygame.font.SysFont('Arial', 66, bold=True)
img = pygame.image.load('1.jpg').convert()

runing = True
 
def run():
    def quit():
        root.destroy()
 
    def exit():
        global runing
        runing = False
        root.destroy()
 
    root = tk.Tk()
    tk.Button(root, text="Перегрузить", command=quit).pack()
    tk.Button(root, text="Закрыть", command=exit).pack()
    root.mainloop()
 
while runing:
    run()

def close_game():
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()

while True:
    surface.blit(img, (0, 0))
    
    [pygame.draw.rect(surface, pygame.Color('green'), (i, j, cube - 1, cube - 1)) for i, j in snake]
    pygame.draw.rect(surface, pygame.Color('red'), (*apple, cube, cube))
    
    render_score = font_score.render(f'SCORE: {score}', 1, pygame.Color('orange'))
    surface.blit(render_score, (5, 5))
    
    speed_count += 1
    if not speed_count % snake_speed:
      x += dx * cube
      y += dy * cube
      snake.append((x, y))
      snake = snake[-length:]
    
    if snake[-1] == apple:
        apple = randrange(cube, root - cube, cube), randrange(cube, root - cube, cube)
        length += 1
        score += 1
        snake_speed -= 1
        snake_speed = max(snake_speed, 4)
    
    if x < 0 or x > root - cube or y < 0 or y > root - cube or len(snake) != len(set(snake)):
        while True:
            render_end = font_end.render('GAME OVER', 1, pygame.Color('orange'))
            surface.blit(render_end, (root // 2 - 200, root // 3))
            pygame.display.flip()
            close_game()

    pygame.display.flip()
    root.mainloop()
    clock.tick(fps)
    close_game()
    
    key = pygame.key.get_pwidth, pressed()
    if key[pygame.K_w]:
        if kl['W']:
            dx, dy = 0, -1
            kl = {'W': True, 'S': False, 'A': True, 'D': True, }
    elif key[pygame.K_s]:
        if kl['S']:
            dx, dy = 0, 1
            kl = {'W': False, 'S': True, 'A': True, 'D': True, }
    elif key[pygame.K_a]:
        if kl['A']:
            dx, dy = -1, 0
            kl = {'W': True, 'S': True, 'A': True, 'D': False, }
    elif key[pygame.K_d]:
        if kl['D']:
            dx, dy = 1, 0
            kl = {'W': True, 'S': True, 'A': False, 'D': True, }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lnerim, 2021-10-09
@AI-problem

As I understand you, after watching this video, you wrote the game "Snake", the source code of which is located at this link. After that you decide to create some login window, but using tkinter instead of pygame.

line 15

root = Tk()
root.geometry('1300x760')
cube = 10

x, y = randrange(cube, root - cube, cube), randrange(cube, root - cube, cube)

rootYou assign an instance of the class to a variable Tk()and then subtract 10 from it, which raises a TypeError.
line 99

key = pygame.key.get_pwidth, pressed()
I don’t know where this came from, but most likely it should be here, looking at the code on GitHub
key = pygame.key.get_pressed()
. And this is only a small part, since in the written code two libraries conflict with each other, so refactoring this code is quite difficult. I advise you to start with the basics of the python language, especially to study the structure of the program itself. After theory, develop console applications, and only then take on programs with an interface, since at this stage it will be very difficult for you to write, at least working code and modify it to suit your needs.
If these words seem like bullshit to you, then google something like this: How to make two windows (displays) on pygame.
PS Next time, attach the text of the error, as you were advised in the comments.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question