# 改改游戏小程序
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() # 将更新显示到屏幕上