Python 3 Pygame Drawing maze - pygame

I'm Trying to create this maze outline by drawing lines in python with pygame, but the format is messed up and i'm not sure what is happening. I believe it is probably an issue with the logic I created for drawing the lines based on what character is active in the for loop.
import pygame
pygame.init()
global black, white
white = (255,255,255)
black = (0,0,0)
display_width = 1200
display_height = 800
gameDisplay = pygame.display.set_mode((display_width,display_height))
clock = pygame.time.Clock()
def draw_line(surface,color,start_pos,end_pos,width):
pygame.draw.line(surface,color,start_pos,end_pos,width)
def game_loop():
maze = "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+n\
| | | | | | |n\
+ +--+ + + +--+ + + + +--+--+ + +--+ +n\
| | | | | | | | | |n\
+ +--+ + + +--+--+ + +--+--+--+--+ + + +n\
| | | | | | | | | | |n\
+--+ +--+--+--+ + +--+ + +--+--+--+--+ + +n\
| | | | | |n\
+ + + +--+--+--+--+--+--+--+--+--+--+--+ +--+n\
| | | | | | |n\
+ +--+ + +--+ + +--+--+--+--+--+--+ + + +n\
| | | | | | | | |n\
+ +--+--+ +--+--+--+ +--+ + + +--+ + + +n\
| | | | | | | | |n\
+--+--+ +--+--+--+ + + +--+ +--+--+--+--+ +n\
| | | |n\
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+"
gameExit = False
WALL_LENGTH = 20
x=y=x2=y2=START=5
gameDisplay.fill(white)
for i in maze:
if i == "+" or i == "-":
x2+=WALL_LENGTH
draw_line(gameDisplay,black,(x,y),(x2,y2),5)
x+=WALL_LENGTH
elif i == "n":
y+=WALL_LENGTH*2
y2=y
x=START
x2=START
elif i == "|":
y2+=WALL_LENGTH*2
draw_line(gameDisplay,black,(x,y),(x2,y2),5)
x+=WALL_LENGTH
x2=x
y2=y
elif i == " ":
x+=WALL_LENGTH
x2=x
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT: gameExit = True
pygame.display.update()
clock.tick(30)
game_loop()
pygame.quit()
quit()

