For some weird purpose I’ve determined I need to turn out to be fluent at writing easy video games utilizing Python Turtle Graphics. I am no newbie at coding, however I nonetheless have an extended strategy to go to get to the extent I need to get to, and would love some assist alongside the best way. I’ve posted this system under in Code Evaluate Stack Change however not but acquired the suggestions I would love. I believed perhaps of us right here may give me some pointers from the sport dev perspective.
For instance, was it loopy to have separate timers for the aircraft and the bomb? How nicely did I deal with when the sport resets after the aircraft crashes or all of the towers are destroyed? Is there a greater method to do that? On the whole, how can I enhance my implementation each by way of recreation dev ideas and precise coding?
There’s a repo containng the sounds in addition to this system right here:
https://github.com/Robin-Andrews/Alien-Blitz-Retro-Recreation-with-Python-Turtle-Graphics
Many thanks upfront for any suggestions.
# alien_blitz.py
import turtle
import random
strive:
import playsound # Not a part of commonplace Library.
SOUND = True
besides ImportError:
SOUND = False
NUM_TOWERS = 20
MAX_TOWER_HEIGHT = 10
CURSOR_SIZE = 20
PLANE_DELAY = 40
BOMB_DELAY = 40
WIDTH = 800
HEIGHT = 600
cell_colors = ["black", "dark green", "brown"]
def move_plane():
international taking part in
new_pos = (aircraft.xcor(), aircraft.ycor())
if new_pos[0] > width // 2:
aircraft.goto(- width // 2, aircraft.ycor() - measurement)
else:
aircraft.goto(aircraft.xcor() + 12, aircraft.ycor())
if check_plane_tower_collision():
taking part in = False
restart(new_level=False)
elif check_player_wins_level():
restart(new_level=True)
else:
display.replace()
turtle.ontimer(move_plane, PLANE_DELAY)
def check_player_wins_level():
if rating >= winning_score:
player_wins_level()
return True
return False
def player_wins_level():
update_score_display()
if SOUND:
playsound.playsound("victory.wav")
def check_plane_tower_collision():
for tower in towers:
for cell in tower:
if aircraft.distance(cell) <= measurement / 2 + 10: # Half cell measurement + half aircraft top
plane_tower_collision()
return True
return False
def plane_tower_collision():
bomb.hideturtle() # If current when aircraft crashes
aircraft.shade("pink")
display.replace()
if SOUND:
playsound.playsound("plane_crash.wav")
def check_bomb_tower_collision():
if taking part in:
for tower in towers:
for cell in tower:
if bomb.distance(cell) <= measurement / 2 + 5: # Half cell measurement + half bomb measurement
bomb_tower_collision(cell)
return True
return False
def bomb_tower_collision(cell):
international rating, high_score
if SOUND:
playsound.playsound("bombed.wav", False)
cell.setx(-1000)
cell.clear()
rating += 10
if rating > high_score:
high_score = rating
update_score_display()
def start_bomb_drop():
# Stop additional key presses till drop is completed tp forestall occasion stacking.
display.onkey(None, "house")
bomb.goto(aircraft.xcor(), aircraft.ycor())
bomb.showturtle()
__continue_bomb_drop()
def __continue_bomb_drop():
international taking part in
bomb.goto(bomb.xcor(), bomb.ycor() - 12)
if check_bomb_tower_collision() or bomb.ycor() < - top // 2 or not taking part in:
stop_bomb_drop()
else:
turtle.ontimer(__continue_bomb_drop, BOMB_DELAY)
def stop_bomb_drop():
bomb.hideturtle()
# It is now secure to permit one other bomb drop, so rebind keyboard occasion.
display.onkey(start_bomb_drop, "house")
def update_score_display():
pen.clear()
pen.write("Rating:{:2} Excessive Rating:{:2}".format(rating, high_score), align="heart", font=("Courier", 24, "regular"))
def get_towers():
end result = []
for col in vary(-NUM_TOWERS // 2, NUM_TOWERS // 2):
tower = []
for degree in vary(random.randrange(1, MAX_TOWER_HEIGHT + 1)):
block = turtle.Turtle(form="sq.")
block.shapesize(measurement / CURSOR_SIZE)
block.shade(random.selection(cell_colors))
block.penup()
block.goto(col * measurement + offset, - top // 2 + degree * measurement + offset)
tower.append(block)
end result.append(tower)
return end result
def setup():
international display, aircraft, bomb, pen, high_score, measurement, offset, top, width, rating
# Display
display = turtle.Display()
display.title("Alien Blitz")
display.setup(WIDTH, HEIGHT)
display.bgcolor("darkish blue")
display.pay attention()
display.onkey(start_bomb_drop, "house")
display.tracer(0)
# MISC.
width = display.window_width() - 50
top = display.window_height() - 50
measurement = width / NUM_TOWERS # Measurement of tower cells in pixels
offset = (NUM_TOWERS % 2) * measurement / 2 + measurement / 2 # Heart even and odd cells
# Airplane
aircraft = turtle.Turtle(form="triangle", seen=False)
aircraft.shade("yellow")
aircraft.shapesize(20 / CURSOR_SIZE, 40 / CURSOR_SIZE)
aircraft.penup()
aircraft.goto(- width // 2, top // 2)
aircraft.showturtle()
# Bomb
bomb = turtle.Turtle(form="circle")
bomb.hideturtle()
bomb.shade("pink")
bomb.shapesize(0.5)
bomb.penup()
# Rating Show
pen = turtle.Turtle()
pen.hideturtle()
pen.shade("white")
pen.penup()
pen.goto(0, 260)
# Initialise excessive rating
high_score = 0
def restart(new_level=False):
international rating, high_score, winning_score, towers, taking part in
# Towers record doesn't exist on first name.
strive:
for tower in towers:
for cell in tower:
cell.setx(-1000)
cell.clear()
besides NameError:
go
aircraft.shade("yellow")
towers = get_towers()
# Right here we deal with the rating for various eventualities for restarting the sport - crashed aircraft or accomplished degree.
if not new_level:
rating = 0
winning_score = sum(len(row) for row in towers) * 10
else:
winning_score += sum(len(row) for row in towers) * 10
update_score_display()
aircraft.goto(- width // 2, top // 2)
bomb.goto(- width // 2, top // 2)
taking part in = True
display.replace()
move_plane()
def fundamental():
setup()
restart()
if __name__ == "__main__":
fundamental()
turtle.executed()
```