Skip to content

Commit ce15744

Browse files
committed
style code correctly
Styled code using fenced code blocks and syntax highlighting.
1 parent 988e5bf commit ce15744

File tree

1 file changed

+39
-26
lines changed

1 file changed

+39
-26
lines changed

Day-7/README.md

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ How can you draw the snake on the screen? Look back at your previous projects fo
2626

2727
Here is an example line of code that draws a circle using variables:
2828

29-
pygame.draw.circle(screen, circl_color, [circle_x, circle_y], circle_size)
29+
```python
30+
pygame.draw.circle(screen, circl_color, [circle_x, circle_y], circle_size)
31+
```
3032

3133
[Solution](finished/snake part 1.py)
3234

@@ -36,13 +38,17 @@ The snake is like an animation that either moves up, down, left, or right. How c
3638

3739
How can you set the direction variable when the arrow keys are hit? (Hint: this will go in SECTION 3 of the code.) Here is an example line of code from the paddle game:
3840

39-
if event.key == pygame.K_LEFT:
40-
paddle_x = paddle_x - paddle_speed
41+
```python
42+
if event.key == pygame.K_LEFT:
43+
paddle_x = paddle_x - paddle_speed
44+
```
4145

4246
How can you change the snake's position variables based on your direction variable? (Hint: this will go in SECTION 4 of the code.) You will probably need 4 ifs like this:
4347

44-
if direction == "right":
45-
snake_x = snake_x + speed
48+
```python
49+
if direction == "right":
50+
snake_x = snake_x + speed
51+
```
4652
4753
You can also delete SECTION 2 from your code - there are not any mouse controls in this game.
4854

@@ -62,10 +68,12 @@ Let's create a function for setting a new random target location. Creating a fun
6268

6369
In SECTION 1 put the following code:
6470

65-
def new_target_location():
66-
global target_x, target_y
67-
target_x = random.randint(30, 610)
68-
target_y = random.randint(30, 450)
71+
```python
72+
def new_target_location():
73+
global target_x, target_y
74+
target_x = random.randint(30, 610)
75+
target_y = random.randint(30, 450)
76+
```
6977

7078
Any time we want to change one of the game's variables inside of a function, the very first line must tell it which variables will be changed by using the "global" keyword. Otherwise this function would have created a new variable called target_x and target_y that would only exist within the function (not modifying the variable in the game loop).
7179

@@ -77,11 +85,13 @@ How can you tell when the snake has hit the target? Refer back to your paddle ga
7785

7886
Here is some example code that creates imaginary rectangles around two circles and checks if they are overlapping:
7987

80-
circle1_rect = pygame.Rect(circle1_x-circle1_size, circle1_y-circle1_size, circle1_size*2,circle1_size*2)
81-
circle2_rect = pygame.Rect(circle2_x-circle2_size, circle2_y-circle2_size, circle2_size*2,circle2_size*2)
82-
#see if the rectangles overlap
83-
if doRectsOverlap(circle1_rect, circle2_rect):
84-
print "YES"
88+
```python
89+
circle1_rect = pygame.Rect(circle1_x-circle1_size, circle1_y-circle1_size, circle1_size*2,circle1_size*2)
90+
circle2_rect = pygame.Rect(circle2_x-circle2_size, circle2_y-circle2_size, circle2_size*2,circle2_size*2)
91+
#see if the rectangles overlap
92+
if doRectsOverlap(circle1_rect, circle2_rect):
93+
print "YES"
94+
```
8595

8696
[Solution](finished/snake part 5.py)
8797

@@ -91,14 +101,16 @@ How do you know when the snake has hit the wall? You probably will want to do 4
91101

92102
Here is an example function that only checks for two walls (this number might have to be adjusted):
93103

94-
def snake_hit_wall():
95-
if snake_y < 20:
96-
return True
97-
if snake_x < 20:
98-
return True
99-
100-
#if the function hasn't already returned True, it should return False
101-
return False
104+
```python
105+
def snake_hit_wall():
106+
if snake_y < 20:
107+
return True
108+
if snake_x < 20:
109+
return True
110+
111+
#if the function hasn't already returned True, it should return False
112+
return False
113+
```
102114

103115
How can you use this function to quit the game when the snake hits the wall? (Hint: this will go in SECTION 4 of the code.)
104116

@@ -110,10 +122,11 @@ How can you change the snake's speed every time it hits the target? (Hint: this
110122

111123
Can you also create a score variable and display it as a label? (Hint: this will go in SECTIONs 1, 4, and 5 of the code.) Here is some example code for creating a label that displays a variable called score:
112124

113-
myfont = pygame.font.SysFont("Arial", 22)
114-
score_label = myfont.render(str(score), 1, pygame.color.THECOLORS['white'])
115-
screen.blit(score_label, (5, 10))
116-
125+
```python
126+
myfont = pygame.font.SysFont("Arial", 22)
127+
score_label = myfont.render(str(score), 1, pygame.color.THECOLORS['white'])
128+
screen.blit(score_label, (5, 10))
129+
```
117130

118131
[Solution](finished/snake part 7.py)
119132

0 commit comments

Comments
 (0)