pygame pacman I want to know the meaning of the code - pygame

It's a function that got Ghost to follow player, but I don't understand.
I want to know the meaning of the code.
def followPlayer(g, dirs):
d = ghosts[g].dir
if d == 1 or d == 3:
if player.x > ghosts[g].x and dirs[0] == 1: ghosts[g].dir = 0
if player.x < ghosts[g].x and dirs[2] == 1: ghosts[g].dir = 2
if d == 0 or d == 2:
if player.y > ghosts[g].y and dirs[1] == 1 and not aboveCentre(ghosts[g]): ghosts[g].dir = 1
if player.y < ghosts[g].y and dirs[3] == 1: ghosts[g].dir = 3

The code determines the direction of movement of the ghost. The movement is encoded in ghosts[g].dir. 0 means right, 2 means left, 1 means down and 3 means up.
If the movement of the ghost is vertical (if d == 1 or d == 3), then it is evaluated if the movement direction has to be changed to a horizontal direction (0, 2).
If the movement of the ghost is horizontal (if d == 0 or d == 2:), then it is evaluated if the movement direction has to be changed to a vertical direction (1, 3).
If the movement to certain direction is allowed is stored in the list dirs.
For instance, the meaning of the following lines of code is:
if d == 1 or d == 3:
if player.x > ghosts[g].x and dirs[0] == 1: ghosts[g].dir = 0
if the movement is vertical (if d == 1 or d == 3:)
and the player is at the right of the ghost (player.x > ghosts[g].x)
and the movement to the right is allowed dirs[0] == 1
then change the direction of movement to the right ghosts[g].dir = 0

Related

how to call a function to another function in julia?

I am writing a code in julia but I am unable to call a function from another function. Code is:
function add(x, y)
if x == 3 && y ==1
z =0
else x == 0 && y ==0
z =1
end
return z
end
function width(a, b, c)
add(x,y)
.....
end
The variables in add function will be used in width function but as I am new to julia, I am unable to call add in the other function. Kindly guide.
Edit:
I tried declaring with the z but it also didn't worked
struct z
a::Int
b::Int
end
There are two problems in your code that are not related to Julia per se. First problem in the add function: if x == 3 && y == 1 the output should be z = 0, else if x == 0 && y == 0, actually the if was missing, the output should be z = 1. Now what will be the output if, e.g., x = 1 && y == 1? The answer is nothing and z will be undefined.
To fix the add function, you should add a default branch for the if-else.
function add(x, y)
if x == 3 && y == 1
z = 0
elseif x == 0 && y == 0
z = 1
else
z = -1 # some default
end
return z
end
The same function could be written more concisely as:
function add(x, y)
x == 3 && y == 1 && return 0
x == 0 && y == 0 && return 1
return -1 # some default
end
which can even be written in a one-liner like this:
add(x, y) = x == 3 && y == 1 ? 0 : x == 0 && y == 0 ? 1 : -1 # some default
The second problem is with the width function. x and y are not defined inside the body of the width function. So, you can't call add(x, y). It should be z = add(a, b) where z should be used in subsequent calculations. Finally, check what the third argument c is for, otherwise, remove it.
function width(a, b, c)
z = add(a, b)
.....
end

how I add a function to draw lines with the mouse on this pygame screen [duplicate]

This question already has answers here:
How do I add a rubber band for mouse dragging in PyGame? [closed]
(1 answer)
Why is my airbrush function not working in pygame?
(1 answer)
Closed 2 years ago.
SCREEN_WIDTH = 1500
SCREEN_HEIGHT = 750
background = pygame.image.load(r'C:\Users\ga-sa\Downloads\honeycomb.png')
background = pygame.transform.scale(BACKGROUND, (SCREEN_WIDTH, SCREEN_HEIGHT))
img1 = pygame.image.load(r"C:\Users\ga-sa\Downloads\As.png")
img2 = pygame.image.load(r"C:\Users\ga-sa\Downloads\AssetsXOR.png")
img3 = pygame.image.load(r"C:\Users\ga-sa\Downloads\AssetsNOT.png")
img4 = pygame.image.load(r"C:\Users\ga-sa\Downloads\AssetsAND.png")
images = [img1, img2, img3, img4]
current_image = -1
img_rects = [images[i].get_rect(topleft=(20 + 40 * i, 20)) for i in range(len(images))]
img_angles = [0 for _ in range(len(images))]
LeftButton = 0
while 1:
for e in pygame.event.get():
if e.type == QUIT:
pygame.quit()
exit(0)
if e.type == pygame.MOUSEBUTTONDOWN:
mouse_rect = pygame.Rect(e.pos, (1, 1))
current_image = mouse_rect.collidelist(img_rects)
if e.type == MOUSEMOTION:
if e.buttons[LeftButton]:
rel = e.rel
if 0 <= current_image < len(images):
img_rects[current_image].x += rel[0]
img_rects[current_image].y += rel[1]
keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT]:
img_angles[current_image] -= 1
if keys[pygame.K_LEFT]:
img_angles[current_image] += 1
screen.blit(background, (0,0))
for i in range(len(images)):
rotated_image = pygame.transform.rotate(images[i], img_angles[i])
rotated_rect = rotated_image.get_rect(center=img_rects[i].center)
screen.blit(rotated_image, rotated_rect)
pygame.display.flip()
i want add the function to draw lines with mouse but i dont know how , my last question was wrong , i was stupid ,I can draw lines with the mouse? , i need , i want to delete the last post

