首页 > 其他分享 >小游戏

小游戏

时间:2023-12-17 22:56:47浏览次数:29  
标签:Pipeline screen Bird 小游戏 pygame font self

制作一个跳跃的小球游戏(Pygame基本使用)

import sys    
import pygame
pygame.init()
size = width, height = 640, 480
screen = pygame.display.set_mode(size)
color = (0, 0, 0)
ball = pygame.image.load("ball123.png")
ballrect = ball.get_rect()
speed = [1, 1]
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
ballrect = ballrect.move(speed)
if ballrect.left < 0 or ballrect.right > width:
speed[0] = -speed[0]
if ballrect.top < 0 or ballrect.bottom > height:
speed[1] = -speed[1]
screen.fill(color)
screen.blit(ball, ballrect)
pygame.display.flip()
pygame.quit()

 

开发Flappy Bird游戏

import pygame
import sys
import random

class Bird(object):
def __init__(self):
self.birdRect = pygame.Rect(65, 50, 50, 50)
self.birdStatus = [pygame.image.load("bird.png"),
pygame.image.load("bird1.png"),
pygame.image.load("bird2.png")]
self.status = 0
self.birdX = 120
self.birdY = 350
self.jump = False
self.jumpSpeed = 10
self.gravity = 5
self.dead = False

def birdUpdate(self):
if self.jump:
self.jumpSpeed -= 1
self.birdY -= self.jumpSpeed
else:
self.gravity += 0.2
self.birdY += self.gravity
self.birdRect[1] = self.birdY

class Pipeline(object):
def __init__(self):
self.wallx = 400
self.pineUp = pygame.image.load("pipe1.png")
self.pineDown = pygame.image.load("pipe.png")

def updatePipeline(self):
self.wallx -= 5
if self.wallx < -80:
global score
score += 1
self.wallx = 400

def createMap(screen, background, font):
screen.fill((255, 255, 255))
screen.blit(background, (0, 0))
screen.blit(Pipeline.pineUp, (Pipeline.wallx, -100))
screen.blit(Pipeline.pineDown, (Pipeline.wallx, 500))
Pipeline.updatePipeline()
if Bird.dead:
Bird.status = 2
elif Bird.jump:
Bird.status = 1
screen.blit(Bird.birdStatus[Bird.status], (Bird.birdX, Bird.birdY))
Bird.birdUpdate()
screen.blit(font.render("score: " + str(score), -1, (255, 255, 255)), (230, 20))

pygame.display.update()

def checkDead():
upRect = pygame.Rect(Pipeline.wallx, -100,Pipeline.pineUp.get_width() - 10, Pipeline.pineUp.get_height())
downRect = pygame.Rect(Pipeline.wallx, 500, Pipeline.pineDown.get_width() - 10, Pipeline.pineDown.get_height())
if upRect.colliderect(Bird.birdRect) or downRect.colliderect(Bird.birdRect):
Bird.dead = True
return True
else:
return False

def getResult():
final_text1 = "Game over"
final_text2 = "Your final score is: " + str(score)
ft1_font = pygame.font.SysFont("Arial", 70)
ft1_surf = ft1_font.render(final_text1, 1, (242, 3, 36))
ft2_font = pygame.font.SysFont("Arial", 50)
ft2_surf = ft2_font.render(final_text2, 1, (253, 177, 6))
screen.blit(ft1_surf, [screen.get_width() / 2 - ft1_surf.get_width() / 2, 100])
screen.blit(ft2_surf, [screen.get_width() / 2 - ft2_surf.get_width() / 2, 200])
pygame.display.flip()

if __name__ == "__main__":
pygame.init()
pygame.font.init()
font = pygame.font.SysFont(None, 50)
size = width, height = 400, 680
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()
Pipeline = Pipeline()
Bird = Bird()
score = 0
while True:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if (event.type == pygame.KEYDOWN or event.type == pygame.MOUSEBUTTONDOWN) and not Bird.dead:
Bird.jump = True
Bird.gravity = 5
Bird.jumpSpeed = 10
background = pygame.image.load("picture.png")
if checkDead():
getResult()
else:
createMap(screen, background, font)
pygame.quit()


 

 

