When I run my code, it opens up a random-generated maze for the user to solve by using the agent object, mover, to move around the maze. When my object reaches the end of the maze, in which it is at the top-left of the maze with the position (1,1), I want it to print "Done" in the terminal. The problem is that the program won't recognize the status of the object's position when it is at the end of the maze, resulting in the conditional statement (shown below) not working.
from pyamaze import maze,COLOR,agent,textLabel
m=maze(10, 10)
m.CreateMaze()
mover = agent(m, footprints=True, color = "red")
m.enableWASD(mover)
if(mover.position == (1,1)):
print("Done")
m.run()
However, when I place my object at the end of the maze then run the program, the conditional statement surprisingly works and it prints out the result. The problem is that I want my object to be controlled by the user when it starts off at the bottom of the maze and when the user reaches the end of the maze, I want the conditional statement to work and print out the result in the terminal.
mover.position = (1,1)
if(mover.position == (1,1)):
print("Done")
Can someone help me in solving this problem, so that my object's position can be recognized when the user reaches the end of the maze, making the conditional statement to work?
Related
I've just started learning python and started working on a project.
I'm moving my mouse cursor from one area to another. I just want it to stop when it reach a certain point/area/zone. I'm using the mouse position to tell me where it is, currently.
For some reason, when this loops starts, it keeps looping even when the IF statement is true.
But if I started when the IF statement is true, the loops kinda works as intended, so far it's only reading the 'X' values.
I couldn't find an answer to this, or any questions like it. If anyone has an idea or can point me to a similar post, I'll appreciate it.
import pyautogui, sys, time, autoit
#Search for a position on screen manually
try:
while True:
x, y = pyautogui.position()
print(pyautogui.position())
print('Stopping for 1 seconds, keep searching or CTRL + C to end')
time.sleep(1)
#Confirmed location on screen.
if pyautogui.position(x,y) >= pyautogui.position(710, 15):
pyautogui.leftClick()
print('The Eagle has landed')
print(pyautogui.position())
break
Update: I got it! Following mkrieger1 advice, I manage to get the 'x, y' values to update. Code was rewritten.
def death_en():
death = pygame.Surface.blit(pygame.image.load('tombstone.png'))
if x + (WarriorSize_x * .8) == x_en:
screenDisplay.blit(death, (x_en, y_en))
I'm new to Python and over all programing. I have started learning about pygame and I'm trying to create a game. What I want this function to do is to put another image on top of the enemy that was killed, though nothing happens when I get close enough to it with the main character. I now I haven't assigned a y-axis, but want to make sure this works first. I could send the whole code if it's necessary.
Thanks in advance.
To check for collisions use the PyGame Rect Class. Keep a rectangle for your player, and a rectangle for each enemy, updating the position of the rect whenever the item it tracks changes position. Also, when an enemy or the player moves, use the function Rect.colliderect() to determine if the two items have intersected on-screen.
This might be something like:
tombstone_image = pygame.image.load('tombstone.png')
...
# Inside main loop
# Have there been any collisions?
for e_rect in all_enemy_rects:
if ( e_rect.colliderect( player_rect ) ):
screenDisplay.blit( tombstone_image, e_rect )
# TODO: remove enemy from game
I know the question is not very precise, and that is because I have no idea what is going on either. I have narrowed down the problem to a couple of lines, but to me the execution seems rather weird.
def loadmaze(backup):
test = createmaze(10,10) #This creates a new 10x10 matrix
maze = placewalls(test, "x") #The X is a string with wall positions
if maze != test:
#when placewalls executed correctly, and returned a maze different from the original
showmaze(maze) #simply printing maze to console
else:
return backup #so nothing changes, looks as if function never happened
return maze,10,10
# this returns the new maze, with walls, and the dimensions of the new maze
def placewalls(amaze,positions):
#placing walls, definitely correct
return amaze-with-walls
Obviously I changed the variable names so it's clear what I'm doing.
The problem is, when I call the function loadmaze(maze), it never returns a maze with walls. Somehow the test and maze values are always identical. I do not understand how this can be as the maze with walls is assigned to maze after test, which at that point does not have walls.
According to the debug, test also contains the walls after the third line has been executed.
Please help, I'm terribly confused.
As #stephan said, the variables test and maze refer to the same objects in the memory. So I changed maze = placewalls(test,"x") with maze = placewalls(createmaze(10,10),"x"), thus creating a new object different from maze. This then results in test and maze being two different mazes.
In Pygame, I'm making a game with highscores. I want to blit a highscore variable onto the screen, called c but whenever I try to run the program, it freezes and I don't know why. How can I blit the variable c to the screen without it freezing? Here is the relevant code:
# High score (printed in the corner)
c=0
while c>=0:
c=c+1
highscore=myfont6.render("High score:",True, THECOLORS["purple"])
score=myfont6.render(c, True, THECOLORS["purple"])
screen.blit(highscore, (20,20))
screen.blit(score,(40,20))
It's this chunk right here. Once you hit this chunk of code, your program will continue to loop forever.
c=0
while c>=0:
c=c+1
I'm guessing you intended to have c increase by one on every loop of the main game loop. If that's the case, just remove the first two lines and call c += 1 on each main game loop.
I am setting up a Fitts' law experiment where I would like a participant to click on a start button, move the mouse in a straight line, and then click on a target. After clicking start, if the participant moves their mouse too far up or down in the vertical direction I would like to display a white screen for 30 seconds and accept no input, afterward moving to the next trial.
Currently, no matter what, the experiment is behaving as if I'm moving outside the acceptable range and always executes the if statement.
Here is the code for it that I have right now:
from psychopy import core
start = mouse.isPressedIn(polygon, buttons=[0])
if start==True:
thisExp.addData('starttime',core.getTime())
x, y = mouse.getPos()
mouse.x.append(x)
mouse.y.append(y)
if y>10 or y<-10: #this is the statement that isn't resulting in what I would expect
thisExp.addData('penalty',1)
finish = mouse.isPressedIn(polygon2, buttons=[0])
if finish==True:
thisExp.addData('stoptime',core.getTime())
continueRoutine=False
I haven't figured out everything I need under the nested if statement yet. Right now I am just trying to make sure it works correctly. It never evaluates the if statement as true and never adds the penalty, even though looking at the collected mouse.y list data in the csv file I can see that there were instances where y was outside of whatever range I set.
It appears that mouse position data is being collected as soon as the trial begins, not just after polygon is clicked. However, starttime and stoptime do appear to be based on when polygon and polygon2 are clicked. I'm really not sure what is going on.
UPDATE:
I didn't get notified about Jonas' response until a day later for some reason. I wish I had because it would have put me on the right track a few hours earlier. It's a builder code component.
My code was all running each frame. At the beginning of the routine I added:
checkstart = False
That with the following code each frame solves this particular problem:
start = mouse.isPressedIn(polygon, buttons=[0])
if start==True:
thisExp.addData('starttime',core.getTime())
x, y = mouse.getPos()
mouse.x.append(x)
mouse.y.append(y)
checkstart=True;
if checkstart==True:
if y>10 or y <-10:
thisExp.addData('penalty',1)
finish = mouse.isPressedIn(polygon2, buttons=[0])
if finish==True:
thisExp.addData('stoptime',core.getTime())
continueRoutine=False
I still don't know why mouse position is collected before polygon is clicked but it doesn't actually matter to me. I can match the data for the frame where the mouse was clicked to the position that corresponds to the start button to get the beginning of the mouse path trace.
My code was all running each frame. At the beginning of the routine I added:
checkstart = False
That with the following code each frame solves this particular problem:
start = mouse.isPressedIn(polygon, buttons=[0])
if start==True:
thisExp.addData('starttime',core.getTime())
x, y = mouse.getPos()
mouse.x.append(x)
mouse.y.append(y)
checkstart=True;
if checkstart==True:
if y>10 or y <-10:
thisExp.addData('penalty',1)
finish = mouse.isPressedIn(polygon2, buttons=[0])
if finish==True:
thisExp.addData('stoptime',core.getTime())
continueRoutine=False
I still don't know why mouse position is collected before polygon is clicked but it doesn't actually matter to me. I can match the data for the frame where the mouse was clicked to the position that corresponds to the start button to get the beginning of the mouse path trace.