zoukankan      html  css  js  c++  java
  • Python-day6

    # 改改游戏小程序
    import pygame
    from pygame.locals import *
    from sys import exit

    pygame.init() # 行初始化
    bgsize = width, height = 1920, 1080
    pygame.display.set_mode(bgsize)
    pygame.display.set_caption('进击中的格雷')
    bg = (80, 160, 255) # 置背景
    dog_image = pygame.image.load('./bianmu.jpeg')
    ele_image = pygame.image.load('./elephent.jpeg')

    dog_image = pygame.transform.scale(dog_image, (width // 10, height // 6)) # 对图
    ele_image = pygame.transform.scale(ele_image, (width // 10, height // 6)) # 对图
    screen = pygame.display.get_surface()
    new_dog_image = pygame.transform.scale(dog_image, (width // 10, height // 6)) # 对图



    class Dog(pygame.sprite.Sprite):
    def __init__(self, up_speed, down_speed):
    pygame.sprite.Sprite.__init__(self)
    self.up_speed = up_speed
    self.down_speed = down_speed
    self.left_speed = -30
    self.image = dog_image
    self.rect = self.image.get_rect()
    self.rect.top = 0
    self.rect.left = (width - self.image.get_width()) // 2

    def moveup(self):
    self.rect.top -= self.up_speed
    global new_dog_image
    new_dog_image = pygame.transform.rotate(self.image, 0)

    def movedown(self):
    self.rect.top += self.down_speed

    def moveleft(self):
    self.rect.left -= 20
    global new_dog_image
    new_dog_image = pygame.transform.rotate(self.image, 90)
    width, height = new_dog_image.get_size()
    new_dog_image = pygame.transform.scale(new_dog_image, (width // 3 * 2, height // 3 * 2)) # 对图

    print(self.rect.left)
    def moveright(self):
    self.rect.left += 20
    global new_dog_image
    new_dog_image = pygame.transform.rotate(self.image, 270)
    width, height = new_dog_image.get_size()
    new_dog_image = pygame.transform.scale(new_dog_image, (width * 3 // 2, height * 3 // 2)) # 对图


    dogGrey = Dog(30, 6)
    clock = pygame.time.Clock()
    while 1:
    for event in pygame.event.get():
    if event.type == QUIT:
    exit()
    key_press = pygame.key.get_pressed()
    if key_press[K_q]:
    break
    elif key_press[K_LEFT] and dogGrey.rect.top > 0:
    dogGrey.moveleft()
    elif key_press[K_RIGHT] and dogGrey.rect.top > 0:
    dogGrey.moveright()
    elif key_press[K_UP] and dogGrey.rect.top > 0:
    dogGrey.moveup()
    elif not key_press[K_UP] and dogGrey.rect.bottom < height:
    dogGrey.movedown()

    screen.fill(bg)
    screen.blit(new_dog_image, dogGrey.rect)
    clock.tick(25)
    pygame.display.flip() # 更新示到屏幕上
     
  • 相关阅读:
    jQuery命名空间,自定义空间及属性,插件开发全解析 (转)
    打印输入表单中的内容
    js 网页烟花效果
    js学习cookie封装之获取
    js学习cookie封装之添加
    git基本使用
    最简单的手风琴效果
    js学习cookie封装之删除
    21个值得收藏的javas技巧
    javascript常用特效汇总
  • 原文地址:https://www.cnblogs.com/augustone/p/11219365.html
Copyright © 2011-2022 走看看