日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python乌龟吃鱼_python学习之乌龟吃鱼and思聪吃热狗游戏

發布時間:2023/12/14 python 87 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python乌龟吃鱼_python学习之乌龟吃鱼and思聪吃热狗游戏 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

烏龜吃魚游戲

游戲規則:

1). 假設游戲場景為范圍(x,y)為0<=x<=10,0<=y<=10

2). 游戲生成1只烏龜和10條魚, 它們的移動方向均隨機

3). 烏龜的最大移動能力為2(它可以隨機選擇1還是2移動),

魚兒的最大移動能力是1當移動到場景邊緣,自動向反方向移動

4). 烏龜初始化體力為100(上限), 烏龜每移動一次,體力消耗1

當烏龜和魚坐標重疊,烏龜吃掉魚,烏龜體力增加20, 魚暫不計算體力

5). 當烏龜體力值為0(掛掉)或者魚兒的數量為0游戲結束

01_turtle_fish.py

import random

class Turtle(object):

"""

烏龜類

屬性: (x,y), power

方法: move(), eat()

"""

def __init__(self):

self.x = random.randint(0, 10)

self.y = random.randint(0, 10)

self.power = 100

def move(self):

"""烏龜移動的方法"""

#烏龜的最大移動能力是2當移動到場景邊緣,

move_skills = [-2, -1, 1, 2]

#計算魚最新的x軸坐標;()

new_x = self.x + random.choice(move_skills)

new_y = self.y + random.choice(move_skills)

#當移動到場景邊緣如何處理?

#(10, 0) ---- (11, 1) ----- (1, 1)

#(10, 0) --- (9, -1) -----(9, 9)

self.x = new_x % 10

self.y = new_y % 10

def eat(self):

"""烏龜吃魚"""

self.power += 20

print("烏龜吃魚, 能量+20!")

class Fish(object):

"""

魚類

屬性: (x,y)

方法: move()

"""

def __init__(self):

self.x = random.randint(0, 10)

self.y = random.randint(0, 10)

def move(self):

"""

魚兒的最大移動能力是1當移動到場景邊緣,

"""

#魚兒的最大移動能力是1當移動到場景邊緣,

move_skills = [-1, 1]

#計算魚最新的x軸坐標;()

new_x = self.x + random.choice(move_skills)

new_y = self.y + random.choice(move_skills)

#當移動到場景邊緣如何處理?

self.x = new_x % 10

self.y = new_y % 10

if __name__ == '__main__':

# 游戲生成1只烏龜和10條魚, 它們的移動方向均隨機.

tur = Turtle()

fishes = [Fish() for item in range(10)]"""

01_turtle_fish.py

import random

class BaseAnimal(object):

def __init__(self):

self.x = random.randint(0, 10)

self.y = random.randint(0, 10)

def move(self, move_skills=(-1, 1)):

new_x = self.x + random.choice(move_skills)

new_y = self.y + random.choice(move_skills)

#當移動到場景邊緣如何處理?

#(10, 0) ---- (11, 1) ----- (1, 1)

#(10, 0) --- (9, -1) -----(9, 9)

self.x = new_x % 10

self.y = new_y % 10

class Turtle(BaseAnimal):

"""

烏龜類

屬性: (x,y), power

方法: move(), eat()

"""

def __init__(self):

super(Turtle, self).__init__()

self.power = 100

def eat(self):

"""烏龜吃魚"""

self.power += 20

print("烏龜吃魚, 能量+20!")

class Fish(BaseAnimal):

pass

if __name__ == '__main__':

# 游戲生成1只烏龜和10條魚, 它們的移動方向均隨機.

tur = Turtle()

fishes = [Fish() for item in range(10)]"""

03_pygame游戲基本操作.py

import pygame

#1). 初始化pygame

pygame.init()

#2). 顯示游戲界面

pygame.display.set_mode((800, 800))

while True:

for event in pygame.event.get():

if event.type == pygame.QUIT:

print("游戲結束......")

exit(0)

elif event.type == pygame.KEYDOWN:

if event.key == pygame.K_UP:

print("UP")

elif event.key == pygame.K_DOWN:

print('DOWN')"""

04_game.py #同理思聰吃熱狗游戲

import random

import pygame

class SiCong(object):

"""

思聰類

屬性: (x,y), power

方法: move(), eat()

"""

def __init__(self):

self.x = random.randint(50, width - 50)

self.y = random.randint(50, height - 50)

self.power = 100

def move(self, new_x, new_y):

"""思聰移動的方法"""

self.x = new_x % width

self.y = new_y % height

def eat(self):

"""思聰吃熱狗"""

self.power += 20

print("思聰吃熱狗, 能量+20!")

class HotDog(object):

"""

