pygame实现一个简单的躲避类游戏
程序员文章站
2022-03-16 08:25:25
...
'''
AUTHOR: vforlove
CREATED: 2018-08-07
IDE:PyCharm
version:3.6.6
'''
import pygame
import random
import time
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((600, 800))
background = pygame.image.load('background.jpg').convert()
start = pygame.image.load('start.jpg').convert()
RUNNING = True
score = 0
pygame.display.set_caption('这是一个小游戏,真的')
my_font = pygame.font.SysFont('arial', 60)
font = pygame.font.SysFont('arial', 40)
class Player(pygame.sprite.Sprite):
def __init__(self):
super(Player, self).__init__()
self.image = pygame.image.load('plane.jpg').convert()
self.image.set_colorkey((255, 255, 255), RLEACCEL)
self.rect = self.image.get_rect()
self.rect.topleft = (250, 800)
def update(self, key_value):
if key_value == 'K_UP':
self.rect.move_ip(0, -5)
if key_value == 'K_DOWN':
self.rect.move_ip(0, 5)
if key_value == 'K_LEFT':
self.rect.move_ip(-5, 0)
if key_value == 'K_RIGHT':
self.rect.move_ip(5, 0)
def key_update(self, pressed_keys):
if pressed_keys[K_UP]:
self.rect.move_ip(0, -1)
if pressed_keys[K_DOWN]:
self.rect.move_ip(0, 1)
if pressed_keys[K_LEFT]:
self.rect.move_ip(-1, 0)
if pressed_keys[K_RIGHT]:
self.rect.move_ip(1, 0)
if self.rect.top <= 0:
self.rect.top = 0
if self.rect.left <= 0:
self.rect.left = 0
if self.rect.right >= 600:
self.rect.right = 600
if self.rect.bottom >= 800:
self.rect.bottom = 800
class Enemy(pygame.sprite.Sprite):
def __init__(self):
super(Enemy, self).__init__()
self.image = pygame.image.load('enemy.jpg').convert()
self.image.set_colorkey((255, 255, 255), RLEACCEL)
self.rect = self.image.get_rect(center=(random.randint(0, 600), 0))
self.speed = random.randint(1, 2)
def update(self):
global score
self.rect.move_ip(0, self.speed)
if self.rect.bottom > 800:
self.kill()
score += 1
ADDENEMY = pygame.USEREVENT + 1
pygame.time.set_timer(ADDENEMY, 250)
mouse_index = 1
while 1:
screen.blit(background, (0, 0))
pygame.display.flip()
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
print('stop running!')
exit()
elif event.type == QUIT:
exit()
elif event.type == MOUSEBUTTONDOWN:
pressed_array = pygame.mouse.get_pressed()
for index in range(len(pressed_array)):
if pressed_array[index]:
if index == 0:
mouse_index = 0
elif event.type == MOUSEMOTION:
pos = pygame.mouse.get_pos()
mouse_x = pos[0]
mouse_y = pos[1]
if (mouse_index == 0) and (mouse_x > 166) and (mouse_x < 420) and (mouse_y > 385) and (mouse_y < 490):
enemies = pygame.sprite.Group()
all_sprites = pygame.sprite.Group()
player = Player()
all_sprites.add(player)
while RUNNING:
screen.blit(start, (0, 0))
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
print('stop running!')
exit()
'''
elif event.key == K_LEFT:
player.update('K_LEFT')
elif event.key == K_RIGHT:
player.update('K_RIGHT')
elif event.key == K_UP:
player.update('K_UP')
elif event.key == K_DOWN:
player.update('K_DOWN')
'''
elif event.type == QUIT:
exit()
elif event.type == ADDENEMY:
new_enemy = Enemy()
enemies.add(new_enemy)
all_sprites.add(new_enemy)
pressed_keys = pygame.key.get_pressed()
player.key_update(pressed_keys)
screen.blit(font.render('score:%s' % score, True, (0, 0, 0)), (0, 0))
#screen.blit(player.surf, player.rect)
enemies.update()
for all_sprite in all_sprites:
screen.blit(all_sprite.image, all_sprite.rect)
#pygame.draw.rect(screen, (255, 255, 255), player.rect)
if pygame.sprite.spritecollideany(player, enemies):
player.kill()
screen.blit(my_font.render('YOU DEAD!', True, (0, 0, 0)), (180, 400))
pygame.display.update()
time.sleep(2)
RUNNING = False
#print(player.rect)
pygame.display.flip()
mouse_index = 1
RUNNING = True
score = 0
上一篇: 小甲鱼Pygame84讲:基本图形绘制(总结笔记)
下一篇: MySQL 事务