python大鱼吃小鱼程序代码_写简单游戏,学编程语言-python篇:大鱼吃小鱼

importpygame,sys,time,randomfrom pygame.locals import *WINWIDTH=640WINHEIGHT=480CAMERASLACK=70lfish_img=[]

FISHSIZE=8PLAYERSIZE=3SCREEN=None

rfish_img=[]

lplayer_img=[]

rplayer_img=[]

backgroundimg=None

rboss_img=None

lboss_img=None

FPS=30WHITE=[255,255,255]

MAXHEALTH=3MINSPEED=3MAXSPEED=9MOVESPEED=7HALF_WINWIDTH=int(WINWIDTH/2)

HALF_WINHEIGHT=int(WINHEIGHT/2)

WINSIZE=300

defmain():globalFPSCLOCK,SCREEN,lfish_img,rfish_img,lplayer_img,rplayer_img,BASICFONT ,rboss_img,lboss_img,backgroundimg

pygame.init()

FPSCLOCK=pygame.time.Clock()

SCREEN=pygame.display.set_mode((WINWIDTH,WINHEIGHT))

pygame.display.set_caption("fish eat fish")

BASICFONT=pygame.font.Font("freesansbold.ttf",32)for i inrange(FISHSIZE):

fishimg=pygame.image.load("Fish%s.bmp" %i)

fishimg.set_colorkey(WHITE)

lfish_img.append(fishimg)

rfishimg=pygame.transform.flip(fishimg,True,False)

rfish_img.append(rfishimg)for i inrange(PLAYERSIZE):

playerimg=pygame.image.load("player%s.png" %i)

playerimg.set_colorkey(WHITE)

lplayer_img.append(playerimg)

rplayerimg=pygame.transform.flip(playerimg,True,False)

rplayerimg.set_colorkey(WHITE)

rplayer_img.append(rplayerimg)

bossimg=pygame.image.load("boss.png")#bossimg.set_colorkey([0,0,0])#SCREEN.blit(bossimg,(0,0))

lboss_img=bossimg

rbossimg=pygame.transform.flip(bossimg,True,False)

rboss_img=rbossimg

backgroundimg=pygame.image.load("background.jpg")whileTrue:

runGame()defrunGame():

invulnerableMode=Fa

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
大鱼吃小鱼是一个经典的游戏,可以用 Python 编程实现。下面是一个简单的示例代码: ```python import pygame import random # 初始化 Pygame pygame.init() # 设置游戏窗口大小 window_width = 640 window_height = 480 window = pygame.display.set_mode((window_width, window_height)) pygame.display.set_caption("Big Fish Eats Small Fish") # 加载图像资源 bg_image = pygame.image.load("background.jpg") fish_image = pygame.image.load("fish.png") # 设置游戏参数 fish_size = 50 fish_speed = 5 fish_x = window_width / 2 - fish_size / 2 fish_y = window_height / 2 - fish_size / 2 fish_direction = "right" fish_score = 0 small_fish_size = 20 small_fish_speed = 3 small_fishes = [] # 随机生成小鱼 def generate_small_fish(): x = random.randint(0, window_width - small_fish_size) y = random.randint(0, window_height - small_fish_size) small_fishes.append({"x": x, "y": y}) # 游戏循环 running = True while running: # 处理事件 for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # 更新游戏状态 keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: fish_x -= fish_speed fish_direction = "left" elif keys[pygame.K_RIGHT]: fish_x += fish_speed fish_direction = "right" elif keys[pygame.K_UP]: fish_y -= fish_speed elif keys[pygame.K_DOWN]: fish_y += fish_speed # 碰撞检测 for small_fish in small_fishes: if fish_x < small_fish["x"] + small_fish_size and \ fish_x + fish_size > small_fish["x"] and \ fish_y < small_fish["y"] + small_fish_size and \ fish_y + fish_size > small_fish["y"]: small_fishes.remove(small_fish) fish_score += 1 # 生成新的小鱼 if len(small_fishes) == 0: generate_small_fish() # 绘制游戏界面 window.blit(bg_image, (0, 0)) for small_fish in small_fishes: pygame.draw.rect(window, (0, 0, 255), (small_fish["x"], small_fish["y"], small_fish_size, small_fish_size)) if fish_direction == "right": fish_image = pygame.transform.flip(fish_image, True, False) window.blit(fish_image, (fish_x, fish_y)) pygame.display.update() # 更新小鱼位置 for small_fish in small_fishes: if fish_x < small_fish["x"]: small_fish["x"] -= small_fish_speed elif fish_x > small_fish["x"]: small_fish["x"] += small_fish_speed if fish_y < small_fish["y"]: small_fish["y"] -= small_fish_speed elif fish_y > small_fish["y"]: small_fish["y"] += small_fish_speed # 游戏结束条件 if fish_score >= 10: running = False # 结束游戏 pygame.quit() ``` 这个示例代码实现了一个简单大鱼吃小鱼游戏。在游戏中,玩家控制大鱼移动,吃掉屏幕上随机生成的小鱼,直到获得足够的分数。游戏过程中需要注意碰撞检测和小鱼的移动。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值