记录一下我的arcade小车
# ver 1.1:
# 加入了UP控制速度的部分
# ver 1.2:
# 加入了武器系统
# ver 1.2.1:
# 修复了武器只能开一枪的问题
from arcade import *
# 设置窗体高度
SCREEN_WIDTH = 600
SCREEN_HEIGHT = 800
# 设置窗体标题
SCREEN_TITLE = "game"
class Road(Sprite):
def __init__(self,image):
super().__init__(image)
self.center_x = SCREEN_WIDTH//2
self.center_y = SCREEN_HEIGHT//2
self.change_y = -4
def update(self):
super().update()
if self.center_y <= SCREEN_HEIGHT//2 - SCREEN_HEIGHT:
self.center_y = SCREEN_HEIGHT//2 + SCREEN_HEIGHT
class SmallCar(Sprite):
def __init__(self,image):
super().__init__(image)
self.center_x = SCREEN_WIDTH//2
self.center_y = 100
class Cart(Sprite):
def __init__(self, image):
super().__init__(image)
self.center_y = random.randint(SCREEN_HEIGHT, SCREEN_HEIGHT+SCREEN_HEIGHT//2)
self.change_y = -6
class GameOver(Sprite):
def __init__(self, image):
super().__init__(image)
self.center_x = SCREEN_WIDTH//2
self.center_y = SCREEN_HEIGHT//2
class StatusBar():
def __init__(self):
self.distance = 0
self.hp = 3
def draw_distance(self):
pos_x = 10
pos_y = SCREEN_HEIGHT - 20
draw_text(f"路程:{self.distance}",pos_x,pos_y,color.BLUE,font_name={"simhei","pingfang"})
def draw_bar(self):
draw_rectangle_filled(SCREEN_WIDTH//2, SCREEN_HEIGHT-15, SCREEN_WIDTH,30,color.WHITE)
def draw_hp(self):
pos_x = SCREEN_WIDTH//2 - 50
pos_y = SCREEN_HEIGHT - 20
draw_text("血量:",pos_x,pos_y,color.BLUE,font_name=("simhei","PingFang"))
hearts = SpriteList()
for i in range(self.hp):
heart = Sprite("images/血量.png")
heart.center_x = pos_x + 50 +heart.width *i
heart.center_y = pos_y + 5
hearts.append(heart)
hearts.draw()
class MyCar(arcade.Window):
def __init__(self, width, height, title):
super().__init__(width, height, title)
self.WEAPON = False
self.setup()
def setup(self):
self.road1 = Road("images/车道.png")
self.road2 = Road("images/车道.png")
self.road2.center_y = SCREEN_HEIGHT//2 + SCREEN_HEIGHT
self.small_car = SmallCar("images/小车1.png")
self.weapon = Weapon("images/bomb.png")
self.gameover = GameOver("images/gameover.png")
self.carts = SpriteList()
self.create_carts()
self.total_time = 0
self.last_time = 0
self.status_bar = StatusBar()
self.game_status = True
def on_draw(self):
start_render()
self.road1.draw()
self.road2.draw()
self.small_car.draw()
for cart in self.carts:
cart.draw()
if self.WEAPON == True:
self.weapon.draw()
self.weapon.bullet_move()
if self.WEAPON == True and self.weapon.center_y > 800:
self.WEAPON = False
self.weapon.center_x = self.small_car.center_x
self.weapon.center_y = 100
self.status_bar.draw_bar()
self.status_bar.draw_distance()
self.status_bar.draw_hp()
if not self.game_status:
self.gameover.draw()
def on_update(self, delta_time: float):
if self.game_status:
self.small_car.update()
self.road1.update()
self.road2.update()
for cart in self.carts:
cart.update()
if cart.top < 0:
cart.kill()
self.total_time += delta_time
if int(self.total_time) != int(self.last_time) and int(self.total_time) % 6 == 0:
self.create_carts()
self.last_time = self.total_time
self.status_bar.distance = int(self.total_time)
hit_list = check_for_collision_with_list(self.small_car, self.carts)
if hit_list:
for hit in hit_list:
hit.kill()
self.status_bar.hp -=1
hit_list2 = check_for_collision_with_list(self.weapon, self.carts)
if hit_list2:
for hit2 in hit_list2:
hit2.kill()
self.judge_game_status()
def on_key_release(self, symbol: int, modifiers: int):
if symbol == key.SPACE:
self.weapon.draw()
self.weapon.bullet_move()
def on_key_press(self, symbol: int, modifiers: int):
if symbol == key.UP :
self.road1.change_y -= 4
self.road2.change_y -= 4
def on_key_release(self,symbol:int,modifiers:int):
if symbol == key.LEFT and self.small_car.center_x >= SCREEN_WIDTH//2:
self.small_car.center_x -= 200
if self.WEAPON ==False:
self.weapon.center_x -= 200
elif symbol == key.RIGHT and self.small_car.center_x <= SCREEN_WIDTH//2:
self.small_car.center_x += 200
if self.WEAPON == False:
self.weapon.center_x += 200
elif symbol == key.R:
self.status_bar.hp=2
self.game_status = True
elif symbol == key.UP :
self.road1.change_y = -5
self.road2.change_y = -5
elif symbol == key.SPACE :
self.WEAPON = True
def create_carts(self):
cart_list = ("images/大车1.png", "images/大车2.png",
"images/大车3.png")
num = 2
x = random.sample([SCREEN_WIDTH//2-200, SCREEN_WIDTH//2, SCREEN_WIDTH // 2 +200],2)
for i in range(num):
cart = Cart(random.choice(cart_list))
cart.center_x = x[i]
self.carts.append(cart)
def judge_game_status(self):
if self.status_bar.hp <=0:
self.game_status = False
class Weapon(Sprite):
def __init__(self, image):
super().__init__(image)
self.center_x = SCREEN_WIDTH //2
self.center_y = 100
def bullet_move(self):
self.center_y+=10
if __name__ == '__main__':
game = MyCar(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
run()