Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

hello, I am making a snake game using python and the turtle module to create it. The game works perfectly but I am trying to

hello, I am making a snake game using python and the turtle module to create it. The game works perfectly but I am trying to add a splash screen in the game but the problem is that the characters in the back are shown threw the splash screen and also it does not fit 100% in the screen it cuts of on top and bottom a bit that's because I used google slides to modify my picture and add extra things on it then converts the picture to "gif" but then when I put it in the code it just cuts off the screen is 600 x 600 pixels. So all I want is that to help me figure out how to make the characters invisible when the splash screen comes and I want the splash screen to fit the screen perfectly.

pics:

the characters are the red and square

image text in transcribed

code:

import turtle import time import random

# constants delay = 0.1 level = 1 score = 0 high_score = 0 game_state = "splash"

# Set up the screen window = turtle.Screen() window.title("Snake Game by @TokyoEdTech") window.bgcolor("green") window.setup(width=600, height=600) window.tracer(0) # Turns off the screen updates

# Snake head/apple head = turtle.Turtle() head.speed(0) head.shape("square") head.color("black") head.penup() head.goto(0,0) head.direction = "stop"

# Snake food food = turtle.Turtle() food.speed(0) food.shape("circle") food.color("red") food.penup() food.goto(0,100)

segments = []

# Pen pen = turtle.Turtle() pen.speed(0) pen.shape("square") pen.color("white") pen.penup() pen.hideturtle() pen.goto(0, 260) pen.write("Score: 0 High Score: 0 Level: 1", align="center", font=("Courier", 16, "normal"))

# Functions def go_up(): if head.direction != "down": head.direction = "up"

def go_down(): if head.direction != "up": head.direction = "down"

def go_left(): if head.direction != "right": head.direction = "left"

def go_right(): if head.direction != "left": head.direction = "right"

def move(): if head.direction == "up": y = head.ycor() head.sety(y + 20)

if head.direction == "down": y = head.ycor() head.sety(y - 20)

if head.direction == "left": x = head.xcor() head.setx(x - 20)

if head.direction == "right": x = head.xcor() head.setx(x + 20)

# Keyboard bindings window.listen() window.onkeypress(go_up, "w") window.onkeypress(go_down, "s") window.onkeypress(go_left, "a") window.onkeypress(go_right, "d")

# Main game loop while True: window.update()

# Check for a collision with the border if head.xcor()>290 or head.xcor()290 or head.ycor()

# Hide the segments for segment in segments: segment.goto(1000, 1000) # Clear the segments list segments.clear()

# Reset the score score = 0

# Reset the delay delay = 0.1 # Reset level level = 1

pen.clear() pen.write("Score: {} High Score: {} Level: {}".format(score, high_score, level), align="center", font=("Courier", 16, "normal"))

# Check for a collision with the food if head.distance(food)

# Add a segment new_segment = turtle.Turtle() new_segment.speed(0) new_segment.shape("square") new_segment.color("grey") new_segment.penup() segments.append(new_segment)

# Shorten the delay delay -= 0.001

# Increase the score score += 10

if score > high_score: high_score = score pen.clear() pen.write("Score: {} High Score: {} Level: {}".format(score, high_score, level), align="center", font=("Courier", 16, "normal"))

# Move the end segments first in reverse order for index in range(len(segments)-1, 0, -1): x = segments[index-1].xcor() y = segments[index-1].ycor() segments[index].goto(x, y)

# Move segment 0 to where the head is if len(segments) > 0: x = head.xcor() y = head.ycor() segments[0].goto(x,y)

move()

# Check for head collision with the body segments for segment in segments: if segment.distance(head)

# Reset the score score = 0

# Reset the delay delay = 0.1 # Reset the level level = 1 # Update the score display pen.clear() pen.write("Score: {} High Score: {} Level: {}".format(score, high_score, level), align="center", font=("Courier", 16, "normal"))

# Levels if level == 1 and score == 50: level += 1 delay *= 0.9 if level == 2 and score == 100: level += 1 delay *= 0.9 if level == 3 and score == 150: level += 1 delay *= 0.9

if game_state == "splash": window.bgpic("snake-2.gif")

time.sleep(delay)

windpw.mainloop()

image:

image text in transcribed

Snake Game by @TokyoEdTech Score: 0 High Score: 0 Level: 1 SNAKE GAME BY:@Ahmad's STUDIO Press S to start inc SNAKE GAME BY:@Ahmad's STUDIO Press S to start

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Business Process Driven Database Design With Oracle PL SQL

Authors: Rajeev Kaula

1st Edition

1795532386, 978-1795532389

More Books

Students also viewed these Databases questions

Question

=+What is the nature of the plant or site-level role of unions?

Answered: 1 week ago