Pygame小游戏别踩钢琴块
Posted Nicholas4u
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Pygame小游戏别踩钢琴块相关的知识,希望对你有一定的参考价值。
源代码:
import random
import sys
import pygame
from pygame.sprite import Group, Sprite
pygame.init()
pygame.mixer.init()
photo1 = pygame.image.load("photo1.png")
tryto = 35
now_block = []
flag1 = [False, 0]
class Music:
music1 = pygame.mixer.Sound("music1.mp3")
music2 = pygame.mixer.Sound("music2.mp3")
music2.set_volume(0.6)
music3 = pygame.mixer.Sound("music3.mp3")
music3.set_volume(0.6)
music4 = pygame.mixer.Sound("music4.mp3")
music4.set_volume(0.6)
music5 = pygame.mixer.Sound("music5.mp3")
music5.set_volume(0.8)
class Color:
color1 = (196, 197, 202)
render_color = (72, 95, 249)
screen_color = (157, 159, 164)
def game_over(screen, score):
pygame.time.wait(1000)
gamefont = pygame.font.SysFont("宋体", 40)
scorerender = gamefont.render(f"score:score", True, Color.render_color)
screen.fill(Color.screen_color)
gameover_Render = gamefont.render("Game Over", True, Color.render_color)
att = gamefont.render("66666!", True, "pink")
screen.blit(att, [screen.get_rect().width / 3 - 20 + 30, screen.get_rect().centery - 150])
screen.blit(gameover_Render, [screen.get_rect().width / 3 - 20, screen.get_rect().centery])
screen.blit(scorerender, [screen.get_rect().width / 3 - 20, screen.get_rect().centery + 36])
pygame.display.flip()
pygame.time.wait(4000)
pygame.quit()
sys.exit()
def draw_lines(screen):
for i in range(100, 400, 100):
pygame.draw.line(screen, Color.color1, (i, 0), (i, 650), 4)
class Block(Sprite):
def __init__(self, screen, score):
Sprite.__init__(self)
global now_block
self.score = score
self.screen = screen
self.screen_rect = self.screen.get_rect()
self.speed = 5
self.a = random.choice(["photo2(1).png", "photo3(1).png"])
self.image = pygame.image.load(self.a)
self.rect = self.image.get_rect()
self.rect.y = 0
self.b = random.choice([[10, 1], [110, 2], [210, 3], [310, 4]])
self.number = self.b[1]
self.rect.x = self.b[0]
now_block.append(self.b[1])
def update(self):
global flag1
self.rect.top += self.speed
if flag1[0]:
if flag1[1] == self.number:
Music.music1.play()
self.kill()
flag1 = [False, 0]
if self.rect.bottom > self.screen_rect.height:
game_over(self.screen, self.score)
def main_start():
global flag1, tryto
pygame.display.set_caption("Tom 的别踩钢琴块") # 设置窗口名字
blocks = Group()
n_200 = n_500 = n_1000 = True
pygame.mixer.music.set_volume(0.6)
Music.music2.play(-1)
gamefont = pygame.font.SysFont("宋体", 40)
score = 0
count = 0
screen = pygame.display.set_mode((400, 650))
screen_rect = screen.get_rect()
clock = pygame.time.Clock()
while True:
if score >= 1000 and n_1000:
n_1000 = False
tryto = 10
Music.music4.stop()
Music.music5.play(-1)
if score >= 500 and n_500:
n_500 = False
Music.music3.stop()
tryto = 18
Music.music4.play(-1)
if score >= 200 and n_200:
n_200 = False
Music.music2.stop()
tryto = 24
Music.music3.play(-1)
count += 1
screen.blit(photo1, screen_rect)
if count % tryto == 0:
block = Block(screen, score)
blocks.add(block)
blocks.draw(screen)
clock.tick(20)
draw_lines(screen)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_1:
if 1 in now_block:
now_block.remove(1)
flag1 = [True, 1]
score += 10
else:
game_over(screen, score)
elif event.key == pygame.K_2:
if 2 in now_block:
now_block.remove(2)
flag1 = [True, 2]
score += 10
else:
game_over(screen, score)
if event.key == pygame.K_3:
if 3 in now_block:
now_block.remove(3)
flag1 = [True, 3]
score += 10
else:
game_over(screen, score)
if event.key == pygame.K_4:
if 4 in now_block:
now_block.remove(4)
flag1 = [True, 4]
score += 10
else:
game_over(screen, score)
blocks.update()
scorerender = gamefont.render(f"score:score", True, (72, 95, 249))
screen.blit(scorerender, scorerender.get_rect())
pygame.display.flip()
if __name__ == '__main__':
main_start()
C语言项目实战:《别踩白块游戏》零基础项目,137 行源代码示例
这篇文章主要为大家详细介绍了C语言实现——《别踩白块游戏》,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下!
游戏介绍:
《别踩白块儿 Don't Tap The White Tile》,这就是这个游戏的一个规则,我们只需要不断踩着黑色方块前进即可,很简单吧?谁都可以会玩,但并不是谁都能玩得很好噢,你呢?快来挑战看看吧!经典模式,以最快的速度到达终点。街机模式,你有能力得多少分就得多少分,没有任何限制,这也是最具挑战性的一个模式。限时模式,在30秒内看你能走几步。极速模式,没有最高速限制的街机模式, 挑战你的极限接力模式,规定时间内完成50块儿,然后会有更多时间去完成另外的50块儿!
好了,那么我们今天就用我们所学的C语言知识来做出这个游戏吧!本项目仅实现了街机模式,其他模式大家可以自行摸索哟!
本项目编译环境:VS2019/VS2013;
插件:图形库插件easyX,涉及图片素材可以自行百度找也可以关注文末领取;
效果图展示
配套讲解教程:别踩白块游戏教程——哔哩哔哩https://www.bilibili.com/video/BV1AA411b7sX?spm_id_from=333.999.0.0
源代码示例:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<graphics.h>
#include<mmsystem.h> //多媒体设备接口头文件
#pragma comment(lib,"winmm.lib") //多媒体设备接口库文件
#define mian main
#define WIDTH 400
#define HEIGHT 700
#define INTERVAL 100
//黑块怎么存储?
int map[4];
int score; //分数
void gameInit()
{
//播放音乐,向多媒体设备接口发送指令字符串
mciSendString(L"open ./野蜂飞舞.mp3 alias BGM", NULL, NULL, NULL); //media device interface send 发送 string 字符串
mciSendString(L"play BGM", NULL, NULL, NULL);
//设置随机数种子
srand((unsigned)time(NULL));
//对map进行随机初始化
for (size_t i = 0; i < 4; i++)
{
map[i] = rand() % 4;
}
}
//游戏界面绘制
void gameDraw()
{
//设置文字样式
settextstyle(30, 0, L"楷体");
settextcolor(RED);
outtextxy(150, 30, L"别踩白块");
//绘制游戏界面
setlinecolor(BLACK);
setlinestyle(PS_SOLID, 2);
for (size_t i = 0; i < 5; i++)
{
line(0, i * 150 + INTERVAL, WIDTH, i * 150 + INTERVAL); //画横线
line(i * 100, INTERVAL, i * 100, HEIGHT); //画竖线
}
//根据map绘制出黑块所在的位置
setfillcolor(BLACK);
for (size_t i = 0; i < 4; i++)
{
int x = map[i] * 100;
int y = i * 150 + INTERVAL;
//画一个黑色的矩形
fillrectangle(x, y, x + 100, y + 150);
}
//输出分数
settextstyle(20, 0, L"楷体");
settextcolor(BLACK);
//把int 转成 字符串用什么?
WCHAR str[20] = L"";
wsprintf(str, L"%d", score);
outtextxy(20, 20, str);
}
void move()
{
//清屏
cleardevice();
//移动
for (size_t i = 3; i > 0; i--)
{
map[i] = map[i - 1];
}
map[0] = rand() % 4;
}
//用鼠标点击黑块,实现所有快,往下移动
bool mouseEvent()
{
//获取鼠标消息
MOUSEMSG msg = GetMouseMsg();
if (msg.uMsg == WM_LBUTTONDOWN) //鼠标左键按下
{
//获取最下面的黑块的左上角坐标
int x = map[3] * 100;
int y = 3 * 150 + INTERVAL;
if (msg.x >= x && msg.x <= x + 100 && msg.y >= y && msg.y <= y + 150)
{
score += 10;
//move();
}
else
{
return false;
}
}
return true;
}
//街机模式
void loop()
{
Sleep(500); // 这个不太行,要自己写定时器最好
move();
}
void gameOver()
{
//弹出框
int isok =MessageBox(GetHWnd(), L"you lowser,是否继续?", L"hit", MB_OKCANCEL);
if (isok == IDOK)
{
printf("继续游戏~\\n");
}
else
{
printf("推出游戏~\\n");
}
}
int mian()
{
//图形,正方形,圆形.... 图片 EW_SHOWCONSOLE同时显示控制台
initgraph(WIDTH, HEIGHT,EW_SHOWCONSOLE);
//设置窗口背景颜色
setbkcolor(WHITE);
cleardevice();
gameInit();
while (true)
{
gameDraw();
if (!mouseEvent())
{
break;
}
loop();
}
//游戏结束处理
gameOver();
getchar();
closegraph();
return 0;
}
写在最后:对于准备学习C/C++编程的小伙伴,如果你想更好的提升你的编程核心能力(内功)不妨从现在开始!
C语言C++编程学习交流圈子,QQ群:829164294【点击进入】微信公众号:C语言编程学习基地
整理分享(多年学习的源码、项目实战视频、项目笔记,基础入门教程)
欢迎转行和学习编程的伙伴,利用更多的资料学习成长比自己琢磨更快哦!
编程学习视频分享:
以上是关于Pygame小游戏别踩钢琴块的主要内容,如果未能解决你的问题,请参考以下文章