If Bedingung in Pygame stoppt nicht

Hier werden alle anderen GUI-Toolkits sowie Spezial-Toolkits wie Spiele-Engines behandelt.
Antworten
MatthiDbr
User
Beiträge: 4
Registriert: Dienstag 10. Mai 2022, 21:48

Was ist der Fehler?
Noch dazu kommt das manchmal das Spiel einfach so abbricht wenn ich es öffne.

Code: Alles auswählen

from ast import While
from cgitb import grey
from ctypes import resize
from curses import resizeterm
from math import gamma
from operator import truediv
from re import M, T
from tkinter import NO, Button, Place
from tracemalloc import start
from turtle import resizemode, title
import pygame
import random

pygame.init()

window_size = [500,500]
game = pygame.display.set_mode((window_size))
pygame.display.set_caption("TicTacToe")
clock = pygame.time.Clock()

text_title = pygame.font.Font(None, 140)
button_titel = pygame.font.Font(None, 120)

Klick = True
player_status = None
counter = 0

matrix = [
    [0,0,0],
    [0,0,0],
    [0,0,0]
]
print(f"{matrix} - Matrix player")



matrix_pc = [
    [0,0,0],
    [0,0,0],
    [0,0,0]
]



print(f"{matrix_pc} - Matrix pc")

start_button = False
game_menu = True
def menu():
    global mouse_position, ttt, game_menu, start_button
    button_size_x = 400
    button_size_y = 80
    game.fill(("#AFEEEE"))

    start_button = pygame.draw.rect(game, ("#E0FFFF"), (44,120,button_size_x,button_size_y), 4)
    start_text = button_titel.render("Start", True, ("White"))
    game.blit(start_text, (152,122))
    if start_button.collidepoint(mouse_position):
        start_button = True

    exit = pygame.draw.rect(game, ("#E0FFFF"), (44,234,button_size_x,button_size_y), 4)
    exit_text = button_titel.render("Exit", True, ("White"))
    game.blit(exit_text, (166,238))
    if exit.collidepoint(mouse_position):
        ttt = False

    title = text_title.render("TicTacToe", True, ("White"))
    game.blit(title, (26,18))

    status_text = pygame.font.Font(None, 80).render(f"You: {player_status}", True, ("White"))
    game.blit(status_text, (10,448))



def drawing():
    global plates, plate_1, plate_2, plate_3, plate_4, plate_5, plate_6, plate_7, plate_8, plate_9
    global Klick, matrix_pc, random_nbr1, random_nbr2, player_status, game_menu, counter
    global player_plate_1, player_plate_2, player_plate_3, player_plate_4, player_plate_5, player_plate_6, player_plate_7, player_plate_8, player_plate_9
    global pc_plate_1, pc_plate_2, pc_plate_3, pc_plate_4, pc_plate_5, pc_plate_6, pc_plate_7, pc_plate_8, pc_plate_9
    pc_start = False
    game.fill(("White"))
    plates = window_size[0]*1/3
    plate_1 = pygame.draw.rect(game, ("#E0FFFF"), (0,0,(plates),(plates)))
    plate_2 = pygame.draw.rect(game, ("#E0FFFF"), (plates+1,0,(plates),(plates)))
    plate_3 = pygame.draw.rect(game, ("#E0FFFF"), (plates*2+1,0,(plates),(plates)))

    plate_4 = pygame.draw.rect(game, ("#E0FFFF"), (0,plates+1,(plates),(plates)))
    plate_5 = pygame.draw.rect(game, ("#E0FFFF"), (plates+1,plates+1,(plates),(plates)))
    plate_6 = pygame.draw.rect(game, ("#E0FFFF"), (plates*2+1,plates+1,(plates),(plates)))

    plate_7 = pygame.draw.rect(game, ("#E0FFFF"), (0,plates*2+1,(plates),(plates)))
    plate_8 = pygame.draw.rect(game, ("#E0FFFF"), (plates+1,plates*2+1,(plates),(plates)))
    plate_9 = pygame.draw.rect(game, ("#E0FFFF"), (plates*2+1,plates*2+1,(plates),(plates)))

