python实现一个简单小游戏

import pygame
import sys
from pygame.locals import *

# 初始化Pygame
pygame.init()

size = width, height = 1024, 768
speed = [-2, 1]
bg = (255, 255, 255) # RGB
#False表示不全屏True表示全屏
fullscreen = False

# 创建指定大小的窗口 Surface
screen = pygame.display.set_mode(size,RESIZABLE)
# 设置窗口标题
pygame.display.set_caption("初次见面,请大家多多关照!")
#设置放大缩小的比率
ratio = 1.0


# 加图片
oturtle = pygame.image.load("turtle.png")
turtle = oturtle
# 获得图像的位置矩形
oturtle_rect = oturtle.get_rect()
position = turtle_rect = oturtle_rect
#走
oturtle_right = pygame.transform.rotate(oturtle, 90)
oturtle_top = pygame.transform.rotate(oturtle, 180)
oturtle_left = pygame.transform.rotate(oturtle, 270)
oturtle_bottom = oturtle

#默认头像左转
l_head = turtle
#表示头向右转,这里调用了翻转图像
r_head = pygame.transform.flip(turtle, True, False)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        #按键控制图像的方向
        if event.type == KEYDOWN:
            if event.key == K_LEFT:
                turtle = l_head
                speed = [-1, 0]
            if event.key == K_RIGHT:
                turtle = r_head
                speed = [1, 0]
            if event.key == K_UP:
                speed = [0,-1]
            if event.key == K_DOWN:
                speed = [0, 1]

            # 全屏(F11)
            if event.key == K_F11:
                fullscreen = not fullscreen
                if fullscreen:
                    screen = pygame.display.set_mode((1024, 768), FULLSCREEN | HWSURFACE)   #hwsurface开启硬件加速
                else:
                    screen = pygame.display.set_mode(size)
            #退出(Esc)           
            if event.key == K_ESCAPE:
                sys.exit()
            #放大 缩小小乌龟(=。-),空格恢复原始大小
            if event.key == K_EQUALS or event.key ==  K_MINUS or event.key == K_SPACE:
                #最大只能放大一倍,缩小50%
                if event.key == K_EQUALS and ratio < 2:
                    ratio +=0.1
                if event.key == K_MINUS and ratio >0.25:
                    ratio -= 0.1
                if event.key == K_SPACE:
                    ratio = 1.0
                turtle = pygame.transform.smoothscale(oturtle,\
                                                      (int(oturtle_rect.width * ratio),\
                                                       int(oturtle_rect.height * ratio)))
                l_head = turtle
                r_head = pygame.transform.flip(turtle, True, False)
                

        # 用户调整窗口尺寸
        if event.type == VIDEORESIZE:
            size = event.size
            width,height = size
            print(size)
            screen = pygame.display.set_mode(size,RESIZABLE)
            
    # 移动图像
    position = position.move(speed)


    #走2
    if position.right > width:
        turtle = oturtle_right
        position = oturtle_rect = oturtle.get_rect()
        position.left = width - oturtle_rect.width
        speed = [0, 5]

    if position.bottom > height:
        turtle = oturtle_bottom
        position = oturtle_rect = oturtle.get_rect()
        position.left = width - oturtle_rect.width
        position.top = height - oturtle_rect.height
        speed = [-5, 0]

    if position.left < 0:
        turtle = oturtle_left
        position = oturtle_rect = oturtle.get_rect()
        position.top = height - oturtle_rect.height
        speed = [0, -5]

    if position.top < 0:
        turtle = oturtle_top
        position = oturtle_rect = oturtle.get_rect()
        speed = [5, 0]

    if position.left < 0 or position.right > width:
        # 翻转图像
        turtle = pygame.transform.flip(turtle, True, False)
        # 反方向移动
        speed[0] = -speed[0]

    if position.top < 0 or position.bottom > height:
        # 翻转图像
        turtle = pygame.transform.flip(turtle, True, False)
        speed[1] = -speed[1]

    # 填充背景
    screen.fill(bg)
    # 更新图像
    screen.blit(turtle, position)
    # 更新界面
    pygame.display.flip()
    # 延迟10毫秒
    pygame.time.delay(10)

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值