import pygame
from pygame.locals import *
import sys
from player import Hero
from level import MapTotal
from enemy import Enemy
from food import Food
BLACK = (0, 0, 0)
WINDOWHEIGHT = 480
WINDOWWIDTH = 540
FPS = 30
pygame.init()
clock = pygame.time.Clock()
SCREEN = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
pygame.display.set_caption('吃豆子')
over_game = False
tip_str = 0
def draw_font(screen, center, size, width, height):
global over_game, tip_str
game_tip_font = pygame.font.SysFont('None', size)
render = game_tip_font.render(str(center), False, 'red')
font_get = render.get_rect()
# 字体居中显示
length_font = font_get.right - font_get.left
height_font = font_get.bottom - font_get.top
center_x = int((width - length_font) / 2)
center_y = int((height - height_font) / 2)
font_get.left, font_get.top = center_x, center_y
mouse_x = pygame.mouse.get_pos()[0]
mouse_y = pygame.mouse.get_pos()[1]
if font_get.left <= mouse_x <= font_get.right and font_get.top <= mouse_y <= font_get.bottom:
click_mouse = pygame.mouse.get_pressed()
if click_mouse[0]:
over_game = False
tip_str = 0
main()
screen.blit(render, font_get)
def main():
global over_game, tip_str
map_total = MapTotal()
map_total.map_level()
# 横走
enemy_1 = Enemy(280, 10, 1)
# 竖走
enemy_2 = Enemy(75, 400, 2, 90)
# 上下左右移动
enemy_3 = Enemy(340, 145, 3)
# 上下左右移动
enemy_4 = Enemy(330, 240, 3)
# 上下左右移动
enemy_5 = Enemy(190, 160, 3, 30)
group_enemy = pygame.sprite.Group()
group_enemy.add(enemy_1)
group_enemy.add(enemy_2)
group_enemy.add(enemy_3)
group_enemy.add(enemy_4)
group_enemy.add(enemy_5)
sprite_hero = Hero((WINDOWWIDTH, WINDOWHEIGHT))
sprite_food = Food(235, 240)
while True:
SCREEN.fill(BLACK)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
old_x, old_y = sprite_hero.rect.topleft
if not over_game:
key_lists = pygame.key.get_pressed()
if key_lists[K_w]:
sprite_hero.move_top()
if key_lists[K_s]:
sprite_hero.move_down()
if key_lists[K_a]:
sprite_hero.move_left()
if key_lists[K_d]:
sprite_hero.move_right()
if pygame.sprite.spritecollide(sprite_hero, map_total.map_group, False):
sprite_hero.rect.topleft = old_x, old_y
if pygame.sprite.spritecollide(sprite_hero, group_enemy, False):
over_game = True
tip_str = 0
if pygame.sprite.collide_rect(sprite_hero, sprite_food):
over_game = True
tip_str = 1
for w in map_total.map_group:
SCREEN.blit(w.surf, w.rect)
for e in group_enemy:
SCREEN.blit(e.surf, e.rect)
e.move()
SCREEN.blit(sprite_food.surf, sprite_food.rect)
SCREEN.blit(sprite_hero.surf, sprite_hero.rect)
if over_game and tip_str == 0:
print('over game')
draw_font(SCREEN, 'Over Game', 80, 540, 480)
elif over_game and tip_str == 1:
print('game victory')
draw_font(SCREEN, 'Game Victory', 80, 540, 480)
pygame.display.update()
clock.tick(FPS)
if __name__ == '__main__':
main()
天天501
- 粉丝: 617
- 资源: 5906
最新资源
- 水电费水电费发发发胜多负少的方法
- recommend system
- WebAPI-1.关于操作元素内容的知识点
- python编辑运行器
- MAE-Masked Autoencoders Are Scalable Vision Learners
- STM32F41xx代码资源
- quark(夸克)正版下载
- 基于ARM Cortex-M3 内核的 STM32F103C8T6 系统板为载体,实现了的智能点阵屏的设计【课程设计/毕业设计】(源码+论文)
- Hierarchical Consensus Hashing for Cross-Modal Retrieval
- 基于 C++ OpenCV视觉库实现的计算机视觉分析,得到手掌上五根手指的长度与宽度、手掌虎口的角度、手掌的宽度以及手腕的宽度 完成对手掌各个参数的精确测量课程设计(源码+报告)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