资源简介
KongLong Run是一款难度低的休闲软件,旨在放松之余为大众提供娱乐的目的。操作简单,轻松体验游戏,也作者的初衷。
用pycharm写的一个小游戏,记得要安装 pygame
代码片段和文件信息
import pygame
from pygame.locals import *
import os
import sys
import random
# set the game window at the center of the display
os.environ[‘SDL_VIDEO_CENTERED‘] = ‘1‘
# defining some global variables
SCREEN_WIDTH = 1280
SCREEN_HEIGHT = 720
FPS = 30
GROUND_HEIGHT = SCREEN_HEIGHT - 70
PLAY_GAME = True
# initialize pygame and create window
pygame.init()
window = pygame.display.set_mode((SCREEN_WIDTH SCREEN_HEIGHT))
pygame.display.set_caption(“KongLong Run“)
clock = pygame.time.Clock()
# load audio files
jump_sound = pygame.mixer.Sound(“sound/jump.ogg“)
score_sound = pygame.mixer.Sound(“sound/score.ogg“)
game_over_sound = pygame.mixer.Sound(“sound/game_over.ogg“)
# function for drawing text on the screen
def draw_text(text font_name size text_color position_x position_y position):
font = pygame.font.Font(font_name size) # loads font
text_plane = font.render(text True text_color) # renders given text in the selected font
text_rect = text_plane.get_rect()
# setting text position
if position == “midtop“:
text_rect.midtop = (position_x position_y)
elif position == “topright“:
text_rect.topright = (position_x position_y)
window.blit(text_plane text_rect) # draws the rendered text on the screen
# function for loading single image file
def load_image(path size_x=0 size_y=0):
image = pygame.image.load(path).convert_alpha() # loads image file and converts it into pixels
if size_x > 0 and size_y > 0:
image = pygame.transform.scale(image (size_x size_y)) # resizing the image to the given size
return image image.get_rect()
# function for loading multiple image files in a list
def load_sprites(image_path image_name_prefix number_of_image size_x=0 size_y=0):
images = [] # declaring list to store the images
for i in range(0 number_of_image):
path = image_path + image_name_prefix + str(i) + “.png“ # creating the path string
image = pygame.image.load(path).convert_alpha() # loads image file and converts it into pixels
if size_x > 0 and size_y > 0:
image = pygame.transform.scale(image (size_x size_y)) # resizing the image to the given size
images.append(image)
return images
# class for creating and moving single background
class Background:
def __init__(self image_path speed=10):
self.image0 self.rect0 = load_image(image_path 1280 720)
self.image1 self.rect1 = load_image(image_path 1280 720)
self.rect0.bottom = SCREEN_HEIGHT
self.rect1.bottom = SCREEN_HEIGHT
self.rect1.left = self.rect0.right
self.speed = speed
def draw(self):
window.blit(self.image0 self.rect0)
window.blit(self.image1 self.rect1)
def update(self):
self.rect0.left -= self.speed
self.rect1.left -= self.speed
if self.rect0.right < 0:
self.rect0.left = self.rect1.right
if self.rect1.righ
- 上一篇:快速入门python培训教程.ppt(63页)
- 下一篇:python 照片人脸识别
评论
共有 条评论