pythonモバイルボードのミニゲームを実現しました。

13092 ワード

本論文の例では、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)
 TextSurf, TextRect = text_objects(text, largeText)
 TextRect.center = ((screen[0] / 2), (screen[1] / 2))
 screen.blit(TextSurf, TextRect)
 pygame.display.update()
 time.sleep(2)
 game_loop()
4.関連するカスタム関数
コードは以下の通りです

def game_first_win():
 intro = True
 while intro:
 for event in pygame.event.get():
  if event.type == pygame.QUIT:
  pygame.quit()
  quit()
 screen.fill(bg_color)
 ###    
 TextSurf, TextRect = text_objects('    ', midlText)
 TextRect.center = ((SCREEN_SIZE[0] / 2), (SCREEN_SIZE[1] / 2 - 70 ))
 screen.blit(TextSurf, TextRect)
 ###  
 TextSurf_ZZ, TextRect_ZZ = text_objects('AUTHOR:    ', smallText)
 TextRect_ZZ.center = ((SCREEN_SIZE[0] / 2), (SCREEN_SIZE[1] / 2 + 30))
 screen.blit(TextSurf_ZZ, TextRect_ZZ)
 button("  ", 60, 400, 100, 50, green, bright_green, game_loop)
 button("  ", 230, 400, 100, 50, red, bright_red, quitgame)
 pygame.display.update()
 clock.tick(15)

######          ######
def game_loop():
 pygame.mouse.set_visible(1) #        
 ###  ###
 score = 0 #  
 count_O = 0 #       1       
 count_N = 0 #       2       
 c_level = 1 #  

 x_change = 0 #     
 x = SCREEN_SIZE[0] // 2 - barsize[0] // 2
 y = SCREEN_SIZE[1] - barsize[1]

 # ball     
 ball_pos_pz = ball_pos
 while True:
 bar_move_left = False
 bar_move_right = False
 ###    X  ,    
 if count_O != count_N and score % 5 == 0:
  c_level += 1
 count_O = count_N
 ######        ######
 for event in pygame.event.get():
  if event.type == QUIT: #        
  pygame.quit()
  sys.exit() #             
  elif event.type == KEYDOWN:
  ##   Q    
  if event.key == pygame.K_q:
   time.sleep(10)
  ##   
  if event.key == pygame.K_LEFT:
   bar_move_left = True
   x_change = -30
  else:
   bar_move_left = False
  ##   
  if event.key == pygame.K_RIGHT:
   bar_move_right = True
   x_change = +30
  else:
   bar_move_right = False
  if event.key != pygame.K_LEFT and event.key != pygame.K_RIGHT:
   bar_move_left = False
   bar_move_right = False

  ##       
  if bar_move_left == True and bar_move_right == False:
  x += x_change
  if bar_move_left == False and bar_move_right == True:
  x += x_change

 ##    
 screen.blit(background, (0, 0)) # (0,0)        x   Y 
 ##         ,      
 bar_pos = pygame.Rect(x, y, barsize[0], BALL_SIZE[1])
 bar_pos.left = x
 pygame.draw.rect(screen, WHITE, bar_pos)

 ##    ,      
 ball_pos_pz.bottom += ball_dir_y * 3
 pygame.draw.rect(screen, WHITE, ball_pos_pz)

 ##          
 if bar_pos.top <= ball_pos_pz.bottom and (
  bar_pos.left <= ball_pos_pz.right and bar_pos.right >= ball_pos_pz.left):
  score += 1 #      1
  count_N += 1
 elif bar_pos.top <= ball_pos_pz.bottom and (
  bar_pos.left > ball_pos_pz.right or bar_pos.right < ball_pos_pz.left):
  print("Game Over: ", score)
  return score

 ##           
 if bar_pos.top <= ball_pos_pz.bottom:
  ball_x = random.randint(0, SCREEN_SIZE[0] - BALL_SIZE[0])
  ball_pos_pz = pygame.Rect(ball_x, ball_pos_y, BALL_SIZE[0], BALL_SIZE[1])

 #########        #########
 TextSurf_lev, TextRect_lev = text_objects("   : " + str(c_level), smallText)
 TextRect_lev.center = (60, 20)
 screen.blit(TextSurf_lev, TextRect_lev)

 #########        #########
 TextSurf_sco, TextRect_sco = text_objects("   : " + str(score), smallText)
 TextRect_sco.center = (60, 50)
 screen.blit(TextSurf_sco, TextRect_sco)

 pygame.display.update() #         
 clock.tick(60)
ヽoo。ツ
コードは以下の通りです

