这是一个使用Python写的过马路游戏。
可以练习一些面向对象的知识。
由于全写注释CSND说我字太少,就统一写代码外面来
实现的功能有:
可以改进的地方有:
import time
from turtle import Screen
from player import Player
from car_manager import CarManager
from scoreboard import ScoreBoard
screen = Screen()
screen.setup(width=600, height=600)
screen.tracer(0)
def cal_limits(screen):
u_limit = screen.window_height() / 2
d_limit = -screen.window_height() / 2
l_limit = -screen.window_width() / 2
r_limit = screen.window_width() / 2
return u_limit, d_limit, l_limit, r_limit
def quit_game():
global game_is_on
game_is_on = False
limits = cal_limits(screen)
player = Player()
car_manager = CarManager(limits=limits)
screen.listen()
screen.onkey(key='Up', fun=player.move)
screen.onkey(key='q', fun=quit_game)
scoreboard = ScoreBoard(limits)
game_is_on = True
while game_is_on:
time.sleep(0.11 - scoreboard.level * 0.015)
screen.update()
car_manager.add_car((limits[0] - 50, limits[1] + 100, limits[3], limits[3] + 300))
car_manager.move(limits)
if car_manager.collision_check(player):
scoreboard.game_clear('Game Over!!!')
player.respawn()
scoreboard.refresh_level()
if player.check_finish():
scoreboard.refresh_level()
STARTING_POSITION = (0, -280)
MOVE_DISTANCE = 10
FINISH_LINE_Y = 280
from turtle import Turtle
class Player(Turtle):
def __init__(self):
super().__init__()
self.penup()
self.shape('turtle')
self.goto(STARTING_POSITION)
self.setheading(90)
def move(self):
self.forward(MOVE_DISTANCE)
def respawn(self):
self.goto(STARTING_POSITION)
def check_finish(self):
if self.pos()[1] >= FINISH_LINE_Y:
self.respawn()
return True
COLORS = ["red", "orange", "yellow", "green", "blue", "purple"]
STARTING_MOVE_DISTANCE = 10
MOVE_INCREMENT = 10
REFRESH_RATE = 0.3
import random
from turtle import Turtle
class CarManager:
def __init__(self, limits):
self.cars = []
spawn_range = (limits[0] - 20, limits[1] + 100, limits[2], limits[3] + 300)
for i in range(25):
self.add_car(spawn_range)
def add_car(self, spawn_range):
if random.random() < REFRESH_RATE:
new_pos = (random.randint(spawn_range[2], spawn_range[3]) // 20) * 20, \
(random.randint(spawn_range[1], spawn_range[0]) // 20) * 20
while self.cars:
for old_car in self.cars:
if old_car.distance(new_pos) <= STARTING_MOVE_DISTANCE:
new_pos = (random.randint(spawn_range[2], spawn_range[3]) // 20) * 20, \
(random.randint(spawn_range[1], spawn_range[0]) // 20) * 20
break
else:
break
new_car = Turtle(shape='square')
new_car.shapesize(stretch_wid=1, stretch_len=2)
new_car.penup()
new_car.color(random.choice(COLORS))
new_car.setheading(180)
new_car.goto(new_pos)
self.cars.append(new_car)
def move(self, limits):
for car in self.cars:
car.forward(MOVE_INCREMENT)
if car.pos()[0] < limits[2] - 20:
car.ht()
self.cars.remove(car)
def collision_check(self, player):
player_up_y_block = (player.pos()[1] + 10) // 10
player_down_y_block = (player.pos()[1] - 10) // 10
for car in self.cars:
car_left_x_block = (car.pos()[0] - 20) // 10
car_right_x_block = (car.pos()[0] + 20) // 10
car_up_y_block = (car.pos()[1] + 10) // 10
car_down_y_block = (car.pos()[1] - 10) // 10
if car_left_x_block <= 0 <= car_right_x_block and \
(car_down_y_block < player_up_y_block < car_up_y_block or
car_down_y_block < player_down_y_block < car_up_y_block):
return True
FONT = ("Courier", 24, "normal")
MAX_LEVEL = 7
import time
from turtle import Turtle
class ScoreBoard(Turtle):
def __init__(self, limits):
super().__init__()
self.level = 1
self.penup()
self.ht()
self.goto(limits[2] + 20, limits[0] - 50)
self.write(f"Level:{self.level}", font=FONT)
def refresh_level(self):
if self.level >= MAX_LEVEL:
self.game_clear('Game Clear!!!')
self.clear()
self.level += 1
self.write(f"Level:{self.level}", font=FONT)
def game_clear(self, info):
self.level = 0
pos = self.pos()
self.goto(-100, 0)
self.write(info, font=FONT)
self.goto(pos)
time.sleep(5)