The easiest way to debug this is to simply put a couple of print statements in your code and have a look at the x,y,x2,y2 variables.
I noticed the following problems:
The backslash at the end of a line tells python the string continues at the beginning of the next line. This means all the whitespace you have there to indent the maze is counted. By the time the code sees the first "|" or "+", the x counter is already at 245, shifting the whole line to the right. Remove the whitespace at the beginning of each maze line to fix this.
You count up your y value every time you have a new line. However, vertical lines only connect two horizontal lines, they're not really maze lines on their own. So you need to do either of two things:
a. Go down at the end of every even row and draw your walls downwards
b. Go down at the end of every odd row and draw your walls upwards (as I've done below)
Lastly, I'm not quite sure what your "+" are supposed to be. You currently use them to denote vertical lines, horizontal lines, corners, line ends, as well as intersections. You need to figure out different ASCII symbols for these cases and replace your "+" by the appropriate symbol and then adjust your code to draw whatever is required. Otherwise you end up drawing horizontal lines when you're supposed to draw vertical ones or vice versa.
Here is a somewhat fixed version of your code. It still does not properly deal with the "+", but I've made it draw the problematic lines in red so it should be easy to figure out the correct behavior.
import pygame
pygame.init()
global black, white
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
display_width = 1200
display_height = 800
gameDisplay = pygame.display.set_mode((display_width,display_height))
clock = pygame.time.Clock()
def draw_line(surface,color,start_pos,end_pos,width):
pygame.draw.line(surface,color,start_pos,end_pos,width)
def game_loop():
maze = "\
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+n\
| | | | | | |n\
+ +--+ + + +--+ + + + +--+--+ + +--+ +n\
| | | | | | | | | |n\
+ +--+ + + +--+--+ + +--+--+--+--+ + + +n\
| | | | | | | | | | |n\
+--+ +--+--+--+ + +--+ + +--+--+--+--+ + +n\
| | | | | |n\
+ + + +--+--+--+--+--+--+--+--+--+--+--+ +--+n\
| | | | | | |n\
+ +--+ + +--+ + +--+--+--+--+--+--+ + + +n\
| | | | | | | | |n\
+ +--+--+ +--+--+--+ +--+ + + +--+ + + +n\
| | | | | | | | |n\
+--+--+ +--+--+--+ + + +--+ +--+--+--+--+ +n\
| | | |n\
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+"
gameExit = False
WALL_LENGTH = 20
x=y=x2=y2=START=5
gameDisplay.fill(white)
linecount = 0
for i in maze:
if i == "-":
x2+=WALL_LENGTH
draw_line(gameDisplay,black,(x,y),(x2,y2),5)
x+=WALL_LENGTH
elif i == "+":
x2+=WALL_LENGTH
draw_line(gameDisplay,red,(x,y),(x2,y2),5)
x+=WALL_LENGTH
elif i == "n":
linecount+=1
if(linecount % 2 == 0):
y+=WALL_LENGTH*2
y2=y
x=START
x2=START
elif i == "|":
y2-=WALL_LENGTH*2
draw_line(gameDisplay,black,(x,y),(x2,y2),5)
x+=WALL_LENGTH
x2=x
y2=y
elif i == " ":
x+=WALL_LENGTH
x2=x
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT: gameExit = True
pygame.display.update()
clock.tick(30)
game_loop()
pygame.quit()
quit()
Screenshot of the current maze output:

Related

Script to search for multiple unread labels in Gmail threads at once

I have the following structure of labels
+------------+------------+---------------+
| label | sub-label | sub-sub-label |
+------------+------------+---------------+
| 01-fruit | | |
| | 01-apples | |
| | | green |
| | | red |
| | 02-oranges | |
| | | red |
| | | orange |
| 02-veggies | | |
| | 01-peppers | |
| | | green |
| | | red |
+------------+------------+---------------+
The script in use is:
function mail2Sheets() {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName('newRec'); //get the sheet
var freshLabel = GmailApp.getUserLabelByName("00-fresh"); // in the end, add this label
const query = "label:unread" + " label:01-fruit";
var foundThreads = GmailApp.search(query);
var newReceipts = [];
for (var i = 0; i < foundThreads.length; i++) {
+++++++ SOME CODE HERE +++++++
}
}
if(!foundThreads.length) return; // if there are no unread ones, do nothing.
sheet.getRange(SpreadsheetApp.getActiveSheet().getLastRow()+1,2,newReceipts.length,newReceipts[0].length).setValues(newReceipts); //write to sheet
GmailApp.markThreadsRead(foundThreads); // mark "foundThreads" as read
freshLabel.addToThreads(foundThreads); // add label "00-fresh" to "foundThreads"
GmailApp.refreshThreads(foundThreads); // refresh "foundThreads" for changes to show
}
I can successfully search for a single label like: const query = "label:unread" + " label:01-fruit";
Also.
Although I have GmailApp.refreshThreads(foundThreads); the Execution never completes.
Instead it shows Status Running
TO RECAP
How can I make the query search at the same time for multiple labels like
"label:unread" + " label:00-fruit/01-apples/red"
AND
"label:unread" + " label:02-veggies/01-peppers/red"
Also. How can the Status Running issue be fixed?
" " which is a space is used as AND operator.
OR and {} can be used as OR operator.
Using above operators, your goal can be achieved.
When you want to search the mails with label:unread and label:00-fruit/01-apples/red, please use the search query as follows.
label:unread label:00-fruit/01-apples/red
When you want to search the mails with label:unread and label:00-fruit/01-apples/red or label:02-veggies/01-peppers/red, please use the search query as follows.
label:unread (label:00-fruit/01-apples/red OR label:02-veggies/01-peppers/red)
or
label:unread {label:00-fruit/01-apples/red label:02-veggies/01-peppers/red}
Reference:
Search operators you can use with Gmail

