用pygame写一个大富翁游戏
时间: 2023-11-16 20:05:06 浏览: 12
用Python和Pygame写游戏
好的,下面是一个简单的大富翁游戏示例代码,需要用到pygame库:
```python
import pygame
import random
# 初始化pygame
pygame.init()
# 定义常量
WIDTH = 800
HEIGHT = 600
FPS = 30
TITLE = "Monopoly"
FONT_SIZE = 32
# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
# 创建游戏窗口
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption(TITLE)
clock = pygame.time.Clock()
# 设置字体
font = pygame.font.Font(None, FONT_SIZE)
# 创建玩家
class Player(pygame.sprite.Sprite):
def __init__(self, color, x, y, money):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((50, 50))
self.image.fill(color)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.money = money
def update(self):
# 玩家移动
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
self.rect.x -= 5
if keys[pygame.K_RIGHT]:
self.rect.x += 5
if keys[pygame.K_UP]:
self.rect.y -= 5
if keys[pygame.K_DOWN]:
self.rect.y += 5
def draw_money(self):
# 绘制玩家资产
text = font.render(str(self.money), True, BLACK)
screen.blit(text, (self.rect.x, self.rect.y - FONT_SIZE))
# 创建地图
class Map(pygame.sprite.Sprite):
def __init__(self, color, x, y, width, height):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((width, height))
self.image.fill(color)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
# 创建房产
class Property(pygame.sprite.Sprite):
def __init__(self, color, x, y, price, rent):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((50, 50))
self.image.fill(color)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.price = price
self.rent = rent
self.owner = None
def update(self):
# 判断是否有玩家到达房产位置
if player.rect.colliderect(self.rect):
if self.owner is None:
# 询问是否购买
if pygame.mouse.get_pressed()[0]:
if player.money >= self.price:
player.money -= self.price
self.owner = player
elif self.owner != player:
# 支付租金
player.money -= self.rent
self.owner.money += self.rent
def draw_price(self):
# 绘制房产价格
text = font.render(str(self.price), True, BLACK)
screen.blit(text, (self.rect.x, self.rect.y - FONT_SIZE))
def draw_owner(self):
# 绘制房产所有者
if self.owner is not None:
text = font.render(str(self.owner.money), True, BLACK)
screen.blit(text, (self.rect.x, self.rect.y + 50))
# 创建游戏元素
all_sprites = pygame.sprite.Group()
map = Map(WHITE, 0, 0, WIDTH, HEIGHT)
all_sprites.add(map)
player = Player((255, 0, 0), 0, 0, 1000)
all_sprites.add(player)
properties = []
colors = [(255, 255, 0), (0, 255, 255), (255, 0, 255)]
for i in range(3):
x = random.randint(0, WIDTH - 50)
y = random.randint(0, HEIGHT - 50)
price = random.randint(100, 500)
rent = price // 10
property = Property(colors[i], x, y, price, rent)
all_sprites.add(property)
properties.append(property)
# 游戏循环
running = True
while running:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# 更新游戏元素
all_sprites.update()
# 绘制游戏界面
screen.fill(BLACK)
all_sprites.draw(screen)
for property in properties:
property.draw_price()
property.draw_owner()
player.draw_money()
# 刷新屏幕
pygame.display.flip()
# 控制游戏帧率
clock.tick(FPS)
# 退出游戏
pygame.quit()
```
这个示例游戏中,玩家可以通过方向键移动,到达房产位置后可以购买房产或者支付租金。每个房产都有不同的价格和租金,玩家需要合理投资以增加自己的资产。
阅读全文