User avatar
NPDedyukhin
Posts: 29
Joined: Fri Sep 20, 2019 3:23 am
Location: RU

Round trip cycle

Wed Oct 09, 2019 5:30 am

Good afternoon! :)

I have a variable: "a"

It can take two values: 1 or 2.

By default, the variable must be of type: 1

Further, using "pygame", we analyze events on the keyboard - if one of the arrows is pressed (left or right), the variable value changes from 1 to 2, and vice versa - each time you click on one of the arrows on the keyboard.

If you press Enter, then the following script is run.

Please tell me how to make a cycle of constant change from one value to another? :?:

Code: Select all

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pygame
pygame.init()
screen = pygame.display.set_mode((400,400))

a = 1
done = False
while not done:
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
            	#how to change the value of "type_con" here?
            if event.key == pygame.K_RIGHT:
            	#how to change the value of "type_con" here?
            if event.key == pygame.K_RETURN:
                import write_connection_01
Respectfully,
Nikita Dedyukhin

User avatar
NPDedyukhin
Posts: 29
Joined: Fri Sep 20, 2019 3:23 am
Location: RU

Re: Round trip cycle

Wed Oct 09, 2019 2:17 pm

NPDedyukhin wrote:
Wed Oct 09, 2019 5:30 am
Good afternoon! :)

I have a variable: "a"

It can take two values: 1 or 2.

By default, the variable must be of type: 1

Further, using "pygame", we analyze events on the keyboard - if one of the arrows is pressed (left or right), the variable value changes from 1 to 2, and vice versa - each time you click on one of the arrows on the keyboard.

If you press Enter, then the following script is run.

Please tell me how to make a cycle of constant change from one value to another? :?:

Code: Select all

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pygame
pygame.init()
screen = pygame.display.set_mode((400,400))

a = 1
done = False
while not done:
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
            	#how to change the value of "type_con" here?
            if event.key == pygame.K_RIGHT:
            	#how to change the value of "type_con" here?
            if event.key == pygame.K_RETURN:
                import write_connection_01
In order to, when an event occurs, a variable changes between two values, it is necessary:

At the very beginning, create this variable (a) and two more (b and c).

Then, we set the variable “a” to 1

And for the remaining two variables (b and c) we assign the value 2.

We have:
A = 1
B = 2
C = 2

Further, when an event occurs, we need to do the following:

To the variable "c" we assign the value to the variable "a".
We assign the value of the variable b to the variable “a”.
To the variable "b" we assign the value to the variable "c".

And we get:
A = 2
B = 1
C = 1

(the value of A changed from 1 to 2 - what you need)

If the event occurs repeatedly, we get:
A = 1
B = 2
C = 2

(the value of A reversed from 2 to 1 - that's right)

Code:

Code: Select all

#!/usr/bin/env python
# -*- coding: utf-8 -*-
a = 1
b = 2
c = 2
a = str(a)
b = str(b)
c = str(c)

import pygame
pygame.init()
screen = pygame.display.set_mode((400,400))

done = False
while not done:
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
            	c = a
                a = b
                b = c
            if event.key == pygame.K_RIGHT:
            	c = a
                a = b
                b = c
            if event.key == pygame.K_RETURN:
                import write_connection_01
Respectfully,
Nikita Dedyukhin

User avatar
joelostinspace
Posts: 51
Joined: Sat Aug 10, 2019 2:51 pm
Location: Earth

Re: Round trip cycle

Wed Oct 09, 2019 5:31 pm

I've seen this solved multiple ways- use a as a count and all even counts are true and Boolean.
I look at this as a Boolean problem and solution: if it's one way then change it, then vice versa.
I like to solve these problems with the interrupter so fallow along with your favorite:

Code: Select all

>>> a= 1
>>> def check_boolean(var):
	    global a
	    if var == True:
		a = 0
		print('false')
	    else:
		a = 1
		print('Now True')
>>> check_boolean(a)
false
>>> check_boolean(a)
Now True
>>> check_boolean(a)
false
>>> 

Code: Select all

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pygame
pygame.init()
screen = pygame.display.set_mode((400,400))

a = 1
def check_boolean(var):
    global a
    if var == True:
        a = 0
	print('false')
    else:
    a = 1
    print('Now True')
		
done = False
while not done:
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
            	check_boolean(a)
            if event.key == pygame.K_RIGHT:
            	check_boolean(a)
            if event.key == pygame.K_RETURN:
                import write_connection_01
then you need a if statement example:

Code: Select all

if a == True:
    do something...
else:
    something else.....

edit: code indent may be off...

I would rather live in a world where my life is surrounded by mystery than a world so small that my mind could comprehend it.


--- Harry Emerson Fosdick

Return to “Python”