熱狗類

屬性: (x,y)

方法: move()

"""

def __init__(self):

self.x = random.randint(50, width - 50)

self.y = random.randint(50, height - 50)

def move(self):

"""

魚兒的最大移動能力是1當移動到場景邊緣,

"""

#魚兒的最大移動能力是1當移動到場景邊緣,

move_skills = [-10]

#計算魚最新的x軸坐標;()

new_x = self.x + random.choice(move_skills)

#當移動到場景邊緣如何處理?

self.x = new_x % width

def main():

pygame.init()

#顯示游戲界面

screen = pygame.display.set_mode((width, height))

#設置界面標題

pygame.display.set_caption("吃熱狗游戲")

#加載游戲中需要的圖片

bg = pygame.image.load('./img/bigger_bg1.jpg').convert()

hotdogImg = pygame.image.load('./img/hot-dog.png').convert_alpha()

sicongImg = pygame.image.load('./img/sicong.png').convert_alpha()

hd_width, hd_height = hotdogImg.get_width(), hotdogImg.get_height()

sc_width, sc_height = sicongImg.get_width(), sicongImg.get_height()

#加載游戲音樂(背景音樂和吃掉熱狗的音樂)

pygame.mixer.music.load('./img/game_music.mp3')

pygame.mixer.music.play(loops=0, start=0.0) # 播放設置, 不循環且從0.0s開始播放

#設置分數顯示參數信息(顯示位置、字體顏色、字體大小)

scoreCount = 0

font = pygame.font.SysFont('arial', 20) # 系統設置字體的類型和大小

#顏色表示法: RGB (255, 0, 0)-紅色 (255, 255, 255)-白色 (0, 0, 0)-黑色

score = font.render("Score: %s" % (scoreCount), True, (0, 0, 0))

#創建一個Clock對象,跟蹤游戲運行時間

fpsClock = pygame.time.Clock()

#創建一個思聰和10個熱狗

sicong = SiCong()

hotdogs = [HotDog() for item in range(10)]

while True:

for event in pygame.event.get():

if event.type == pygame.QUIT:

print("游戲結束......")

exit(0)

elif event.type == pygame.KEYDOWN:

if event.key == pygame.K_UP:

# 移動人物向上多少個像素

sicong.move(sicong.x, sicong.y - 10)

elif event.key == pygame.K_DOWN:

sicong.move(sicong.x, sicong.y + 10)

if event.key == pygame.K_LEFT:

# 移動人物向上多少個像素

sicong.move(sicong.x - 10, sicong.y)

elif event.key == pygame.K_RIGHT:

sicong.move(sicong.x + 10, sicong.y)

#繪制背景和分數

screen.blit(bg, (0, 0))

screen.blit(score, (200, 20))

#繪制熱狗,并實現熱狗的移動

for hd in hotdogs:

screen.blit(hotdogImg, (hd.x, hd.y))

hd.move()

#繪制sicong

screen.blit(sicongImg, (sicong.x, sicong.y))

#判斷游戲是否結束: 當人物體力值為0(掛掉)或者熱狗的數量為0游戲結束

if sicong.power == 0:

print("Game Over: Sicong Poer is 0")

exit(1)

if len(hotdogs) == 0:

print("Game Over: hot-dog count is 0")

exit(2)

#判斷人物是否吃到熱狗:人物和熱狗的坐標值相同, 則認為吃掉

for hd in hotdogs:

if 0 < sicong.x - hd.x < 50 and 0 < sicong.y - hd.y < 50:

# 增加人物的能量值

sicong.eat()

#移除被吃掉的熱狗

hotdogs.remove(hd)

#增加得分

scoreCount += 10

#重新設置得分信息

score = font.render("Score: %s" % (scoreCount), True, (0, 0, 0))

#更新內容到游戲窗口

pygame.display.update()

fpsClock.tick(10) # 每秒更新10幀

if __name__ == '__main__':

width =1000

height = 666

main()

05_烏龜吃魚游戲.py

import random

import time

import pygame

import sys

from pygame.locals import * # 導入一些常用的函數

width = 474

height = 233

pygame.init()

screen = pygame.display.set_mode([width, height])

pygame.display.set_caption('烏龜吃魚') # 定義窗口的標題為'烏龜吃魚'

background = pygame.image.load("./img/bg.jpg").convert()

fishImg = pygame.image.load("./img/hot-dog.png").convert_alpha()

wuguiImg = pygame.image.load("./img/sicong.png").convert_alpha()

#烏龜吃掉小魚的音樂 mp3格式的不行,wav格式的

#eatsound = pygame.mixer.Sound("achievement.wav")

#背景音樂

pygame.mixer.music.load("./img/game_music.mp3")

pygame.mixer.music.play(loops=0, start=0.0)

#成績文字顯示

count = 0

font = pygame.font.SysFont("arial", 20)

score = font.render("score %d" % count, True, (255, 255, 255))

#顯示游戲狀態

status = font.render("Gaming", True, (255, 255, 255))

w_width = wuguiImg.get_width() - 5 # 得到烏龜圖片的寬度,后面留著吃魚的時候用

w_height = wuguiImg.get_height() - 5 # 得到烏龜圖片的高度

y_width = fishImg.get_width() - 5 # 得到魚圖片的寬度

y_height = fishImg.get_height() - 5 # 得到魚圖片的高度

fpsClock = pygame.time.Clock() # 創建一個新的Clock對象,可以用來跟蹤總共的時間

#烏龜類

class Turtle:

def __init__(self):

self.power = 100 # 體力

#烏龜坐標

self.x = random.randint(0, width - w_width)

self.y = random.randint(0, height - w_height)

#烏龜移動的方法:移動方向均隨機 第四條

def move(self, new_x, new_y):

self.x = new_x % width

self.y = new_y % height

self.power -= 1 # 烏龜每移動一次,體力消耗1

def eat(self):

self.power += 20 # 烏龜吃掉魚,烏龜體力增加20

if self.power > 100:

self.power = 100 # 烏龜體力100(上限)

#魚類

class Fish:

def __init__(self):

# 魚坐標

self.x = random.randint(0, width - y_width)

self.y = random.randint(0, height - y_height)

def move(self):

new_x = self.x + random.choice([-10])

self.x = new_x % width

tur = Turtle() # 生成1只烏龜

fish = [] # 生成10條魚

for item in range(10):

newfish = Fish()

fish.append(newfish) # 把生成的魚放到魚缸里

#pygame有一個事件循環,不斷檢查用戶在做什么。事件循環中,如何讓循環中斷下來(pygame形成的窗口中右邊的插號在未定義前是不起作用的)

while True:

for event in pygame.event.get():

if event.type == pygame.QUIT:

sys.exit()

if event.type == pygame.KEYDOWN:

# 通過上下左右方向鍵控制烏龜的動向

if event.key == pygame.K_LEFT:

tur.move(tur.x - 10, tur.y)

if event.key == pygame.K_RIGHT:

tur.move(tur.x + 10, tur.y)

if event.key == pygame.K_UP:

tur.move(tur.x, tur.y - 10)

if event.key == pygame.K_DOWN:

tur.move(tur.x, tur.y + 10)

screen.blit(background, (0, 0)) # 繪制背景圖片

screen.blit(score, (400, 20)) # 繪制分數

screen.blit(status, (0, 20)) # 繪制分數

#繪制魚

for item in fish:

screen.blit(fishImg, (item.x, item.y))

#pygame.time.delay(100)

item.move() # 魚移動

screen.blit(wuguiImg, (tur.x, tur.y)) # 繪制烏龜

#判斷游戲是否結束:當烏龜體力值為0(掛掉)或者魚兒的數量為0游戲結束

if tur.power < 0:

print("Game Over: Turtle power is 0")

#顯示游戲狀態

status = font.render("Game Over: Turtle power is 0", True, (255, 255, 255))

pygame.display.update() # 更新到游戲窗口

time.sleep(1)

sys.exit(0)

elif len(fish) == 0:

status = font.render("Game Over: Fish is empty", True, (255, 255, 255))

pygame.display.update() # 更新到游戲窗口

sys.exit(0)

for item in fish:

# print("魚", item.x, item.y, y_width, y_height)

#print("烏龜", tur.x, tur.y, w_width, w_height)

#判斷魚和烏龜是否碰撞?

if ((tur.x < item.x + y_width) and (tur.x + w_width > item.x)

and (tur.y < item.y + y_height) and (w_height + tur.y > item.y)):

fish.remove(item) # 魚死掉

#吃魚音樂

#eatsound.play()

count = count + 1 # 累加

score = font.render("score %d" % count, True, (255, 255, 255))

#print("死了一只魚")

#print("烏龜最新體力值為 %d" % tur.power)

pygame.display.update() # 更新到游戲窗口

fpsClock.tick(10) # 通過每幀調用一次fpsClock.tick(10),這個程序就永遠不會以超過每秒10幀的速度運行

總結

以上是生活随笔為你收集整理的python乌龟吃鱼_python学习之乌龟吃鱼and思聪吃热狗游戏的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。