#Playerklick
    start_button = True
    if event.type == pygame.MOUSEBUTTONDOWN and Klick and game_menu == False and start_button:
        if plate_1.collidepoint(mouse_position) and Klick and not matrix_pc[0][0] == 1 and not matrix[0][0] == 1:
            #print(f"{Klick} - Klick")
            matrix[0][0] = 1
            Klick = False
            print(matrix)
            print("Player")
            pc_start = True
            print(pc_start)
        if plate_2.collidepoint(mouse_position) and Klick and not matrix_pc[0][1] == 1 and not matrix[0][1] == 1:
            #print(f"{Klick} - Klick")
            matrix[0][1] = 1
            Klick = False
            print(matrix)
            pc_start = True
            print(pc_start)
        if plate_3.collidepoint(mouse_position) and Klick and not matrix_pc[0][2] == 1 and not matrix[0][2] == 1:
            #print(f"{Klick} - Klick")
            matrix[0][2] = 1
            Klick = False
            print(matrix)
            pc_start = True
            print(pc_start)
        if plate_4.collidepoint(mouse_position) and Klick and not matrix_pc[1][0] == 1 and not matrix[1][0] == 1:
            #print(f"{Klick} - Klick")
            matrix[1][0] = 1
            Klick = False
            print(matrix)
            pc_start = True
            print(pc_start)
        if plate_5.collidepoint(mouse_position) and Klick and not matrix_pc[1][1] == 1 and not matrix[1][1] == 1:
            #print(f"{Klick} - Klick")
            matrix[1][1] = 1
            Klick = False
            print(matrix)
            pc_start = True
            print(pc_start)
        if plate_6.collidepoint(mouse_position) and Klick and not matrix_pc[1][2] == 1 and not matrix[1][2] == 1:
            #print(f"{Klick} - Klick")
            matrix[1][2] = 1
            Klick = False
            print(matrix)
            pc_start = True
            print(pc_start)
        if plate_7.collidepoint(mouse_position) and Klick and not matrix_pc[2][0] == 1 and not matrix[2][0] == 1:
            #print(f"{Klick} - Klick")
            matrix[2][0] = 1
            Klick = False
            print(matrix)
            pc_start = True
            print(pc_start)
        if plate_8.collidepoint(mouse_position) and Klick and not matrix_pc[2][1] == 1 and not matrix[2][1] == 1:
            #print(f"{Klick} - Klick")
            matrix[2][1] = 1
            Klick = False
            print(matrix)
            pc_start = True
            print(pc_start)
        if plate_9.collidepoint(mouse_position) and Klick and not matrix_pc[2][2] == 1 and not matrix[2][2] == 1:
            #print(f"{Klick} - Klick")
            matrix[2][2] = 1
            Klick = False
            print(matrix)
            pc_start = True
            print(pc_start)



    pressed_plate_titel = pygame.font.Font(None, 280)
    player = pressed_plate_titel.render("X", True, ("White"))
    pc = pressed_plate_titel.render("O", True, ("White"))
    text_plate_places = 500*1/3

    print(f"{Klick}-Klick")
    print(f"{pc_start}-PC Start")
    random_nbr1 = random.randint(0,2)
    random_nbr2 = random.randint(0,2)
    if Klick == False and pc_start:
        matrix_pc[random_nbr1][random_nbr2]
        
    pc_game_matrix_x = [
        [8, text_plate_places, text_plate_places*2+8],
        [8, text_plate_places+8, text_plate_places*2+8],
        [8, text_plate_places+8, text_plate_places*2+8]
    ]

    pc_game_matrix_y = [
        [0,0,0],
        [text_plate_places, text_plate_places, text_plate_places],
        [text_plate_places*2, text_plate_places*2,  text_plate_places*2]
    ]

    plate1 = 0
    plate2 = 0
    plate3 = 0
    plate4 = 0
    plate5 = 0
    plate6 = 0
    plate7 = 0
    plate8 = 0
    plate9 = 0

    #row 1
    if matrix[0][0] == 1 and not matrix_pc[0][0] == 1 and plate1 == 0:
        player_plate_1 = game.blit(player, (18,0))
        print(f"{matrix}-Player")
        Klick = True
        if Klick:
            game.blit(pc, (pc_game_matrix_x[0][0],pc_game_matrix_y[0][0]))
            Klick = False
            plate1 = +1
    elif matrix[0][1] == 1 and not matrix_pc[0][1] == 1 and plate2 == 0:
        player_plate_2 = game.blit(player, (text_plate_places,0))
        print(f"{matrix}-Player")
        counter += 1
        print(f"{counter}-Counter1")
        Klick = True
        if Klick:
            game.blit(pc, (pc_game_matrix_x[0][0],pc_game_matrix_y[0][0]))
            Klick = False
            plate2 += 1
 
    elif matrix[0][2] == 1 and not matrix_pc[0][2] == 1 and plate3 == 0:
        player_plate_3 = game.blit(player, (text_plate_places*2+14,0))
        print(f"{matrix}-Player")
        counter += 1
        print(f"{counter}-Counter2")
        Klick = True
        if Klick:
            game.blit(pc, (pc_game_matrix_x[0][0],pc_game_matrix_y[0][0]))
            Klick = False
            plate3 += 1
    #row 2
    elif matrix[1][0] == 1 and not matrix_pc[1][0] == 1 and plate4 == 0:
        player_plate_4 = game.blit(player, (18,text_plate_places))
        print(f"{matrix}-Player")
        counter += 1
        print(f"{counter}-Counter3")
        Klick = True
        if Klick:
            game.blit(pc, (pc_game_matrix_x[0][0],pc_game_matrix_y[0][0]))
            Klick = False
            plate4 += 1
    elif matrix[1][1] == 1 and not matrix_pc[1][1] == 1 and plate5 == 0:
        player_plate_5 = game.blit(player, (text_plate_places+14,text_plate_places))
        print(f"{matrix}-Player")
        counter += 1
        print(f"{counter}-Counter4")
        Klick = True
        if Klick:
            game.blit(pc, (pc_game_matrix_x[0][0],pc_game_matrix_y[0][0]))
            Klick = False
            plate5 += 1
    elif matrix[1][2] == 1 and not matrix_pc[1][2] == 1 and plate6 == 0:
        player_plate_6 = game.blit(player, (text_plate_places*2+14,text_plate_places))
        print(f"{matrix}-Player")
        counter += 1
        print(f"{counter}-Counter5")
        Klick = True
        if Klick:
            game.blit(pc, (pc_game_matrix_x[0][0],pc_game_matrix_y[0][0]))
            Klick = False
            plate6 += 1
    #row 3
    elif matrix[2][0] == 1 and not matrix_pc[2][0] == 1 and plate7 == 0:
        player_plate_7 = game.blit(player, (18,text_plate_places*2))
        print(f"{matrix}-Player")
        counter += 1
        print(f"{counter}-Counter6")
        Klick = True
        if Klick:
            game.blit(pc, (pc_game_matrix_x[0][0],pc_game_matrix_y[0][0]))
            Klick = False
            plate7 += 1
    elif matrix[2][1] == 1 and not matrix_pc[2][1] == 1 and plate8 == 0:
        player_plate_8 = game.blit(player, (text_plate_places+14,text_plate_places*2))
        print(f"{matrix}-Player")
        counter += 1
        print(f"{counter}-Counter7")
        Klick = True
        if Klick:
            game.blit(pc, (pc_game_matrix_x[0][0],pc_game_matrix_y[0][0]))
            Klick = False
            plate8 += 1
    elif matrix[2][2] == 1 and not matrix_pc[2][2] == 1 and plate9 == 0:
        player_plate_9 = game.blit(player, (text_plate_places*2+14,text_plate_places*2))
        print(f"{matrix}-Player")
        counter += 1
        print(f"{counter}-Counter8")
        Klick = True
        if Klick:
            game.blit(pc, (pc_game_matrix_x[0][0],pc_game_matrix_y[0][0]))
            Klick = False
            plate9 += 1
    
    

    if matrix[0][0] == 1 and matrix[0][1] ==  1 and matrix[0][2] == 1 and not matrix_pc[0][0] == 1 and not matrix_pc[0][1] == 1 and not matrix_pc[0][2] == 1:
        print("Won")
        player_status = "won"
        game_menu = True
    if matrix[1][0] == 1 and matrix[1][1] ==  1 and matrix[1][2] == 1 and not matrix_pc[1][0] == 1 and not matrix_pc[1][1] == 1 and not matrix_pc[1][2] == 1:
        print("Won")
        player_status = "won"
        game_menu = True
    if matrix[2][0] == 1 and matrix[2][1] ==  1 and matrix[2][2] == 1 and not matrix_pc[2][0] == 1 and not matrix_pc[2][1] == 1 and not matrix_pc[2][2] == 1:
        print("Won")
        player_status = "won"
        game_menu = True
    if matrix[0][0] == 1 and matrix[1][0] ==  1 and matrix[2][0] == 1 and not matrix_pc[0][0] == 1 and not matrix_pc[1][0] == 1 and not matrix_pc[2][0] == 1:
        print("Won")
        player_status = "won"
        game_menu = True
    if matrix[0][1] == 1 and matrix[1][1] ==  1 and matrix[2][1] == 1 and not matrix_pc[0][1] == 1 and not matrix_pc[1][1] == 1 and not matrix_pc[2][1] == 1:
        print("Won")
        player_status = "won"
        game_menu = True
    if matrix[0][2] == 1 and matrix[1][2] ==  1 and matrix[2][2] == 1 and not matrix_pc[0][2] == 1 and not matrix_pc[1][2] == 1 and not matrix_pc[2][2] == 1:
        print("Won")
        player_status = "won"
        game_menu = True
    if matrix[0][0] == 1 and matrix[1][1] ==  1 and matrix[2][2] == 1 and not matrix_pc[0][0] == 1 and not matrix_pc[1][1] == 1 and not matrix_pc[2][2] == 1:
        print("Won")
        player_status = "won"
        game_menu = True
    if matrix[0][2] == 1 and matrix[1][1] ==  1 and matrix[2][0] == 1 and not matrix_pc[0][2] == 1 and not matrix_pc[1][1] == 1 and not matrix_pc[2][0] == 1:
        print("Won")
        player_status = "won"
        game_menu = True

    if matrix_pc[0][0] == 1 and matrix_pc[0][1] ==  1 and matrix_pc[0][2] == 1 and not matrix[0][0] == 1 and not matrix[0][1] == 1 and not matrix[0][2] == 1:
        print("Lose")
        player_status = "lose"
        game_menu = True
    if matrix_pc[1][0] == 1 and matrix_pc[1][1] ==  1 and matrix_pc[1][2] == 1 and not matrix[1][0] == 1 and not matrix[1][1] == 1 and not matrix[1][2] == 1:
        print("Lose")
        player_status = "lose"
        game_menu = True
    if matrix_pc[2][0] == 1 and matrix_pc[2][1] ==  1 and matrix_pc[2][2] == 1 and not matrix[2][0] == 1 and not matrix[2][1] == 1 and not matrix[2][2] == 1:
        print("Lose")
        player_status = "lose"
        game_menu = True
    if matrix_pc[0][0] == 1 and matrix_pc[1][0] ==  1 and matrix_pc[2][0] == 1 and not matrix[0][0] == 1 and not matrix[1][0] == 1 and not matrix[2][0] == 1:
        print("Lose")
        player_status = "lose"
        game_menu = True
    if matrix_pc[0][1] == 1 and matrix_pc[1][1] ==  1 and matrix_pc[2][1] == 1 and not matrix[0][1] == 1 and not matrix[1][1] == 1 and not matrix[2][1] == 1:
        print("Lose")
        player_status = "lose"
        game_menu = True
    if matrix_pc[0][2] == 1 and matrix_pc[1][2] ==  1 and matrix_pc[2][2] == 1 and not matrix[0][2] == 1 and not matrix[1][2] == 1 and not matrix[2][2] == 1:
        print("Lose")
        player_status = "lose"
        game_menu = True
    if matrix_pc[0][0] == 1 and matrix_pc[1][1] ==  1 and matrix_pc[2][2] == 1 and not matrix[0][0] == 1 and not matrix[1][1] == 1 and not matrix[2][2] == 1:
        print("Lose")
        player_status = "lose"
        game_menu = True
    if matrix_pc[0][2] == 1 and matrix_pc[1][1] ==  1 and matrix_pc[2][0] == 1 and not matrix[0][2] == 1 and not matrix[1][1] == 1 and not matrix[2][0] == 1:
        print("Lose")
        player_status = "lose"
        game_menu = True
    

