首页 > 其他分享 >小游戏

小游戏

时间:2023-12-17 22:57:53浏览次数:24  
标签:ball self pipe 小游戏 pygame image rect

fly bird游戏


import pygame
import random


pygame.init()


WIDTH = 288
HEIGHT = 512
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Flappy Bird')

background_img = pygame.image.load('background.png').convert()
bird_img = pygame.image.load('bird.png').convert_alpha()
pipe_img = pygame.image.load('pipe.png').convert_alpha()
game_over_img = pygame.image.load('gameover.png').convert_alpha()
flap_sound = pygame.mixer.Sound('_aigei_com.mp3')
hit_sound = pygame.mixer.Sound('_aigei_com2.mp3')


FPS = 60
GRAVITY = 0.25
JUMP_SPEED = -4
PIPE_GAP = 100
PIPE_VELOCITY = -2
INITIAL_PIPE_X = WIDTH + 20
SCORE_FONT = pygame.font.Font(None, 40)

class Bird:
def __init__(self):
self.x = 50
self.y = HEIGHT // 2
self.velocity = 0
self.image = bird_img
self.rect = self.image.get_rect()
self.rect.center = (self.x, self.y)

def update(self):

self.velocity += GRAVITY
self.y += self.velocity
self.rect.center = (self.x, self.y)

def jump(self):

self.velocity = JUMP_SPEED
flap_sound.play()

def draw(self):

screen.blit(self.image, self.rect)

class Pipe:
def __init__(self, x):
self.x = x
self.y = random.randint(HEIGHT // 4, HEIGHT // 2)
self.image = pipe_img
self.rect_top = self.image.get_rect(topleft=(self.x, self.y - PIPE_GAP))
self.rect_bottom = self.image.get_rect(topleft=(self.x, self.y + PIPE_GAP))

def update(self):

self.x += PIPE_VELOCITY
self.rect_top.topleft = (self.x, self.y - PIPE_GAP)
self.rect_bottom.topleft = (self.x, self.y + PIPE_GAP)

def offscreen(self):

return self.x < -self.rect_top.width

def draw(self):

screen.blit(self.image, self.rect_top)
screen.blit(pygame.transform.flip(self.image, False, True), self.rect_bottom)

class Score:
def __init__(self):
self.value = 0
self.font = SCORE_FONT
self.color = pygame.Color('white')
self.x = WIDTH // 2
self.y = 50

def increment(self):

self.value += 1

def draw(self):

score_text = self.font.render(str(self.value), True, self.color)
score_rect = score_text.get_rect(center=(self.x, self.y))
screen.blit(score_text, score_rect)


bird = Bird()
pipes = [Pipe(INITIAL_PIPE_X)]
score = Score()


clock = pygame.time.Clock()
game_running = True
game_over = False

while game_running:

for event in pygame.event.get():
if event.type == pygame.QUIT:
game_running = False
elif event.type == pygame.KEYDOWN:
if not game_over and (event.key == pygame.K_SPACE or event.key == pygame.K_UP):
bird.jump()


if not game_over:
bird.update()
score.draw()
score.increment()
pipes_inside_screen = [pipe for pipe in pipes if not pipe.offscreen()]
for pipe in pipes_inside_screen:
pipe.update()
if len(pipes) < 2 or pipes[-1].x < WIDTH - INITIAL_PIPE_X:
pipes.append(Pipe(INITIAL_PIPE_X))
top_pipe_collider = bird.rect.colliderect(pipes[0].rect_top)
bottom_pipe_collider = bird.rect.colliderect(pipes[0].rect_bottom)
if top_pipe_collider or bottom_pipe_collider or bird.y > HEIGHT:
hit_sound.play()
game_over = True


screen.blit(background_img, (0, 0))
bird.draw()
for pipe in pipes:
pipe.draw()
if game_over:
screen.blit(game_over_img, ((WIDTH - game_over_img.get_width()) // 2, (HEIGHT - game_over_img.get_height()) // 2))


pygame.display.flip()
clock.tick(FPS)


pygame.quit()

 

会动的球游戏
import pygame
import sys


pygame.init()


width, height = 800, 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Simple Game")


white = (255, 255, 255)
blue = (0, 0, 255)


ball_radius = 50
ball_x, ball_y = width // 2, height // 2
ball_dx, ball_dy = 5, 5


running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False


ball_x += ball_dx
ball_y += ball_dy


if ball_x + ball_radius > width or ball_x - ball_radius < 0:
ball_dx *= -1
if ball_y + ball_radius > height or ball_y - ball_radius < 0:
ball_dy *= -1


screen.fill(white)


pygame.draw.circle(screen, blue, (ball_x, ball_y), ball_radius)


pygame.display.flip()


pygame.time.delay(30)


pygame.quit()
sys.exit()

标签:ball,self,pipe,小游戏,pygame,image,rect
From: https://www.cnblogs.com/wqx3121343019/p/17910031.html

相关文章

  • 小游戏
    制作一个跳跃的小球游戏(Pygame基本使用)importsysimportpygamepygame.init()size=width,height=640,480screen=pygame.display.set_mode(size)color=(0,0,0)ball=pygame.image.load("ball123.png")ballrect=ball.get_rect()s......
  • 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我们承诺不......