用pygame,做一个小小小小小........游戏

根据键盘方向键控制“不知火舞”的上下左右移动

  其实我对Python能不能做大型游戏,还是执质疑态度。Python属于高级语言,那么不可避免的是,它的运行速度没有C语言快。机器语言到汇编语言,到面向过程再到面向对象,它们在电脑上的执行速度由快到慢,而游戏讲究的就是快!一定要快!所以我觉得,Python,不适用于写大型游戏。有的人不服了,Python语言是有C语言开发的,而C语言是做游戏的最有优势的语言,所以Python也能做?

  不可否认的是,Python的确有先天性的优势,然而这些优势并不体现在做游戏上面,它在网页上面的优势更大,Python的爬虫等。但是,做些小游戏,Python是肯定不在话下的。pygame官方给的帧数为40~200,而我们人类肉眼能分辨的帧数不超过30,所以你是不会在玩着游戏的时候,一卡一顿的。

  

import pygame
import sys
from pygame.locals import *

#初始化Pygame
pygame.init()

size = width,hight = 600,400
speed = [-2,1]
bg = (255,200,255) #RGB颜色

#clock = pygame.time.Clock()
#创建指定大写的窗口
screen = pygame.display.set_mode(size)
#设置窗口标题
pygame.display.set_caption('不知火舞')

#加载图片
turtle = pygame.image.load('turtle.png')
#获得图像的位置矩形
position = turtle.get_rect()
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]
    #移动图像
    position = position.move(speed)

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

    if position.top < 0 or position.bottom > hight:
        speed[1] = -speed[1]

    #填充背景
    screen.fill(bg)
    #双缓冲
    #更新图像
    screen.blit(turtle,position)#bilt方法将一个图像覆盖到另一个图象上
    #更新界面
    pygame.display.flip()
    #延迟10毫秒
    pygame.time.delay(10)

    #clock.tick(200)
    
#什么是surface对象?
#pygame 用来表示图像的对象

额,很简单的一个东西。基本没什么难度,但重在坚持嘛!万一做着做着就特么做了一个大大大大大大.....游戏了呢?

(有了这些我觉得我可以先做个贪吃蛇来玩玩?)

很想高飞,但我不能;不想天空,剩我一人。
原文地址:https://www.cnblogs.com/lixiansheng/p/6940519.html