python如何使板子移动_python实现移动木板小游戏

本文介绍了一个使用Python编写的移动木板小游戏,旨在帮助初学者熟悉Python编程。游戏包括初始化、设置屏幕、自定义函数等步骤,玩家可以通过键盘控制木板移动,接住下落的球来得分。游戏具有等级提升功能,每达到一定分数,等级会增加。
摘要由CSDN通过智能技术生成

本文实例为大家分享了python实现移动木板小游戏的具体代码,供大家参考,具体内容如下

一、游戏简介

本游戏是通过python编写的小游戏,给初学者熟悉python编程语言抛砖引玉,希望有所帮助。

成型的效果图如下:

二、编写步骤

1.引入库

代码如下:

###### AUTHOR:破茧狂龙 ######

###### DATE:20201002 ######

###### DESCRIPTION:移动的木板 ######

import pygame

from pygame.locals import *

import sys

import time

import random

2.初始化

代码如下:

pygame.init()

BLACK = (0, 0, 0) # 黑色

WHITE = (255, 255, 255) # 白色

bg_color = (0,0,70) # 背景颜色

red = (200, 0, 0)

green = (0, 200, 0)

bright_red = (255, 0, 0)

bright_green = (0, 255, 0)

smallText = pygame.font.SysFont('SimHei', 20) #comicsansms

midlText = pygame.font.SysFont('SimHei', 50)

barsize = [30, 10]

SCREEN_SIZE = [400, 500] # 屏幕大小

BALL_SIZE = [15, 15] # 球的尺寸

fontcolor = (255,255,255) # 定义字体的颜色

myimg = r"img\b1.jpg"

background = pygame.image.load(myimg) # 图片位置

background = pygame.transform.scale(background, SCREEN_SIZE)

# ball 初始位置

ball_pos_x = SCREEN_SIZE[0] // 2 - BALL_SIZE[0] / 2

ball_pos_y = 0

# ball 移动方向

ball_dir_y = 1 # 1:down

ball_pos = pygame.Rect(ball_pos_x, ball_pos_y, BALL_SIZE[0], BALL_SIZE[1])

clock = pygame.time.Clock() # 定时器

screen = pygame.display.set_mode(SCREEN_SIZE)

# 设置标题

pygame.display.set_caption('python小游戏-移动木板')

# 设置图标

image = pygame.image.load(myimg)

pygame.display.set_icon(image)

3.相关自定义函数

代码如下:

###### 自定义函数 ######

def button(msg, x, y, w, h, ic, ac, action=None):

mouse = pygame.mouse.get_pos()

click = pygame.mouse.get_pressed()

if x + w > mouse[0] > x and y + h > mouse[1] > y:

pygame.draw.rect(screen, ac, (x, y, w, h))

if click[0] == 1 and action != None:

action()

else:

pygame.draw.rect(screen, ic, (x, y, w, h))

textSurf, textRect = text_objects(msg, smallText)

textRect.center = ((x + (w / 2)), (y + (h / 2)))

screen.blit(textSurf, textRect)

def text_objects(text, font):

textSurface = font.render(text, True, fontcolor)

return textSurface, textSurface.get_rect()

def quitgame():

pygame.quit()

quit()

def message_diaplay(text):

largeText = pygame.font.SysFont('SimHei', 115)

TextSu

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值