You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -36,13 +38,17 @@ The snake is like an animation that either moves up, down, left, or right. How c
36
38
37
39
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:
38
40
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
+
```
41
45
42
46
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:
43
47
44
-
if direction == "right":
45
-
snake_x = snake_x + speed
48
+
```python
49
+
if direction =="right":
50
+
snake_x = snake_x + speed
51
+
```
46
52
47
53
You can also delete SECTION 2 from your code - there are not any mouse controls in this game.
48
54
@@ -62,10 +68,12 @@ Let's create a function for setting a new random target location. Creating a fun
62
68
63
69
In SECTION 1 put the following code:
64
70
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
+
defnew_target_location():
73
+
global target_x, target_y
74
+
target_x = random.randint(30, 610)
75
+
target_y = random.randint(30, 450)
76
+
```
69
77
70
78
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).
71
79
@@ -77,11 +85,13 @@ How can you tell when the snake has hit the target? Refer back to your paddle ga
77
85
78
86
Here is some example code that creates imaginary rectangles around two circles and checks if they are overlapping:
@@ -91,14 +101,16 @@ How do you know when the snake has hit the wall? You probably will want to do 4
91
101
92
102
Here is an example function that only checks for two walls (this number might have to be adjusted):
93
103
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
+
defsnake_hit_wall():
106
+
if snake_y <20:
107
+
returnTrue
108
+
if snake_x <20:
109
+
returnTrue
110
+
111
+
#if the function hasn't already returned True, it should return False
112
+
returnFalse
113
+
```
102
114
103
115
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.)
104
116
@@ -110,10 +122,11 @@ How can you change the snake's speed every time it hits the target? (Hint: this
110
122
111
123
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:
0 commit comments