游戏介绍:
一款单人版的思聪吃热狗游戏,你可以自己调节思聪的位置,移动时会消耗能量10,游戏中吃到热狗分数加 1,能量加 20,最后的目标就是称霸世界咯,吃掉所有的热狗即游戏胜利。王思聪能量消耗完毕即游戏失败。
如何开始:
玩家:键盘方向键↑↓←→控制王思聪的移动。
游戏目标:
不断的吃掉热狗,不断的强大起来吧!
游戏素材:
游戏要求:
1. 游戏背景可以为黑色或者其他图片(自定义即可);
2. 王思聪可以上下左右移动,热狗只能向左移动,当移动到最左边时, 穿越屏幕,到达最右端,继续向左移动。
3. 王思聪默认能量值(power)为 200,每移动一次消耗能量值 10,当吃到一个热狗, 能量值增加 20。
4. 王思聪只有一个,而热狗的个数是随机的(10~40 个之间)。
5. 当热狗被彻底消灭掉或者思聪毫无能量值时,游戏结束。
游戏进阶要求:能否实现一个双人版吃热狗游戏?
在这大千世界里,现在的所有物种都是通过自身群体间的相互竞争进化而生存下来,物竞天择这个词就是这样的由来,生存环境发生了变化,要么尝试地去改变去适应它,要么就被无情地淘汰,最终陈列在博物馆内。我们的祖先经过几亿年的演变,适应了一个又一个的极端环境,才有了现在的我们,他们的坚韧体现的淋漓尽致。
emmmmmm,貌似扯远了....双人游戏很好的诠释了物竞天择这一点。
进阶游戏要求:
我们刚开始是两个人物,一个玩家是你,一个玩家是王思聪,周围都是仅剩无几的热狗食物,为了生存两个玩家必须争夺仅有的热狗。 当热狗吃完时,谁的得分高,谁就是最终的赢家。 或者其中一方玩家体力消耗仅剩 0,另外一方玩家即为赢家。
操作要求:
玩家 1:键盘方向键↑↓←→控制移动。
玩家 2:键盘 WSAD 控制控制移动。
编写代码如下,可供参考:
import random
import time
import sys
import pygame
from pygame.locals import *
from PIL import Image
# 首先缩小游戏背景图片尺寸
filename = './img/background.jpg'
img = Image.open(filename)
out = img.resize((800, 600) , Image.ANTIALIAS)
out.save('./img/background_resize.jpg')
width = 800
height = 600
pygame.init()
screen = pygame.display.set_mode([width, height])
pygame.display.set_caption('王思聪吃热狗(双人版)')
background = pygame.image.load('./img/background_resize.jpg').convert()
hotdogImg = pygame.image.load('./img/hotdog.png').convert_alpha()
wangImg = pygame.image.load('./img/wang.png').convert_alpha()
count1 = 0
count2 = 0
font = pygame.font.SysFont("arial",40)
player1_score = font.render("player1 score:%d" %(count1), True, (255, 255, 255))
player2_score = font.render("player2 score:%d" %(count2), True, (255, 255, 255))
w_width = wangImg.get_width() - 5
w_height = wangImg.get_height() - 5
h_width = hotdogImg.get_width() - 5
h_height = hotdogImg.get_height() - 5
fpsClock = pygame.time.Clock()
class Wang1:
def __init__(self):
self.power = 200
self.x = random.randint(0, width-w_width)
self.y = random.randint(0, height-w_height)
def move(self, new_x, new_y):
if new_x < 0:
self.x = 0 - new_x
elif new_x > width:
self.x = 0
else:
self.x = new_x
if new_y < 0:
self.y = 0 - new_y
elif new_y > height:
self.y = 0
else:
self.y = new_y
self.power -= 10
def eat(self):
self.power += 20
if self.power > 200:
self.power = 200
class Wang2:
def __init__(self):
self.power = 200
self.x = random.randint(0, width-w_width)
self.y = random.randint(0, height-w_height)
def move(self, new_x, new_y):
if new_x < 0:
self.x = 0 - new_x
elif new_x > width:
self.x = 0
else:
self.x = new_x
if new_y < 0:
self.y = 0 - new_y
elif new_y > height:
self.y = 0
else:
self.y = new_y
self.power -= 10
def eat(self):
self.power += 20
if self.power > 200:
self.power = 200
class Hotdog:
def __init__(self):
self.x = random.randint(0, width - h_width)
self.y = random.randint(0, height - h_height)
def move(self):
new_x = self.x + random.choice([-10])
if self.x < 0:
self.x = width
else:
self.x = new_x
wang1 = Wang1()
wang2 = Wang2()
pygame.display.update()
hotdog = []
for item in range(random.randint(10,40)):
newHotdog = Hotdog()
hotdog.append(newHotdog)
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:
wang1.move(wang1.x - 10, wang1.y)
if event.key == pygame.K_RIGHT:
wang1.move(wang1.x + 10, wang1.y)
if event.key == pygame.K_UP:
wang1.move(wang1.x, wang1.y - 10)
if event.key == pygame.K_DOWN:
wang1.move(wang1.x, wang1.y + 10)
if event.key == pygame.K_a:
wang2.move(wang2.x - 10, wang2.y)
if event.key == pygame.K_d:
wang2.move(wang2.x + 10, wang2.y)
if event.key == pygame.K_w:
wang2.move(wang2.x, wang2.y - 10)
if event.key == pygame.K_s:
wang2.move(wang2.x, wang2.y + 10)
screen.blit(background, (0, 0))
screen.blit(player2_score, (580, 20))
screen.blit(player1_score, (0, 20))
for item in hotdog:
screen.blit(hotdogImg, (item.x, item.y))
item.move()
screen.blit(wangImg, (wang1.x, wang1.y))
screen.blit(wangImg, (wang2.x, wang2.y))
if wang1.power < 0:
print("Player2 Win!")
pygame.display.update()
time.sleep(1)
sys.exit(0)
if wang2.power < 0:
print("Player1 Win!")
pygame.display.update()
time.sleep(1)
sys.exit(0)
elif len(hotdog) == 0:
if player1_score > player2_score:
print("Player1 Win!")
else:
print("Player2 Win!")
pygame.display.update()
sys.exit(0)
for item in hotdog:
if ((wang1.x < item.x + w_width) and (wang1.x + w_width > item.x)
and (wang1.y < item.y + h_height) and (w_height + wang1.y > item.y)) :
hotdog.remove(item)
count1 = count1 + 1
player1_score = font.render("player1 score:%d" % count1, True, (255, 0, 0))
for item in hotdog:
if ((wang2.x < item.x + w_width) and (wang2.x + w_width > item.x)
and (wang2.y < item.y + h_height) and (w_height + wang2.y > item.y)) :
hotdog.remove(item)
count2 = count2 + 1
player2_score = font.render("player2 score:%d" % count2, True, (255, 0, 0))
pygame.display.update()
fpsClock.tick(10)
运行之后,游戏的要求都可以实现,在这里放一张游戏操作界面的截图: