python编程游戏软件_python编程之bomb catcher 小游戏

一个简单的演示,综合了鼠标的输入、一些基本的图形绘制等等。当炸弹到达屏幕底端的时候如果没有抓住的话会丢掉性命。如果撞击到挡板,玩家就算抓住了炸弹,另一个炸弹也会落下。

源代码如下:

# -*- coding: utf-8 -*-

"""

Created on Tue Jun 30 23:42:49 2015

@author: liuchang

"""

import pygame,sys,random

from pygame.locals import *

pygame.init()

color= 255,255,0

#绘制圆心的位置

positionx=300

positiony=0

radius = 30

width=5

vel_x = 1

vel_y = 1

rec_x=250

rec_y=500

rec_a=100

rec_b=20

mouse_x=0

mouse_y=0

lives = 10

score = 0

#判断是否需要重启游戏

game_over =False

font1 =pygame.font.Font(None,30)

screen= pygame.display.set_mode((600,520))

pygame.display.set_caption("bomb game")

def print_text(font,x,y,text,color=(255,255,255)):

imgText=font.render(text,True,color)

screen.blit(imgText,(x,y))

while True:

for event in pygame.event.get():

if event.type in (QUIT,KEYDOWN):

sys.exit()

elif event.type== MOUSEMOTION:

mouse_x,mouse_y = event.pos

#print positionx

positionx +=vel_x

positiony+=vel_y

if positionx600-radius:

vel_x =- vel_x

if positiony<0 or positiony>500-radius:

vel_y =- vel_y

screen.fill((0,0,200))

#draw boom

pygame.draw.circle(screen,color ,(positionx,positiony),radius,0)

#draw rectangle

rec_x = mouse_x

if rec_x <0:

rec_x=0

elif rec_x > 500:

rec_x = 500

pygame.draw.rect(screen,color,(rec_x,rec_y,rec_a,rec_b),0)

#score and lives

if positiony == 500-radius :

if positionx>rec_x and positionx

score+=1

positionx = random.randint(0,500)

positiony=0

else:

lives -=1

positionx = random.randint(0,500)

positiony=0

if lives == 0:

game_over =True

print "game over"

if game_over :

print_text(font1,100,200,"")

lives=0

score = 0

#draw score and lives

print_text(font1,0,0,"lives: "+str(lives/2))

print_text(font1,500,0,"score :"+str(score/2))

pygame.display.update()

截图:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 Python 扫雷游戏代码示例: ```python import random # 定义常量 BOMB = -1 HIDDEN = 0 UNCOVERED = 1 class Minesweeper: def __init__(self, rows, cols, bombs): self.rows = rows self.cols = cols self.bombs = bombs self.grid = [[HIDDEN] * cols for _ in range(rows)] self.generate_bombs() def generate_bombs(self): bombs_placed = 0 while bombs_placed < self.bombs: row = random.randint(0, self.rows - 1) col = random.randint(0, self.cols - 1) if self.grid[row][col] != BOMB: self.grid[row][col] = BOMB bombs_placed += 1 def uncover(self, row, col): if self.grid[row][col] == BOMB: return False elif self.grid[row][col] == HIDDEN: self.grid[row][col] = UNCOVERED if self.neighbor_bomb_count(row, col) == 0: for r in range(max(0, row - 1), min(row + 2, self.rows)): for c in range(max(0, col - 1), min(col + 2, self.cols)): if r != row or c != col: self.uncover(r, c) return True def neighbor_bomb_count(self, row, col): count = 0 for r in range(max(0, row - 1), min(row + 2, self.rows)): for c in range(max(0, col - 1), min(col + 2, self.cols)): if self.grid[r][c] == BOMB: count += 1 return count def __str__(self): result = '' for row in self.grid: for cell in row: if cell == UNCOVERED: count = self.neighbor_bomb_count(self.grid.index(row), row.index(cell)) result += str(count) if count > 0 else ' ' elif cell == HIDDEN: result += '*' else: result += 'X' result += '\n' return result[:-1] # 示例用法 game = Minesweeper(5, 5, 5) print(game) game.uncover(2, 2) print(game) ``` 该示例代码使用了一个 `Minesweeper` 类来表示扫雷游戏,并包含了以下方法: - `__init__(self, rows, cols, bombs)`:构造方法,初始化游戏的行数、列数和炸弹数,并生成游戏网格。 - `generate_bombs(self)`:随机生成指定数量的炸弹。 - `uncover(self, row, col)`:揭开指定位置的格子,并递归揭开周围的空格(如果有的话)。 - `neighbor_bomb_count(self, row, col)`:返回指定位置周围的炸弹数量。 - `__str__(self)`:将游戏网格转换为字符串,方便输出。 你可以根据自己的需求对这个示例代码进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值