|
20鱼币
#贪吃蛇.py
import pygame
import sys
from pygame.locals import *
import 金币
import 蛇身
pygame.init()
pygame.mixer.init()
size = width, height = 600, 400
bg = (0, 0, 255)
YELLOW = (255, 255, 0)
snake = (0, 255, 0)
white = (255, 255, 255)
Sbody = {}
fanxiang = 'right'
left = 300
top = 200
pygame.mouse.set_visible(False)
#pygame.mixer.music.load('Ximalaya-4.0.0_99B_sc100006.exe')
pygame.mixer.music.load('game_music.ogg')
pygame.mixer.music.set_volume(1)
a = pygame.mixer.Sound('upgrade.wav')
screen = pygame.display.set_mode(size)
pygame.display.set_caption('贪吃蛇')
move = USEREVENT
money = USEREVENT + 1
pygame.time.set_timer(move, 1 * 100)
pygame.time.set_timer(money, 5 * 1000)
score = 0
score_font = pygame.font.Font(None, 20)
GameOver_font = pygame.font.Font(None, 100)
Money = pygame.sprite.Group()
snakebody = pygame.sprite.Group()
pygame.mixer.music.play()
running = True
e = True
clock = pygame.time.Clock()
while running:
if e:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == move:
if fanxiang == 'left':
Sbody[0] = [left, top]
left -= 11
for b in snakebody:
Sbody[int(b.number)] = [b.width, b.height]
elif fanxiang == 'top':
Sbody[0] = [left, top]
top -= 11
for b in snakebody:
Sbody[int(b.number)] = [b.width, b.height]
elif fanxiang == 'right':
Sbody[0] = [left, top]
left += 11
for b in snakebody:
Sbody[int(b.number)] = [b.width, b.height]
elif fanxiang == 'bottom':
Sbody[0] = [left, top]
top += 11
for b in snakebody:
Sbody[int(b.number)] = [b.width, b.height]
#生成金币
elif event.type == money:
print(left, top)
m = 金币.money()
Money.add(m)
key_pressed = pygame.key.get_pressed()
#控制方向
if key_pressed[K_w] or key_pressed[K_UP]:
fanxiang = 'top'
elif key_pressed[K_s] or key_pressed[K_DOWN]:
fanxiang = 'bottom'
elif key_pressed[K_d] or key_pressed[K_RIGHT]:
fanxiang = 'right'
elif key_pressed[K_a] or key_pressed[K_LEFT]:
fanxiang = 'left'
if left <= 0 or left >= 600 or top <= 0 or top >= 400:
pygame.mixer.music.stop()
#raise OSError('不能碰到身体或边缘!!!')
e = False
#GameOver = True
screen.fill(bg)
#蛇身机制
for b in snakebody:
lastnumber = b.number - 1
nextnumber = b.number + 1
#print(Sbody)
b.width = Sbody[lastnumber][0]
b.height = Sbody[lastnumber][1]
b.rect = pygame.Rect(b.width, b.height, 10, 10)
pygame.draw.rect(screen, snake, b.rect, 0)
if b.rect.colliderect(pygame.Rect(left, top, 10, 10)):
pygame.mixer.music.stop()
#raise OSError('不能碰到身体或边缘!!!')
e = False
#GameOver = True
#金币机制
for i in Money:
if running:
pygame.draw.rect(screen, YELLOW, i.rect, 0)
if i.rect.colliderect(pygame.Rect(left, top, 10, 10)):
score+=1000
thousand = score / 1000
last = thousand - 1
#增加长度
s = 蛇身.body(int(Sbody[last][0]), int(Sbody[last][1]), thousand, size)
snakebody.add(s)
Money.remove(i)
a.play()
#绘制贪吃蛇
pygame.draw.rect(screen, snake, (left, top, 10, 10), 0)
#显示字迹
score_text = score_font.render(f'Score:{score}', True, white)
screen.blit(score_text, (10, 5))
pygame.display.flip()
elif not e:
GameOver_text = GameOver_font.render('Game Over', True, white)
screen.blit(GameOver_text, (100, 100))
pygame.time.delay(1000)
pygame.display.flip()
clock.tick(10)
#蛇身.py
import pygame
class body(pygame.sprite.Sprite):
def __init__(self, width, height, number, bg_size):
pygame.sprite.Sprite.__init__(self)
self.width = width
self.height = height
self.rect = pygame.Rect(self.width, self.height, 10, 10)
self.number = number
self.left = bg_size[0]
self.top = bg_size[1]
#金币.py
import pygame
import random
class money(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.rect = pygame.Rect(random.randint(0, 590), random.randint(0, 390), 10, 10)
我在测试失败界面时游戏突然卡了,麻烦各位看看是怎么回事,谢谢
在下面这一段代码中,你没有处理退出命令: elif not e:
GameOver_text = GameOver_font.render('Game Over', True, white)
screen.blit(GameOver_text, (100, 100))
pygame.time.delay(1000)
pygame.display.flip()
clock.tick(10)
此外,强制等待1秒钟也会引起卡顿。
解决方案如下: elif not e:
GameOver_text = GameOver_font.render('Game Over', True, white)
screen.blit(GameOver_text, (100, 100))
pygame.display.flip()
clock.tick(10)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
|
最佳答案
查看完整内容
在下面这一段代码中,你没有处理退出命令:
此外,强制等待1秒钟也会引起卡顿。
解决方案如下:
|