python初学者日志1

这个博客介绍了作者创建的一款名为'game'的游戏,游戏中包含道路、小车、武器系统和游戏结束状态。玩家可以控制小车上下移动,并通过空格键发射武器攻击随机出现的大车。当大车撞到小车或被武器击中时,游戏会根据剩余血量更新状态。游戏画面由多个精灵类组成,包括车道、小车、大车和游戏结束图像。
摘要由CSDN通过智能技术生成

记录一下我的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()

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值