Answer the question
In order to leave comments, you need to log in
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
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)
root
You 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()
key = pygame.key.get_pressed()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question