标签:Pipeline,screen,Bird,小游戏,pygame,font,self
From: https://www.cnblogs.com/CCaii/p/17910047.html

相关文章

  • C语言—猜数字小游戏
    #include<stdio.h>#include<time.h>#include<stdlib.h>voidmenu(){ printf("########################\n"); printf("#####1.play0.exit#####\n"); printf("########################\n");}voidgame(){......
  • 【开源】贪吃蛇小游戏
    #include<bits/stdc++.h>//清屏:system("cls"); 1:'◎'2:'⊙'3:'▲'4:'◆'5:'■'#include<windows.h>//停顿:Sleep();#include<conio.h>usingnamespacestd;inta[100][100],dir[100......
  • python——小游戏(ball,bird)
      ball #-*-coding:utf-8-*-"""CreatedonWedDec1309:19:382023@author:kabuqinuo"""importsys#导入sys模块importpygame#导入pygame模块pygame.init()#初始化pygamesize=width,height=640,480#设置窗......
  • 耐心极限大挑战,整蛊小游戏之「禁止向上走」【玩转Web小游戏】
    故事是这样开始的很久很久以前,我关注的一个游戏博主,发了一个游戏视频。然后我就见识到了什么叫,「游戏叫你一步噶,你绝对走不到第二步」。这个带那么点整蛊的性质的脑洞游戏,瞬间引起了我浓厚的兴趣。需要玩家克服大脑常规套路的惯性,那岂不是游戏处处是惊喜。不过,游戏的本质还是在于趣......
  • Unity中实现简单的弹反小游戏
    最近开发了一个小游戏,在其中实现了简单的弹反效果。在敌人的剑上绑定一个boxcollider,同时勾选isTrigger:保证玩家有一个胶囊碰撞体。1、敌人代码随后我在控制敌人的代码(enemy.cs)中加入下面的函数控制弹反://进入剑触发器voidOnTriggerEnter(Collidercollider)......
  • 学习记录:用python实现井字棋小游戏
    1.实现双人井字棋 创建3*3矩阵 制作下棋函数 制作判断获胜函数 2.实现简单ai 在输入下棋位置后自动在空位下棋 3.复杂化 自动寻找下后获胜的位置 若无则判断对手是否即将获胜并进行堵截 仍无则在随机空位下棋 新手上路出现了很多啼笑皆非的bug 诸如在print下方出现N......
  • 微信小游戏开发怎么选游戏引擎
    微信小游戏现在非常的火,当我们下定决心做微信小游戏开发的时候,面临我们的第一个问题是怎么选一个H5的游戏引擎,那么今天本小编就来给大家分析一下目前能开发各大平台H5小游戏的游戏引擎和它们的优缺点。入选原则:(1)能一次开发,同时发布到多个游戏平台的游戏引擎;(android,IOS,......
  • 猜年龄小游戏
    【一】需求介绍设定好用户年龄,用户通过输入猜测的年龄进行匹配最大尝试次数:用户最多尝试猜测3次最大尝试次数后:如3次后,问用户是否还想继续玩如果回答Y或y,就再给3次机会,提示【还剩最后三次机会】3次都猜错的话游戏结束如果回答N或n,游戏结束!如果格式输入错误,提示【输入格式......
  • <推箱子>小游戏隐私协议
    <推箱子>小游戏隐私协议欢迎您使用<臣妾要告发熹贵妃工作室>开发的<推箱子>小游戏!在使用本游戏之前,请您仔细阅读以下隐私协议。个人信息的收集与使用1为了提供更好的游戏体验和服务,我们可能会收集一些您的个人信息,例如您的设备标识符、操作系统版本、游戏进度等。2我们承诺不......
  • windows 微信小游戏
    有好几年没怎么使用Windows系统了,Windows版本微信居然小程序和小游戏了,这个确实会方便很多用户。总体来说是不错的。对我来说,这个非常不安全,非常容易制作辅助和外挂,感觉现在小程序Windows版本的辅助应该满天飞了吧 我现在简单记录一下小游戏存储路径,搞一些小动作需要知道的......