Piecewise Functions in R

I'm trying to plot a piecewise function in R that is equal to -3 if x < 0, 1/3 * x^3 if x is between 0 and 5 (inclusive), and 4x otherwise. The function I've been running seems to work, but the plot returns an error message. The code I wrote is:
g <- function(x) {
if (x < 0)
-3 # first component
else if (x >= 0 & x <= 5)
(1/3) * x #second component
else
4*x # third component
}
Then when I try to plot it with
plot(g, -20, 20)
I get
Error in curve(expr = x, from = from, to = to, xlim = xlim, ylab = ylab, :
'expr' did not evaluate to an object of length 'n'
In addition: Warning message:
In if (x < 0) -3 else if (x >= 0 & x <= 5) (1/3) * x else 4 * x :
the condition has length > 1 and only the first element will be used
I can't quite figure out how to troubleshoot this, and the 'ifelse' function returned a dark black line when I attempted to plot it, which I'd prefer to avoid, so this seems to be my best bet.
I would greatly appreciate any insights.

Calling a function multiple times with return value

from graphics import *
def draw():
returnStuff = {'again' : 0, '1st' : 1 }
draw.again = False
win = GraphWin("Quadrilateral Maker", 600, 600)
win.setBackground("yellow")
text = Text(Point(150, 15), 'Click 4 points to create a Quadrilateral')
text.draw(win)
#gets the 4 points
p1 = win.getMouse()
p1.draw(win)
p2 = win.getMouse()
p2.draw(win)
p3 = win.getMouse()
p3.draw(win)
p4 = win.getMouse()
p4.draw(win)
vertices = [p1, p2, p3, p4]
#draws the shape
quad = Polygon(vertices)
quad.setFill('red')
quad.setOutline('black')
quad.setWidth(3)
quad.draw(win)
text.setText('Click in the appropriate box.')
#Quit box
quitBox = Rectangle(Point(30, 500), Point(100,550))
quitBox.setFill('green')
quitBox.draw(win)
quitorNah = Text(Point(60, 490), 'Quit')
quitorNah.draw(win)
#again box
quitBox = Rectangle(Point(480, 500), Point(550,550))
quitBox.setFill('green')
quitBox.draw(win)
quitorNah = Text(Point(510, 490), 'Draw Again')
quitorNah.draw(win)
click = win.getMouse()
x = click.getX()
y = click.getY()
while True:
if 30 < x < 100 and 500 < y < 550:
returnStuff['again'] = 0
win.close()
break
elif 480 < x < 550 and 500 < y < 550:
returnStuff['again'] = 1
win.close()
break
return returnStuff
count = 1
returnValue = draw()
if returnValue['1st'] == 1:
count = 0
while count == 1 or returnValue['again'] == 1:
return_value = draw()
So I have this simple interactive program using Zelle graphics, it asks the user to click on 4 points in a window and from that it creates a shape. Then, the user is shown 2 boxes, one to quit and one to draw again. My draw again isn't working, and it has something to do with the return value. I am returning a dictionary, as I need access to 2 of the variables within the function. In the 'returnStuff' dictionary, I have a part called 'again', which is initially set to 0. If the user clicks in the run again box, it changes this value to 1, and then outside the function I have an if statement that should call the function again if that again value is 1. It does this properly the FIRST time, but the 2nd time around my program just stops all together, and I don't understand why.
Can anybody explain why this is happening?
I think you need a while...
while count==1 or returnValue['again'] == 1:
returnValue = draw()

Check if wind direction is within a specified range

I am representing wind directions using integer values (an Enum) ranging from 0 for North, through to 15 for North-North-West.
I need to check if a given wind direction (integer value between 0 and 15) is within a certain range. I specify my WindDirectionFrom value first moving clockwise to WindDirectionTo to specify the range of allowable wind direction.
Obviously if WindDirectionFrom=0 and WindDirectionTo=4 (between N and E direction) and the wind direction is NE (2) the calculation is simply
int currentWindDirection = 2;
bool inRange = (WindDirectionFrom <= currentWindDirection && currentWindDirection <= WindDirectionTo);
//(0 <= 2 && 2 <= 4) simple enough...
However for a different case where say WindDirectionFrom=15, WindDirectionTo=4 and wind direction is NE (2) again, the calculation immediately breaks...
bool inRange = (WindDirectionFrom <= currentWindDirection && currentWindDirection <= WindDirectionTo);
//(15 <= 2 && 2 <= 4) oops :(
I'm sure this can't be too difficult, but I'm having a real mental block with this one.
What you want is modular arithmetic. Do your arithmetic mod 16, and check to see if the difference is from (say) at least 14 (the modular equivalent of -2) or at most 2.
How to do modular arithmetic will vary between languages. With C or C++, you would find x mod 16 as follows:
int xm = x % 16;
if (xm < 0) xm += 16;
(Thanks to msw for pointing out that arithmetic on enums is frequently not allowed, and for good reasons. An enum normally represents objects or conditions that are discrete and not related arithmetically.)
I would do it like this:
int normedDirection( int direction, int norm )
{
return (NumberOfDirections + currentDirection - norm) % NumberOfDirections;
}
int normed = normedDirection( currentWindDirection, WindDirectionFrom );
bool inRange = (0 <= normed && normed <= normedDirection( WindDirectionTo, WindDirectionFrom );