Bold Specific Values in Expression in SSRS

I'm trying to make multiple values "Bold" within an expression when the value contains "x". Html Placeholder properties doesn't meet my need and I'm struggling to get it working with the below as there are multiple statements in my expression:
=(IIF(Fields!Test1.Value="x", "Bold", "Normal") OR (Fields!Test2.Value="x", "Bold", "Normal") etc etc
I think I need to create a custom code function then call the function where needed in the expression but I haven't a clue where to start! Any help would be greatly appreciated.
Update:
Switch is working but bolding whole expression not just values specified within Placeholder Properties of expression. I believe this is because my main expression has concatenated fields creating one long string.
Placeholder exp
Result
Value exp
+-----------+------------+-----------+------------------+------------------+-----------+-----------+------------+-----------+-------------------+---------+--------------+-----------+----------------+-------------------------------+-----------------+---------------+--------------+----------+------------------+------------------+------------------+------------------+------------+---+------------+-----------+-------------------+-----------+--------------+-----------+----------------+---------+----------+-------------------+----------------------+---------------+----------------+---------+
| id_number | first_name | last_name | pref_address_ind | h_addr_type_code | h_care_of | h_street1 | h_street2 | h_street3 | h_foreign_cityzip | h_city | h_state_code | h_zipcode | h_country_code | h_email_address | p_email_address | h_phone | | hc_phone | b_company_name_1 | b_company_name_2 | b_business_title | fld_of_work_code | b_street1 | | b_street2 | b_street3 | b_foreign_cityzip | b_city | b_state_code | b_zipcode | b_country_code | b_phone | bc_phone | b_email_address | | full_business | pref_email_ind | Main_ID |
+-----------+------------+-----------+------------------+------------------+-----------+-----------+------------+-----------+-------------------+---------+--------------+-----------+----------------+-------------------------------+-----------------+---------------+--------------+----------+------------------+------------------+------------------+------------------+------------+---+------------+-----------+-------------------+-----------+--------------+-----------+----------------+---------+----------+-------------------+----------------------+---------------+----------------+---------+
| 165815 | Test | Test1 | NULL | | NULL | NULL | x Apt #09 | NULL | NULL | NULL | NULL | NULL | x USA | NULL | NULL | DELETED | | DELETED | NULL | ~ | NULL | NULL | NULL | | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | | NULL | NULL | 165815 |
| 165816 | Test | Test2 | NULL | | NULL | Street | x Street 1 | x Street2 | NULL | Houston | NULL | NULL | NULL | x Home Email:testing#test.com | NULL | x Home Phone: | 111-111-1111 | NULL | NULL | ~ | NULL | NULL | x Business | 1 | x Street 2 | NULL | NULL | x Houston | x TX | x 77777 | NULL | NULL | NULL | x Business Email: | btesting#testing.com | NULL | NULL | 165816 |
+-----------+------------+-----------+------------------+------------------+-----------+-----------+------------+-----------+-------------------+---------+--------------+-----------+----------------+-------------------------------+-----------------+---------------+--------------+----------+------------------+------------------+------------------+------------------+------------+---+------------+-----------+-------------------+-----------+--------------+-----------+----------------+---------+----------+-------------------+----------------------+---------------+----------------+---------+
There should be no need for custom code although depending on how complex the rules are you may want to consider SWITCH.
Based on your simple example you could do either of these
=IIF(
Fields!Test1.Value = "x" OR Fields!Test2.Value = "x",
"Bold",
Nothing)
or for a more complex situation
= SWITCH (
Fields!Test1l.Value = "x" OR Fields!Test2.Value="x", "Bold",
Feilds!Test3.Value = "Z" AND Fields!Test4.Value >10, "SemiBold",
True, Nothing)
SWITCH Evaluates each expression/result pair and stops when it hits the first that evalutes to True. The final 'True, Nothing' acts like an else. SWITCH if easier to read than nested IIFs but IIF can be simpler if the rules are simple.
UPDATE:
The following shows doing this using HTML generated in the dataset query. I know next to nothing about HTML this this may be fixable but here's where I got to ....
Based on your sample table I created a new column with a HTML version of the address using </p> to create the line breaks.
It also suppresses blank rows.
The sql looks something like
SELECT
HomeAddrFormattedP1 = '<p>Home Address: </p>'
+ IIF( ISNULL(h_street1,'')='', '', IIF( LEFT(h_street1,1) = 'x', '<b>' + h_street1 + '</b></p>', h_street1 + '</p>') )
+ IIF( ISNULL(h_street2,'')='', '', IIF( LEFT(h_street2,1) = 'x', '<b>' + h_street2 + '</b></p>', h_street2 + '</p>') )
+ IIF( ISNULL(h_street3,'')='', '', IIF( LEFT(h_street3,1) = 'x', '<b>' + h_street3 + '</b></p>', h_street3 + '</p>') )
+ IIF( ISNULL(h_foreign_cityzip,'')='', '', IIF( LEFT(h_foreign_cityzip,1) = 'x', '<b>' + h_foreign_cityzip + '</b></p>', h_foreign_cityzip + '</p>') )
+ IIF( ISNULL(h_city,'')='', '', IIF( LEFT(h_city,1) = 'x', '<b>' + h_city + '</b></p>', h_city + '</p>') )
+ IIF( ISNULL(h_state_code,'')='', '', IIF( LEFT(h_state_code,1) = 'x', '<b>' + h_state_code + '</b></p>', h_state_code + '</p>') )
+ IIF( ISNULL(h_zipcode,'')='', '', IIF( LEFT(h_zipcode,1) = 'x', '<b>' + h_zipcode + '</b></p>', h_zipcode + '</p>') )
+ IIF( ISNULL(h_country_code,'')='', '', IIF( LEFT(h_country_code,1) = 'x', '<b>' + h_country_code + '</b></p>', h_country_code + '</p>') )
,
*
FROM myTable
Using this as my report's dataset query I then added a simple table with 3 columns one for HomeAddrFormattedP1, first_name and last_name
All I did then was right-click the HomeAddrFormattedP1 placeholder and set its properties to Markup Type = HTML
This gave the following final result.

mysql regular replace characters with empty char

I have the following strings in the table:
step1.cards.choice_step_1
step1.cards.choice_step_2
step2.cards.choice_step_1
step2.cards.choice_step_2
I would replace with empty characters all in these string using that pattern:
^step([0-9]|[1-9][0-9]).cards.choice_step_1$
^step([0-9]|[1-9][0-9]).cards.choice_step_2$
Does it possible to do that in MySQL?
Finally I would have in result:
1
1
2
2
EDIT
select regex_replace('[^0-9]','','step1.cards.choice_step_1');
return me
11
please help on correct pattern on the following data :
sometext.step1
sometext.step1.cards.choice_step_1
sometext.step1.cards.choice_step_2
sometext.step1.desire
sometext.step2
sometext.step2.cards.choice_step_1
sometext.step2.cards.choice_step_2
sometext.step2.desire
to get the following result:
step1
step1
step1
step1
step2
step2
step2
step2
You could try something like this:
create table test (field1 varchar(100));
select * from test;
+------------------------------------+
| field1 |
+------------------------------------+
| sometext.step1 |
| sometext.step1.cards.choice_step_1 |
| sometext.step1.cards.choice_step_2 |
| sometext.step1.desire |
| sometext.step2 |
| sometext.step2.cards.choice_step_1 |
| sometext.step2.cards.choice_step_2 |
| sometext.step2.desire |
| step1 |
+------------------------------------+
Query:
select
field1,
-- find position of step
position('step' in field1) as step,
-- find position of dot AFTER step is found
position('.' in
substr(field1, position('step' in field1), length(field1)))
+ position('step' in field1) as dot,
-- if position of 'step' --and-- position of 'step' + position of 'dot' is 1
-- that means: step is at position 1 and dot is not found, display field as is
-- if position of 'step' --and-- position of 'step' + position of 'dot' are equal
-- that means: dot is not found. Grab everything from step onwards
-- if position of 'step' --and-- position of 'step' + position of 'dot' are **NOT** equal
-- grab everything from where step was found thruogh just before dot was found
case
when position('step' in field1) = 1
and
position('.' in substr(field1, position('step' in field1), length(field1)))
+ position('step' in field1) = 1
then
field1
when position('step' in field1) =
position('.' in substr(field1, position('step' in field1), length(field1)))
+ position('step' in field1)
then
substr(field1, position('step' in field1), length(field1))
else
substr(field1,
position('step' in field1),
position('.' in substr(field1, position('step' in field1), length(field1)))-1
)
end as ans
from test;
Result:
+------------------------------------+------+------+-------+
| field1 | step | dot | ans |
+------------------------------------+------+------+-------+
| sometext.step1 | 10 | 10 | step1 |
| sometext.step1.cards.choice_step_1 | 10 | 16 | step1 |
| sometext.step1.cards.choice_step_2 | 10 | 16 | step1 |
| sometext.step1.desire | 10 | 16 | step1 |
| sometext.step2 | 10 | 10 | step2 |
| sometext.step2.cards.choice_step_1 | 10 | 16 | step2 |
| sometext.step2.cards.choice_step_2 | 10 | 16 | step2 |
| sometext.step2.desire | 10 | 16 | step2 |
| step1 | 1 | 1 | step1 |
+------------------------------------+------+------+-------+
Notice the last field that has the data you desire. Please feel free to tweak this to your needs.

Grouping Instances/ Symbols Together

so I am working on a game where if you touch one side of a box, you'll be propelled in that direction.
* For example: Let's say I hit the left side of the box, I should be propelled to the left.
One way I could do this, is split the box into 4 instances where each instance is on the left, right, top & bottom sides.
Is there a way for me to embed instances/ access parts of an instance, etc.
OR if there is a better way to do this can you tell me how?
I guess the answer to your question is yes; what you want to do is have a single container MovieClip with 4 internal MovieClips that will be your hit targets.
I would go for the following setup; i.e. use triangles.
------------
|\ top /|
| \ / |
| \ / |
| \ / r|
|left\/ i|
| /\ g|
| / \ h|
| / \ t|
| / \ |
|/ bottom \|
------------
if you were to do it based on coordinates, as Adam Harte mentioned, handle it like a 9-slice grid. if you imagine the centre box oversized, that will give you maximum hit areas.
-----------------
| | | |
| | T | |
-----------------
| | | |
| L | | R |
| | | |
-----------------
| | B | |
| | | |
-----------------
You could just test the x and y position of the touch inside the the box when they touch it. Something like this pseudocode:
function onTouchDown(){
if(touchX < halfBoxWidth)
{
// We touched the left side, so go left.
}
else
{
// We touched the right side, so go right.
}
}

Crop With Padding

I need to crop an image specifying coordinates that may exceed the image's bounds. If the coordinates are off, appropriate padding is applied.
Normally:
+===============+
| Source Bitmap |
| +-------+ |
| + Crop + |
| +-------+ |
| |
+===============+
...which works perfectly well with WriteableBitmapEx's Crop() extension. But in my case:
+-----------+
+ Crop +
+ +
+ +===============+
+ | Source Bitmap |
+ | |
+ +===============+
+ +
+-----------+
In this case, the bounds exceed the top, left, and bottom. The resulting bitmap need to be:
+-----------+
+ +
+ +
+ +=========+
+ | +
+ | +
+ +=========+
+ +
+-----------+
What's the best (and fastest) way to accomplish this?
The easiest would be to create a new WB with the dimension of the final result, then use the Blit() method to copy the region of the source to your new destination bitmap's region.
Nice ASCII art job btw. :)