ttt = True
while ttt:
    mouse_position = pygame.mouse.get_pos()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            ttt = False
        if event.type == pygame.MOUSEBUTTONDOWN and game_menu:
            print(Klick)
            game_menu = False
            print(Klick)
             
    if game_menu:
        menu()
    else:
        drawing()

    pygame.display.update()
    clock.tick(60)
Sirius3
User
Beiträge: 17750
Registriert: Sonntag 21. Oktober 2012, 17:20

Du hast Deine IDE nicht im Griff. Die automatisch generierten Importe sein zum Großteil Quatsch und zwingen den Leser erst einmal, das aufzuräumen.
Dreihundert Zeilen kopierter Code und Funktionen mit 34 als global definierter Variablen animieren auch nicht gerade zum Helfen.
Der erste Schritt wäre also, den Code so aufzuräumen, dass der Fehler leicht zu finden ist.
Im jetzigen Zustand ist eine Fehlersuche unmöglich.

Aller Code ist in Funktionen organisiert. Funktionen bekommen alles was sie brauchen über Argumente, und geben Rückgabewerte per `return` zurück. Funktionen sind so kurz, dass man sie nicht die Übersicht verliert. Globale Variablen darf man nicht verwenden. Wenn man anfängt Variablen durchzunummerieren, will man eigentlich Listen verwenden, und statt Code zu kopieren und leicht zu ändern, benutzt man Funktionen und Schleifen.

Eine Frage im Forum besteht aus der Beschreibung, was man machen möchte, wie das beobachtete Verhalten ist, und wie dieses Verhalten von der Erwartung abweicht. Wenn man eine Fehlermeldug erhält, postet man sie komplett mit dem zugehörigen Code.
Mit einem "Was ist der Fehler?" kann niemand etwas anfangen.
Antworten