###### AUTHOR:     ######
###### DATE:20201002 ######
###### DESCRIPTION:      ######
import pygame
from pygame.locals import *
import sys
import time
import random

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)

######       ######
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)
 TextSurf, TextRect = text_objects(text, largeText)
 TextRect.center = ((screen[0] / 2), (screen[1] / 2))
 screen.blit(TextSurf, TextRect)
 pygame.display.update()
 time.sleep(2)
 game_loop()

def game_first_win():
 intro = True
 while intro:
 for event in pygame.event.get():
  if event.type == pygame.QUIT:
  pygame.quit()
  quit()
 screen.fill(bg_color)
 ###    
 TextSurf, TextRect = text_objects('    ', midlText)
 TextRect.center = ((SCREEN_SIZE[0] / 2), (SCREEN_SIZE[1] / 2 - 70 ))
 screen.blit(TextSurf, TextRect)
 ###  
 TextSurf_ZZ, TextRect_ZZ = text_objects('AUTHOR:    ', smallText)
 TextRect_ZZ.center = ((SCREEN_SIZE[0] / 2), (SCREEN_SIZE[1] / 2 + 30))
 screen.blit(TextSurf_ZZ, TextRect_ZZ)
 button("  ", 60, 400, 100, 50, green, bright_green, game_loop)
 button("  ", 230, 400, 100, 50, red, bright_red, quitgame)
 pygame.display.update()
 clock.tick(15)

######          ######
def game_loop():
 pygame.mouse.set_visible(1) #        
 ###  ###
 score = 0 #  
 count_O = 0 #       1       
 count_N = 0 #       2       
 c_level = 1 #  

 x_change = 0 #     
 x = SCREEN_SIZE[0] // 2 - barsize[0] // 2
 y = SCREEN_SIZE[1] - barsize[1]

 # ball     
 ball_pos_pz = ball_pos
 while True:
 bar_move_left = False
 bar_move_right = False
 ###    X  ,    
 if count_O != count_N and score % 5 == 0:
  c_level += 1
 count_O = count_N
 ######        ######
 for event in pygame.event.get():
  if event.type == QUIT: #        
  pygame.quit()
  sys.exit() #             
  elif event.type == KEYDOWN:
  ##   Q    
  if event.key == pygame.K_q:
   time.sleep(10)
  ##   
  if event.key == pygame.K_LEFT:
   bar_move_left = True
   x_change = -30
  else:
   bar_move_left = False
  ##   
  if event.key == pygame.K_RIGHT:
   bar_move_right = True
   x_change = +30
  else:
   bar_move_right = False
  if event.key != pygame.K_LEFT and event.key != pygame.K_RIGHT:
   bar_move_left = False
   bar_move_right = False

  ##       
  if bar_move_left == True and bar_move_right == False:
  x += x_change
  if bar_move_left == False and bar_move_right == True:
  x += x_change

 ##    
 screen.blit(background, (0, 0)) # (0,0)        x   Y 
 ##         ,      
 bar_pos = pygame.Rect(x, y, barsize[0], BALL_SIZE[1])
 bar_pos.left = x
 pygame.draw.rect(screen, WHITE, bar_pos)

 ##    ,      
 ball_pos_pz.bottom += ball_dir_y * 3
 pygame.draw.rect(screen, WHITE, ball_pos_pz)

 ##          
 if bar_pos.top <= ball_pos_pz.bottom and (
  bar_pos.left <= ball_pos_pz.right and bar_pos.right >= ball_pos_pz.left):
  score += 1 #      1
  count_N += 1
 elif bar_pos.top <= ball_pos_pz.bottom and (
  bar_pos.left > ball_pos_pz.right or bar_pos.right < ball_pos_pz.left):
  print("Game Over: ", score)
  return score

 ##           
 if bar_pos.top <= ball_pos_pz.bottom:
  ball_x = random.randint(0, SCREEN_SIZE[0] - BALL_SIZE[0])
  ball_pos_pz = pygame.Rect(ball_x, ball_pos_y, BALL_SIZE[0], BALL_SIZE[1])

 #########        #########
 TextSurf_lev, TextRect_lev = text_objects("   : " + str(c_level), smallText)
 TextRect_lev.center = (60, 20)
 screen.blit(TextSurf_lev, TextRect_lev)

 #########        #########
 TextSurf_sco, TextRect_sco = text_objects("   : " + str(score), smallText)
 TextRect_sco.center = (60, 50)
 screen.blit(TextSurf_sco, TextRect_sco)

 pygame.display.update() #         
 clock.tick(60)

####      ######
game_first_win()
game_loop()
pygame.quit()
以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。