Dec-14-2018, 10:07 PM
Hi there,
I've used Python in school for like two years, so I really don't know what I'm doing. Excuse that please and don't worry about my messy code.
I'm currently coding to check a wether a click was performed on the right tile.
I've gotten so far, that I can spawn in images to give feedback to the user, but I also want to count the times, the user is clicking the wrong tile.
My problem is, that I defined "wrong" as following:
(Yes, I'm using actual pixel numbers, but I will change that after testing(also ignore my suboptimal code))
So my question is:
How do I solve this properly?
Thanks in advance for any help!
Here is the whole Code for you to orientate in case of irritation:
I've used Python in school for like two years, so I really don't know what I'm doing. Excuse that please and don't worry about my messy code.
I'm currently coding to check a wether a click was performed on the right tile.
I've gotten so far, that I can spawn in images to give feedback to the user, but I also want to count the times, the user is clicking the wrong tile.
My problem is, that I defined "wrong" as following:
def wrong (wx,wy):
window.blit(wrongpng, (wx,wy))
lcchange = -1this is called upon in this:(Yes, I'm using actual pixel numbers, but I will change that after testing(also ignore my suboptimal code))
def a(av):
mposx, mposy = pygame.mouse.get_pos()
if av == 1:
if mposx >= tx+16 and mposx <= tx+26 and mposy >= ty+16 and mposy <= ty+26:
rectangle (tx+16, ty+16, 10, 10, GREY)
if av == 2:
if mposx >= tx+27 and mposx <= tx+37 and mposy >= ty+16 and mposy <= ty+26:
wrong(wx,wy)
if av == 3:
if mposx >= tx+38 and mposx <= tx+48 and mposy >= ty+16 and mposy <= ty+26:
wrong(wx,wy)
if av == 4:
if mposx >= tx+49 and mposx <= tx+59 and mposy >= ty+16 and mposy <= ty+26:
wrong(wx,wy)
if av == 5:
if mposx >= tx+60 and mposx <= tx+70 and mposy >= ty+16 and mposy <= ty+26:
rectangle (tx+60, ty+16, 10, 10, GREY)which is called in this: for av in range (6):
a(av)
av = av+1because I wanted to change the Lifecounter in this: Lifecounter = Lifecounter + lcchange
lcchange = 0I've tested with print in every imaginable place and I've realized, that in the instant I return to the a for-loop that the lcchange variable is back to 0 although it changed it to -1 in the "wrong def" So my question is:
How do I solve this properly?
Thanks in advance for any help!
Here is the whole Code for you to orientate in case of irritation:
import pygame, sys
from pygame.locals import *
import time
import random
import pygame
pygame.init()
# CLOCK ######################################
FPS = 30
clock = pygame.time.Clock()
# FENSTER ####################################
width = 1200
height = 600
Fenster = pygame.display.set_mode((width,height))
pygame.display.set_caption('Sudoku in Cool')
# VARIABLES ##################################
BLACK = (0,0,0)
GREY = (40,40,40)
WHITE = (255,255,255)
tx = 500
ty = 200
wx = 500
wy = 300
testpng = pygame.image.load('Test.png')
wrongpng = pygame.image.load('wrong.png')
def test (tx,ty):
Fenster.blit(testpng, (tx,ty))
def rectangle(rex, rey, rew, reh, color):
pygame.draw.rect(Fenster, color, [rex, rey, rew, reh])
# GAMELOOP ###################################
def game_loop():
End = False
def wrong (wx,wy):
Fenster.blit(wrongpng, (wx,wy))
lcchange = -1
def a(av):
mposx, mposy = pygame.mouse.get_pos()
if av == 1:
if mposx >= tx+16 and mposx <= tx+26 and mposy >= ty+16 and mposy <= ty+26:
rectangle (tx+16, ty+16, 10, 10, GREY)
if av == 2:
if mposx >= tx+27 and mposx <= tx+37 and mposy >= ty+16 and mposy <= ty+26:
wrong(wx,wy)
if av == 3:
if mposx >= tx+38 and mposx <= tx+48 and mposy >= ty+16 and mposy <= ty+26:
wrong(wx,wy)
if av == 4:
if mposx >= tx+49 and mposx <= tx+59 and mposy >= ty+16 and mposy <= ty+26:
wrong(wx,wy)
if av == 5:
if mposx >= tx+60 and mposx <= tx+70 and mposy >= ty+16 and mposy <= ty+26:
rectangle (tx+60, ty+16, 10, 10, GREY)
def b(bv):
mposx, mposy = pygame.mouse.get_pos()
if bv == 1:
if mposx >= tx+16 and mposx <= tx+26 and mposy >= ty+27 and mposy <= ty+37:
rectangle (tx+16, ty+27, 10, 10, GREY)
if bv == 2:
if mposx >= tx+27 and mposx <= tx+37 and mposy >= ty+27 and mposy <= ty+37:
rectangle (tx+27, ty+27, 10, 10, GREY)
if bv == 3:
if mposx >= tx+38 and mposx <= tx+48 and mposy >= ty+27 and mposy <= ty+37:
rectangle (tx+38, ty+27, 10, 10, GREY)
if bv == 4:
if mposx >= tx+49 and mposx <= tx+59 and mposy >= ty+27 and mposy <= ty+37:
wrong(wx,wy)
if bv == 5:
mposx, mposy = pygame.mouse.get_pos()
if mposx >= tx+60 and mposx <= tx+70 and mposy >= ty+27 and mposy <= ty+37:
rectangle (tx+60, ty+27, 10, 10, GREY)
def c(cv):
mposx, mposy = pygame.mouse.get_pos()
if cv == 1:
if mposx >= tx+16 and mposx <= tx+26 and mposy >= ty+38 and mposy <= ty+48:
wrong(wx,wy)
if cv == 2:
mposx, mposy = pygame.mouse.get_pos()
if mposx >= tx+27 and mposx <= tx+37 and mposy >= ty+38 and mposy <= ty+48:
wrong(wx,wy)
if cv == 3:
if mposx >= tx+38 and mposx <= tx+48 and mposy >= ty+38 and mposy <= ty+48:
rectangle (tx+38, ty+38, 10, 10, GREY)
if cv == 4:
if mposx >= tx+49 and mposx <= tx+59 and mposy >= ty+38 and mposy <= ty+48:
wrong(wx,wy)
if cv == 5:
if mposx >= tx+60 and mposx <= tx+70 and mposy >= ty+38 and mposy <= ty+48:
rectangle (tx+60, ty+38, 10, 10, GREY)
def d(dv):
mposx, mposy = pygame.mouse.get_pos()
if dv == 1:
if mposx >= tx+16 and mposx <= tx+26 and mposy >= ty+49 and mposy <= ty+59:
wrong(wx,wy)
if dv == 2:
if mposx >= tx+27 and mposx <= tx+37 and mposy >= ty+49 and mposy <= ty+59:
rectangle (tx+27, ty+49, 10, 10, GREY)
if dv == 3:
if mposx >= tx+38 and mposx <= tx+48 and mposy >= ty+49 and mposy <= ty+59:
rectangle (tx+38, ty+49, 10, 10, GREY)
if dv == 4:
if mposx >= tx+49 and mposx <= tx+59 and mposy >= ty+49 and mposy <= ty+59:
rectangle (tx+49, ty+49, 10, 10, GREY)
if dv == 5:
if mposx >= tx+60 and mposx <= tx+70 and mposy >= ty+49 and mposy <= ty+59:
rectangle (tx+60, ty+49, 10, 10, GREY)
def e(ev):
mposx, mposy = pygame.mouse.get_pos()
if ev == 1:
if mposx >= tx+16 and mposx <= tx+26 and mposy >= ty+60 and mposy <= ty+70:
wrong(wx,wy)
if ev == 2:
if mposx >= tx+27 and mposx <= tx+37 and mposy >= ty+60 and mposy <= ty+70:
rectangle (tx+27, ty+60, 10, 10, GREY)
if ev == 3:
if mposx >= tx+38 and mposx <= tx+48 and mposy >= ty+60 and mposy <= ty+70:
wrong(wx,wy)
if ev == 4:
if mposx >= tx+49 and mposx <= tx+59 and mposy >= ty+60 and mposy <= ty+70:
wrong(wx,wy)
if ev == 5:
if mposx >= tx+60 and mposx <= tx+70 and mposy >= ty+60 and mposy <= ty+70:
rectangle (tx+60, ty+60, 10, 10, GREY)
Fenster.fill (BLACK)
tx = 500
ty = 200
test(tx,ty)
mposx = 0
mposy = 0
Lifecounter = 3
# EVENTHANDLING ##############################
while not End:
for event in pygame.event.get():
if event.type == QUIT:
End = True
if event.type == pygame.MOUSEBUTTONUP:
mposx, mposy = pygame.mouse.get_pos()
rectangle (wx, wy, 75, 75, BLACK)
lcchange = 0
for av in range (6):
a(av)
av = av+1
for bv in range (6):
b(bv)
bv = bv+1
for cv in range (6):
c(cv)
cv = cv+1
for dv in range (6):
d(dv)
dv = dv+1
for ev in range (6):
e(ev)
ev = ev+1
Lifecounter = Lifecounter + lcchange
lcchange = 0
if Lifecounter == 0:
End = True
pygame.display.update()
clock.tick(FPS)
game_loop()
pygame.quit ()
